|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Charcoal\Cache\Facade; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Cache layer to help deal with the cache. |
|
7
|
|
|
*/ |
|
8
|
|
|
class CachePoolFacade |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var mixed $cache The cache pool. |
|
13
|
|
|
*/ |
|
14
|
|
|
private $cache; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var mixed $logger Logger. |
|
18
|
|
|
*/ |
|
19
|
|
|
private $logger; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var integer $ttl Cache time to live |
|
23
|
|
|
*/ |
|
24
|
|
|
private $ttl; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param array $container Dependencies. |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(array $container) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->setCache($container['cache']); |
|
32
|
|
|
$this->setLogger($container['logger']); |
|
33
|
|
|
$this->setTtl($container['ttl'] ?: 60); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Get cache object from key or sets it from lambda function. |
|
38
|
|
|
* @param string $key Key to cache data. |
|
39
|
|
|
* @param callable|null $lambda Function to generate cache data. |
|
40
|
|
|
* @param integer|null $ttl Time to live. |
|
41
|
|
|
* @return mixed Whatever was in the cache. |
|
42
|
|
|
*/ |
|
43
|
|
|
public function get($key, $lambda = null, $ttl = null) |
|
44
|
|
|
{ |
|
45
|
|
|
$cache = $this->cache(); |
|
46
|
|
|
$cacheItem = $cache->getItem($key); |
|
47
|
|
|
$out = $cacheItem->get(); |
|
48
|
|
|
|
|
49
|
|
|
if (!$cacheItem->isMiss()) { |
|
50
|
|
|
return $out; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if (is_callable($lambda)) { |
|
54
|
|
|
$out = call_user_func($lambda); |
|
55
|
|
|
$this->set($cacheItem, $out, $ttl); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $out; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Sets the cache data. |
|
63
|
|
|
* @param string $cacheItem Cache item. |
|
64
|
|
|
* @param mixed $out Data to set in cache. |
|
65
|
|
|
* @param mixed $ttl Time to live. |
|
66
|
|
|
* @return self |
|
67
|
|
|
*/ |
|
68
|
|
|
public function set($cacheItem, $out, $ttl = null) |
|
69
|
|
|
{ |
|
70
|
|
|
$cacheItem->lock(); |
|
71
|
|
|
|
|
72
|
|
|
if (!$ttl) { |
|
73
|
|
|
$ttl = $this->ttl(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$cache = $this->cache(); |
|
77
|
|
|
|
|
78
|
|
|
$cacheItem->expiresAfter($ttl); |
|
79
|
|
|
$cacheItem->set($out); |
|
80
|
|
|
$cache->save($cacheItem); |
|
81
|
|
|
|
|
82
|
|
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Removes the object from the cache. |
|
87
|
|
|
* @param string $key Key to object. |
|
88
|
|
|
* @return self |
|
89
|
|
|
*/ |
|
90
|
|
|
public function delete($key) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->cache->deleteItem($key); |
|
93
|
|
|
|
|
94
|
|
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return mixed |
|
99
|
|
|
*/ |
|
100
|
|
|
public function cache() |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->cache; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param mixed $cache Cache for CachePoolFacade. |
|
107
|
|
|
* @return self |
|
108
|
|
|
*/ |
|
109
|
|
|
public function setCache($cache) |
|
110
|
|
|
{ |
|
111
|
|
|
$this->cache = $cache; |
|
112
|
|
|
|
|
113
|
|
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return mixed |
|
118
|
|
|
*/ |
|
119
|
|
|
public function logger() |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->logger; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param mixed $logger Logger for CachePoolFacade. |
|
126
|
|
|
* @return self |
|
127
|
|
|
*/ |
|
128
|
|
|
public function setLogger($logger) |
|
129
|
|
|
{ |
|
130
|
|
|
$this->logger = $logger; |
|
131
|
|
|
|
|
132
|
|
|
return $this; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return mixed |
|
137
|
|
|
*/ |
|
138
|
|
|
public function ttl() |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->ttl; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param mixed $ttl Cache time to live. |
|
145
|
|
|
* @return self |
|
146
|
|
|
*/ |
|
147
|
|
|
public function setTtl($ttl) |
|
148
|
|
|
{ |
|
149
|
|
|
$this->ttl = $ttl; |
|
150
|
|
|
|
|
151
|
|
|
return $this; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|