Skip to content

Commit

Permalink
Refactor CachePoolFacade
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mcaskill committed Mar 11, 2019
1 parent c95e426 commit ce43155
Show file tree
Hide file tree
Showing 5 changed files with 301 additions and 105 deletions.
2 changes: 1 addition & 1 deletion src/Charcoal/Cache/CacheConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class CacheConfig extends AbstractConfig
private $types;

/**
* Time-to-live in seconds.
* Default maximum time an item will be cached.
*
* @var integer
*/
Expand Down
194 changes: 102 additions & 92 deletions src/Charcoal/Cache/Facade/CachePoolFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,152 +2,162 @@

namespace Charcoal\Cache\Facade;

use InvalidArgumentException;

// From PSR-6
use Psr\Cache\CacheItemInterface;

// From 'charcoal-cache'
use Charcoal\Cache\CacheConfig;
use Charcoal\Cache\CachePoolAwareTrait;

/**
* Cache layer to help deal with the cache.
* Cache Pool Facade
*
* The facade provides a simpler interface to work with the cache pool.
*/
class CachePoolFacade
{
use CachePoolAwareTrait;

/**
* @var mixed $cache The cache pool.
*/
private $cache;

/**
* @var mixed $logger Logger.
* Default maximum time an item will be cached.
*
* @var mixed
*/
private $logger;
private $defaultTtl = CacheConfig::HOUR_IN_SECONDS;

/**
* @var integer $ttl Cache time to live
* Create a cache pool facade.
*
* @param array $data The facade dependencies.
*/
private $ttl;

/**
* @param array $container Dependencies.
*/
public function __construct(array $container)
public function __construct(array $data)
{
$this->setCache($container['cache']);
$this->setLogger($container['logger']);
$this->setTtl($container['ttl'] ?: 60);
}

/**
* Get cache object from key or sets it from lambda function.
* @param string $key Key to cache data.
* @param callable|null $lambda Function to generate cache data.
* @param integer|null $ttl Time to live.
* @return mixed Whatever was in the cache.
*/
public function get($key, $lambda = null, $ttl = null)
{
$cache = $this->cache();
$cacheItem = $cache->getItem($key);
$out = $cacheItem->get();

if (!$cacheItem->isMiss()) {
return $out;
}
$this->setCachePool($data['cache']);

if (is_callable($lambda)) {
$out = call_user_func($lambda);
$this->set($cacheItem, $out, $ttl);
if (isset($data['default_ttl'])) {
$this->setDefaultTtl($data['default_ttl']);
}

return $out;
}

/**
* Sets the cache data.
* @param string $cacheItem Cache item.
* @param mixed $out Data to set in cache.
* @param mixed $ttl Time to live.
* @return self
* Retrieve the value associated with the specified key from the pool.
*
* This method will call $lambada if the cache item representing $key resulted in a cache miss.
*
* @param string $key The key for which to return the associated value.
* @param callable|null $resolve The function to execute if the cached value does not exist
* or is considered expired. The function must return a value which will be stored
* in the cache before being returned by the method.
* @param integer|null $ttl The time-to-live, in seconds, after which the cached value
* must be considered expired.
* If NULL, the facade's default time-to-live is used.
* @return mixed The value corresponding to this cache item's $key, or NULL if not found.
*/
public function set($cacheItem, $out, $ttl = null)
public function get($key, callable $resolve = null, $ttl = null)
{
$cacheItem->lock();
$pool = $this->cachePool();
$item = $pool->getItem($key);
$data = $item->get();

if (!$ttl) {
$ttl = $this->ttl();
if ($item->isHit()) {
return $data;
}

$cache = $this->cache();

$cacheItem->expiresAfter($ttl);
$cacheItem->set($out);
$cache->save($cacheItem);
if (is_callable($resolve)) {
$item->lock();
$data = call_user_func($resolve);
$this->save($item, $data, $ttl);
return $data;
}

return $this;
return null;
}

/**
* Removes the object from the cache.
* @param string $key Key to object.
* @return self
* Determine if the specified key results in a cache hit.
*
* @param string $key The key for which to check existence.
* @return boolean TRUE if item exists in the cache, FALSE otherwise.
*/
public function delete($key)
public function has($key)
{
$this->cache->deleteItem($key);

return $this;
return $this->cachePool()->getItem($key)->isHit();
}

/**
* @return mixed
* Store a value with the specified key to be saved immediately.
*
* @param string $key The key to save the value on.
* @param mixed $value The serializable value to be stored.
* @param integer|null $ttl The time-to-live, in seconds.
* @return boolean TRUE if the item was successfully persisted. FALSE if there was an error.
*/
public function cache()
public function set($key, $value, $ttl = null)
{
return $this->cache;
$item = $this->cachePool()->getItem($key);

return $this->save($item, $value, $ttl);
}

/**
* @param mixed $cache Cache for CachePoolFacade.
* @return self
* Set a value on a cache item to be saved immediately.
*
* @param CacheItemInterface $item The cache item to save.
* @param mixed $value The serializable value to be stored.
* @param integer|null $ttl The time-to-live, in seconds.
* @return boolean TRUE if the item was successfully persisted. FALSE if there was an error.
*/
public function setCache($cache)
protected function save(CacheItemInterface $item, $value, $ttl = null)
{
$this->cache = $cache;
if (!$ttl) {
$ttl = $this->defaultTtl();
}

return $this;
return $item->setTTL($ttl)
->set($value)
->save();
}

/**
* @return mixed
* Removes one or more items from the pool.
*
* @param string[] ...$keys One or many keys to delete.
* @return bool TRUE if the item was successfully removed. FALSE if there was an error.
*/
public function logger()
public function delete(...$keys)
{
return $this->logger;
}
$pool = $this->cachePool();

/**
* @param mixed $logger Logger for CachePoolFacade.
* @return self
*/
public function setLogger($logger)
{
$this->logger = $logger;
$results = true;
foreach ($keys as $key) {
$results = $pool->deleteItem($key) && $results;
}

return $this;
return $results;
}

/**
* @return mixed
* Retrieve the facade's default time-to-live for cached items.
*
* @return mixed An integer, date interval, or date.
*/
public function ttl()
public function defaultTtl()
{
return $this->ttl;
return $this->defaultTtl;
}

/**
* @param mixed $ttl Cache time to live.
* @return self
* Set the facade's default time-to-live for cached items.
*
* @see \Stash\Item::setTTL()
*
* @param mixed $ttl An integer, date interval, or date.
* @return void
*/
public function setTtl($ttl)
public function setDefaultTtl($ttl)
{
$this->ttl = $ttl;

return $this;
$this->defaultTtl = $ttl;
}
}
10 changes: 6 additions & 4 deletions src/Charcoal/Cache/ServiceProvider/CacheServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public function registerDrivers(Container $container)
// Apc is not available on system
return null;
}

return new $drivers['Apc']([
'ttl' => $container['cache/config']['default_ttl'],
'namespace' => $container['cache/config']['prefix'],
Expand All @@ -103,6 +104,7 @@ public function registerDrivers(Container $container)
// SQLite is not available on system
return null;
}

return new $drivers['SQLite']();
};

Expand Down Expand Up @@ -173,6 +175,7 @@ public function registerDrivers(Container $container)
// Redis is not available on system
return null;
}

return new $drivers['Redis']();
};

Expand Down Expand Up @@ -251,13 +254,12 @@ public function registerService(Container $container)
*/
$container['cache/facade'] = function (Container $container) {
$args = [
'cache' => $container['cache'],
'logger' => $container['logger'],
'cache' => $container['cache'],
];

$cacheConfig = $container['cache/config'];
if (isset($cacheConfig['ttl'])) {
$args['ttl'] = $cacheConfig['ttl'];
if (isset($cacheConfig['default_ttl'])) {
$args['default_ttl'] = $cacheConfig['default_ttl'];
}

return new CachePoolFacade($args);
Expand Down
Loading

0 comments on commit ce43155

Please sign in to comment.