|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WebComplete\core\utils\cache; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface; |
|
6
|
|
|
use Symfony\Component\Cache\CacheItem; |
|
7
|
|
|
|
|
8
|
|
|
class Cache |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var CacheService |
|
12
|
|
|
*/ |
|
13
|
|
|
protected static $cacheService; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @param string $key |
|
17
|
|
|
* |
|
18
|
|
|
* @return mixed|null |
|
19
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
|
20
|
|
|
* @throws \RuntimeException |
|
21
|
|
|
*/ |
|
22
|
|
|
public static function get(string $key) |
|
23
|
|
|
{ |
|
24
|
|
|
$item = self::getCacheService()->user()->getItem($key); |
|
25
|
|
|
if ($item->isHit()) { |
|
26
|
|
|
return $item->get(); |
|
27
|
|
|
} |
|
28
|
|
|
return null; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param string $key |
|
33
|
|
|
* @param $value |
|
34
|
|
|
* @param int|null $ttl |
|
35
|
|
|
* @param array $tags |
|
36
|
|
|
* |
|
37
|
|
|
* @throws \RuntimeException |
|
38
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
|
39
|
|
|
* @throws \Symfony\Component\Cache\Exception\InvalidArgumentException |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function set(string $key, $value, int $ttl = null, array $tags = []) |
|
42
|
|
|
{ |
|
43
|
|
|
$cache = self::getCacheService()->user(); |
|
44
|
|
|
$item = $cache->getItem($key); |
|
45
|
|
|
$item->set($value); |
|
46
|
|
|
if ($ttl) { |
|
|
|
|
|
|
47
|
|
|
$item->expiresAfter($ttl); |
|
48
|
|
|
} |
|
49
|
|
|
if ($tags && $item instanceof CacheItem) { |
|
|
|
|
|
|
50
|
|
|
$item->tag($tags); |
|
51
|
|
|
} |
|
52
|
|
|
self::getCacheService()->user()->save($item); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $key |
|
57
|
|
|
* @param \Closure $closure |
|
58
|
|
|
* @param int|null $ttl |
|
59
|
|
|
* @param array $tags |
|
60
|
|
|
* |
|
61
|
|
|
* @return mixed |
|
62
|
|
|
* @throws \Symfony\Component\Cache\Exception\InvalidArgumentException |
|
63
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
|
64
|
|
|
* @throws \RuntimeException |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function getOrSet(string $key, \Closure $closure, int $ttl = null, array $tags = []) |
|
67
|
|
|
{ |
|
68
|
|
|
$item = self::getCacheService()->user()->getItem($key); |
|
69
|
|
|
if ($item->isHit()) { |
|
70
|
|
|
return $item->get(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$result = $closure(); |
|
74
|
|
|
$item->set($result); |
|
75
|
|
|
if ($ttl) { |
|
|
|
|
|
|
76
|
|
|
$item->expiresAfter($ttl); |
|
77
|
|
|
} |
|
78
|
|
|
if ($tags && $item instanceof CacheItem) { |
|
|
|
|
|
|
79
|
|
|
$item->tag($tags); |
|
80
|
|
|
} |
|
81
|
|
|
self::getCacheService()->user()->save($item); |
|
82
|
|
|
return $result; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param string $key |
|
87
|
|
|
* |
|
88
|
|
|
* @throws \RuntimeException |
|
89
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
|
90
|
|
|
*/ |
|
91
|
|
|
public static function invalidate(string $key) |
|
92
|
|
|
{ |
|
93
|
|
|
self::getCacheService()->user()->deleteItem($key); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param array $tags |
|
98
|
|
|
* |
|
99
|
|
|
* @throws \RuntimeException |
|
100
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
|
101
|
|
|
*/ |
|
102
|
|
|
public static function invalidateTags(array $tags) |
|
103
|
|
|
{ |
|
104
|
|
|
$cache = self::getCacheService()->user(); |
|
105
|
|
|
if ($cache instanceof TagAwareAdapterInterface) { |
|
106
|
|
|
$cache->invalidateTags($tags); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param string $key |
|
112
|
|
|
* @param int|null $ttl |
|
113
|
|
|
* @param array $tags |
|
114
|
|
|
* |
|
115
|
|
|
* @return null|HtmlCache |
|
116
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
|
117
|
|
|
* @throws \RuntimeException |
|
118
|
|
|
*/ |
|
119
|
|
|
public static function html(string $key, int $ttl = null, array $tags = []) |
|
120
|
|
|
{ |
|
121
|
|
|
if ($content = self::get($key)) { |
|
122
|
|
|
echo $content; |
|
123
|
|
|
return null; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
ob_start(); |
|
127
|
|
|
return new HtmlCache(self::getCacheService(), $key, $ttl, $tags); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param CacheService $service |
|
132
|
|
|
*/ |
|
133
|
|
|
public static function setCacheService(CacheService $service) |
|
134
|
|
|
{ |
|
135
|
|
|
self::$cacheService = $service; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @return CacheService |
|
140
|
|
|
* @throws \RuntimeException |
|
141
|
|
|
*/ |
|
142
|
|
|
protected static function getCacheService(): CacheService |
|
143
|
|
|
{ |
|
144
|
|
|
if (!self::$cacheService) { |
|
145
|
|
|
throw new \RuntimeException('CacheService is not defined'); |
|
146
|
|
|
} |
|
147
|
|
|
return self::$cacheService; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: