Skip to content

Commit ce43155

Browse files
committed
Refactor CachePoolFacade
Added: - Method `CachePoolFacade::has()` to check if the cache item hits - Method `CachePoolFacade::set()` to store a literal value (renamed previous `set()` to `save()`) - Unit tests for `CachePoolFacade` Changed: - Replaced cache setter/getter with `CachePoolAwareTrait` - Replaced 60 second TTL with 1 hour (via `CacheConfig::HOUR_IN_SECONDS`) - Renamed the option/setter/getter "ttl" to "default_ttl" - Renamed parameter `$lambda` to `$resolve` on method `CachePoolFacade::get()` - Renamed `CachePoolFacade::set()` method to `save()` (added new `set()`) - Methods `save()` and `delete()` on `CachePoolFacade` to return a boolean instead of self - Method `CachePoolFacade::delete()` to accept multiple keys - Cleanup documentation of class, properties, methods Removed: - Logger from `CachePoolFacade` Fixed: - Various lint issues
1 parent c95e426 commit ce43155

File tree

5 files changed

+301
-105
lines changed

5 files changed

+301
-105
lines changed

src/Charcoal/Cache/CacheConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class CacheConfig extends AbstractConfig
4949
private $types;
5050

5151
/**
52-
* Time-to-live in seconds.
52+
* Default maximum time an item will be cached.
5353
*
5454
* @var integer
5555
*/

src/Charcoal/Cache/Facade/CachePoolFacade.php

Lines changed: 102 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -2,152 +2,162 @@
22

33
namespace Charcoal\Cache\Facade;
44

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+
514
/**
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.
718
*/
819
class CachePoolFacade
920
{
21+
use CachePoolAwareTrait;
1022

1123
/**
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
1827
*/
19-
private $logger;
28+
private $defaultTtl = CacheConfig::HOUR_IN_SECONDS;
2029

2130
/**
22-
* @var integer $ttl Cache time to live
31+
* Create a cache pool facade.
32+
*
33+
* @param array $data The facade dependencies.
2334
*/
24-
private $ttl;
25-
26-
/**
27-
* @param array $container Dependencies.
28-
*/
29-
public function __construct(array $container)
35+
public function __construct(array $data)
3036
{
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']);
5238

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']);
5641
}
57-
58-
return $out;
5942
}
6043

6144
/**
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.
6757
*/
68-
public function set($cacheItem, $out, $ttl = null)
58+
public function get($key, callable $resolve = null, $ttl = null)
6959
{
70-
$cacheItem->lock();
60+
$pool = $this->cachePool();
61+
$item = $pool->getItem($key);
62+
$data = $item->get();
7163

72-
if (!$ttl) {
73-
$ttl = $this->ttl();
64+
if ($item->isHit()) {
65+
return $data;
7466
}
7567

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+
}
8174

82-
return $this;
75+
return null;
8376
}
8477

8578
/**
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.
8983
*/
90-
public function delete($key)
84+
public function has($key)
9185
{
92-
$this->cache->deleteItem($key);
93-
94-
return $this;
86+
return $this->cachePool()->getItem($key)->isHit();
9587
}
9688

9789
/**
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.
9996
*/
100-
public function cache()
97+
public function set($key, $value, $ttl = null)
10198
{
102-
return $this->cache;
99+
$item = $this->cachePool()->getItem($key);
100+
101+
return $this->save($item, $value, $ttl);
103102
}
104103

105104
/**
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.
108111
*/
109-
public function setCache($cache)
112+
protected function save(CacheItemInterface $item, $value, $ttl = null)
110113
{
111-
$this->cache = $cache;
114+
if (!$ttl) {
115+
$ttl = $this->defaultTtl();
116+
}
112117

113-
return $this;
118+
return $item->setTTL($ttl)
119+
->set($value)
120+
->save();
114121
}
115122

116123
/**
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.
118128
*/
119-
public function logger()
129+
public function delete(...$keys)
120130
{
121-
return $this->logger;
122-
}
131+
$pool = $this->cachePool();
123132

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+
}
131137

132-
return $this;
138+
return $results;
133139
}
134140

135141
/**
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.
137145
*/
138-
public function ttl()
146+
public function defaultTtl()
139147
{
140-
return $this->ttl;
148+
return $this->defaultTtl;
141149
}
142150

143151
/**
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
146158
*/
147-
public function setTtl($ttl)
159+
public function setDefaultTtl($ttl)
148160
{
149-
$this->ttl = $ttl;
150-
151-
return $this;
161+
$this->defaultTtl = $ttl;
152162
}
153163
}

src/Charcoal/Cache/ServiceProvider/CacheServiceProvider.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public function registerDrivers(Container $container)
8787
// Apc is not available on system
8888
return null;
8989
}
90+
9091
return new $drivers['Apc']([
9192
'ttl' => $container['cache/config']['default_ttl'],
9293
'namespace' => $container['cache/config']['prefix'],
@@ -103,6 +104,7 @@ public function registerDrivers(Container $container)
103104
// SQLite is not available on system
104105
return null;
105106
}
107+
106108
return new $drivers['SQLite']();
107109
};
108110

@@ -173,6 +175,7 @@ public function registerDrivers(Container $container)
173175
// Redis is not available on system
174176
return null;
175177
}
178+
176179
return new $drivers['Redis']();
177180
};
178181

@@ -251,13 +254,12 @@ public function registerService(Container $container)
251254
*/
252255
$container['cache/facade'] = function (Container $container) {
253256
$args = [
254-
'cache' => $container['cache'],
255-
'logger' => $container['logger'],
257+
'cache' => $container['cache'],
256258
];
257259

258260
$cacheConfig = $container['cache/config'];
259-
if (isset($cacheConfig['ttl'])) {
260-
$args['ttl'] = $cacheConfig['ttl'];
261+
if (isset($cacheConfig['default_ttl'])) {
262+
$args['default_ttl'] = $cacheConfig['default_ttl'];
261263
}
262264

263265
return new CachePoolFacade($args);

0 commit comments

Comments
 (0)