-
Notifications
You must be signed in to change notification settings - Fork 21
/
Cache.php
42 lines (34 loc) · 1.1 KB
/
Cache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
namespace go1\util;
use Doctrine\Common\Cache\Cache as CacheInterface;
class Cache
{
public static function cacheId(string $entityType, string $entityId)
{
return "{$entityType}:{$entityId}";
}
public static function eTag(string $entityType, string $entityId, int $update): string
{
return md5(self::cacheId($entityType, $entityId) . ":etag:{$update}");
}
public static function setETag(CacheInterface $cache, string $cacheId, string $eTag)
{
# Invalid previous eTag.
if ($cache->contains("{$cacheId}:eTag")) {
if ($prevETag = $cache->fetch("{$cacheId}:eTag")) {
$cache->delete($prevETag);
}
}
$cache->save("{$cacheId}:eTag", $eTag);
$cache->save($eTag, time());
}
public static function invalidate(CacheInterface $cache, string $cacheId)
{
if ($cache->contains("{$cacheId}:eTag")) {
if ($prevETag = $cache->fetch("{$cacheId}:eTag")) {
$cache->delete($prevETag);
}
$cache->delete("$cacheId:eTag");
}
}
}