|
2 | 2 |
|
3 | 3 | namespace Charcoal\Cache\Facade;
|
4 | 4 |
|
| 5 | +use InvalidArgumentException; |
| 6 | + |
| 7 | +// From PSR-6 |
| 8 | +use Psr\Cache\CacheItemInterface; |
| 9 | + |
| 10 | +// From 'charcoal-cache' |
| 11 | +use Charcoal\Cache\CacheConfig; |
| 12 | +use Charcoal\Cache\CachePoolAwareTrait; |
| 13 | + |
5 | 14 | /**
|
6 |
| - * Cache layer to help deal with the cache. |
| 15 | + * Cache Pool Facade |
| 16 | + * |
| 17 | + * The facade provides a simpler interface to work with the cache pool. |
7 | 18 | */
|
8 | 19 | class CachePoolFacade
|
9 | 20 | {
|
| 21 | + use CachePoolAwareTrait; |
10 | 22 |
|
11 | 23 | /**
|
12 |
| - * @var mixed $cache The cache pool. |
13 |
| - */ |
14 |
| - private $cache; |
15 |
| - |
16 |
| - /** |
17 |
| - * @var mixed $logger Logger. |
| 24 | + * Default maximum time an item will be cached. |
| 25 | + * |
| 26 | + * @var mixed |
18 | 27 | */
|
19 |
| - private $logger; |
| 28 | + private $defaultTtl = CacheConfig::HOUR_IN_SECONDS; |
20 | 29 |
|
21 | 30 | /**
|
22 |
| - * @var integer $ttl Cache time to live |
| 31 | + * Create a cache pool facade. |
| 32 | + * |
| 33 | + * @param array $data The facade dependencies. |
23 | 34 | */
|
24 |
| - private $ttl; |
25 |
| - |
26 |
| - /** |
27 |
| - * @param array $container Dependencies. |
28 |
| - */ |
29 |
| - public function __construct(array $container) |
| 35 | + public function __construct(array $data) |
30 | 36 | {
|
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 |
| - } |
| 37 | + $this->setCachePool($data['cache']); |
52 | 38 |
|
53 |
| - if (is_callable($lambda)) { |
54 |
| - $out = call_user_func($lambda); |
55 |
| - $this->set($cacheItem, $out, $ttl); |
| 39 | + if (isset($data['default_ttl'])) { |
| 40 | + $this->setDefaultTtl($data['default_ttl']); |
56 | 41 | }
|
57 |
| - |
58 |
| - return $out; |
59 | 42 | }
|
60 | 43 |
|
61 | 44 | /**
|
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 |
| 45 | + * Retrieve the value associated with the specified key from the pool. |
| 46 | + * |
| 47 | + * This method will call $lambada if the cache item representing $key resulted in a cache miss. |
| 48 | + * |
| 49 | + * @param string $key The key for which to return the associated value. |
| 50 | + * @param callable|null $resolve The function to execute if the cached value does not exist |
| 51 | + * or is considered expired. The function must return a value which will be stored |
| 52 | + * in the cache before being returned by the method. |
| 53 | + * @param integer|null $ttl The time-to-live, in seconds, after which the cached value |
| 54 | + * must be considered expired. |
| 55 | + * If NULL, the facade's default time-to-live is used. |
| 56 | + * @return mixed The value corresponding to this cache item's $key, or NULL if not found. |
67 | 57 | */
|
68 |
| - public function set($cacheItem, $out, $ttl = null) |
| 58 | + public function get($key, callable $resolve = null, $ttl = null) |
69 | 59 | {
|
70 |
| - $cacheItem->lock(); |
| 60 | + $pool = $this->cachePool(); |
| 61 | + $item = $pool->getItem($key); |
| 62 | + $data = $item->get(); |
71 | 63 |
|
72 |
| - if (!$ttl) { |
73 |
| - $ttl = $this->ttl(); |
| 64 | + if ($item->isHit()) { |
| 65 | + return $data; |
74 | 66 | }
|
75 | 67 |
|
76 |
| - $cache = $this->cache(); |
77 |
| - |
78 |
| - $cacheItem->expiresAfter($ttl); |
79 |
| - $cacheItem->set($out); |
80 |
| - $cache->save($cacheItem); |
| 68 | + if (is_callable($resolve)) { |
| 69 | + $item->lock(); |
| 70 | + $data = call_user_func($resolve); |
| 71 | + $this->save($item, $data, $ttl); |
| 72 | + return $data; |
| 73 | + } |
81 | 74 |
|
82 |
| - return $this; |
| 75 | + return null; |
83 | 76 | }
|
84 | 77 |
|
85 | 78 | /**
|
86 |
| - * Removes the object from the cache. |
87 |
| - * @param string $key Key to object. |
88 |
| - * @return self |
| 79 | + * Determine if the specified key results in a cache hit. |
| 80 | + * |
| 81 | + * @param string $key The key for which to check existence. |
| 82 | + * @return boolean TRUE if item exists in the cache, FALSE otherwise. |
89 | 83 | */
|
90 |
| - public function delete($key) |
| 84 | + public function has($key) |
91 | 85 | {
|
92 |
| - $this->cache->deleteItem($key); |
93 |
| - |
94 |
| - return $this; |
| 86 | + return $this->cachePool()->getItem($key)->isHit(); |
95 | 87 | }
|
96 | 88 |
|
97 | 89 | /**
|
98 |
| - * @return mixed |
| 90 | + * Store a value with the specified key to be saved immediately. |
| 91 | + * |
| 92 | + * @param string $key The key to save the value on. |
| 93 | + * @param mixed $value The serializable value to be stored. |
| 94 | + * @param integer|null $ttl The time-to-live, in seconds. |
| 95 | + * @return boolean TRUE if the item was successfully persisted. FALSE if there was an error. |
99 | 96 | */
|
100 |
| - public function cache() |
| 97 | + public function set($key, $value, $ttl = null) |
101 | 98 | {
|
102 |
| - return $this->cache; |
| 99 | + $item = $this->cachePool()->getItem($key); |
| 100 | + |
| 101 | + return $this->save($item, $value, $ttl); |
103 | 102 | }
|
104 | 103 |
|
105 | 104 | /**
|
106 |
| - * @param mixed $cache Cache for CachePoolFacade. |
107 |
| - * @return self |
| 105 | + * Set a value on a cache item to be saved immediately. |
| 106 | + * |
| 107 | + * @param CacheItemInterface $item The cache item to save. |
| 108 | + * @param mixed $value The serializable value to be stored. |
| 109 | + * @param integer|null $ttl The time-to-live, in seconds. |
| 110 | + * @return boolean TRUE if the item was successfully persisted. FALSE if there was an error. |
108 | 111 | */
|
109 |
| - public function setCache($cache) |
| 112 | + protected function save(CacheItemInterface $item, $value, $ttl = null) |
110 | 113 | {
|
111 |
| - $this->cache = $cache; |
| 114 | + if (!$ttl) { |
| 115 | + $ttl = $this->defaultTtl(); |
| 116 | + } |
112 | 117 |
|
113 |
| - return $this; |
| 118 | + return $item->setTTL($ttl) |
| 119 | + ->set($value) |
| 120 | + ->save(); |
114 | 121 | }
|
115 | 122 |
|
116 | 123 | /**
|
117 |
| - * @return mixed |
| 124 | + * Removes one or more items from the pool. |
| 125 | + * |
| 126 | + * @param string[] ...$keys One or many keys to delete. |
| 127 | + * @return bool TRUE if the item was successfully removed. FALSE if there was an error. |
118 | 128 | */
|
119 |
| - public function logger() |
| 129 | + public function delete(...$keys) |
120 | 130 | {
|
121 |
| - return $this->logger; |
122 |
| - } |
| 131 | + $pool = $this->cachePool(); |
123 | 132 |
|
124 |
| - /** |
125 |
| - * @param mixed $logger Logger for CachePoolFacade. |
126 |
| - * @return self |
127 |
| - */ |
128 |
| - public function setLogger($logger) |
129 |
| - { |
130 |
| - $this->logger = $logger; |
| 133 | + $results = true; |
| 134 | + foreach ($keys as $key) { |
| 135 | + $results = $pool->deleteItem($key) && $results; |
| 136 | + } |
131 | 137 |
|
132 |
| - return $this; |
| 138 | + return $results; |
133 | 139 | }
|
134 | 140 |
|
135 | 141 | /**
|
136 |
| - * @return mixed |
| 142 | + * Retrieve the facade's default time-to-live for cached items. |
| 143 | + * |
| 144 | + * @return mixed An integer, date interval, or date. |
137 | 145 | */
|
138 |
| - public function ttl() |
| 146 | + public function defaultTtl() |
139 | 147 | {
|
140 |
| - return $this->ttl; |
| 148 | + return $this->defaultTtl; |
141 | 149 | }
|
142 | 150 |
|
143 | 151 | /**
|
144 |
| - * @param mixed $ttl Cache time to live. |
145 |
| - * @return self |
| 152 | + * Set the facade's default time-to-live for cached items. |
| 153 | + * |
| 154 | + * @see \Stash\Item::setTTL() |
| 155 | + * |
| 156 | + * @param mixed $ttl An integer, date interval, or date. |
| 157 | + * @return void |
146 | 158 | */
|
147 |
| - public function setTtl($ttl) |
| 159 | + public function setDefaultTtl($ttl) |
148 | 160 | {
|
149 |
| - $this->ttl = $ttl; |
150 |
| - |
151 |
| - return $this; |
| 161 | + $this->defaultTtl = $ttl; |
152 | 162 | }
|
153 | 163 | }
|
0 commit comments