|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bavix\Flow; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
6
|
|
|
|
|
7
|
|
|
class Cache |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @var CacheItemPoolInterface |
|
12
|
|
|
*/ |
|
13
|
|
|
protected static $pool; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected static $cache = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
protected static $names = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @return CacheItemPoolInterface |
|
27
|
|
|
*/ |
|
28
|
20 |
|
protected static function getPool() |
|
29
|
|
|
{ |
|
30
|
20 |
|
return static::$pool; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param CacheItemPoolInterface $pool |
|
35
|
|
|
*/ |
|
36
|
24 |
|
public static function setPool($pool) |
|
37
|
|
|
{ |
|
38
|
24 |
|
static::$pool = $pool; |
|
39
|
24 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string $key |
|
43
|
|
|
* |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
20 |
|
protected static function name(string $key): string |
|
47
|
|
|
{ |
|
48
|
20 |
|
if (!isset(static::$names[$key])) |
|
49
|
|
|
{ |
|
50
|
13 |
|
static::$names[$key] = Flow::VERSION . \sha1($key); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
20 |
|
return static::$names[$key]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $key |
|
58
|
|
|
* |
|
59
|
|
|
* @return null|\Psr\Cache\CacheItemInterface |
|
60
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
|
61
|
|
|
*/ |
|
62
|
20 |
|
public static function getItem(string $key) |
|
63
|
|
|
{ |
|
64
|
20 |
|
if (static::getPool()) |
|
65
|
|
|
{ |
|
66
|
2 |
|
return static::$pool->getItem(static::name($key)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
18 |
|
return null; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $key |
|
74
|
|
|
* @param callable $callback |
|
75
|
|
|
* |
|
76
|
|
|
* @return mixed |
|
77
|
|
|
*/ |
|
78
|
19 |
|
protected static function syncCache(string $key, callable $callback) |
|
79
|
|
|
{ |
|
80
|
19 |
|
if (!isset(static::$cache[$key])) |
|
81
|
|
|
{ |
|
82
|
19 |
|
static::$cache[static::name($key)] = $callback(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
19 |
|
return static::$cache[static::name($key)]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param string $key |
|
90
|
|
|
* |
|
91
|
|
|
* @return mixed |
|
92
|
|
|
*/ |
|
93
|
2 |
|
protected static function getCache(string $key) |
|
94
|
|
|
{ |
|
95
|
2 |
|
return static::$cache[static::name($key)] ?? null; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param string $key |
|
100
|
|
|
* @param callable $callback |
|
101
|
|
|
* |
|
102
|
|
|
* @return mixed |
|
103
|
|
|
*/ |
|
104
|
20 |
|
public static function get(string $key, callable $callback) |
|
105
|
|
|
{ |
|
106
|
20 |
|
$item = static::getItem($key); |
|
107
|
|
|
|
|
108
|
20 |
|
if (!$item) |
|
109
|
|
|
{ |
|
110
|
18 |
|
return static::syncCache($key, $callback); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
2 |
|
if (!$item->isHit()) |
|
114
|
|
|
{ |
|
115
|
1 |
|
$item->set( |
|
116
|
1 |
|
static::syncCache($key, $callback) |
|
117
|
|
|
); |
|
118
|
|
|
|
|
119
|
1 |
|
static::getPool()->save($item); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
2 |
|
$cache = static::getCache($key); |
|
123
|
|
|
|
|
124
|
2 |
|
if ($cache) |
|
125
|
|
|
{ |
|
126
|
2 |
|
return $cache; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return $item->get(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|