Skip to content

Commit

Permalink
Made improvements to cache storage classes (#24)
Browse files Browse the repository at this point in the history
* made improvements to cache storage classes

* fixed incorrect logic in Cache::storage()
  • Loading branch information
garrettw authored and gjerokrsteski committed Jun 22, 2017
1 parent d7e3a79 commit 9cdf200
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 10 deletions.
6 changes: 5 additions & 1 deletion core/Pimf/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ class Cache
*
* @return CS\Apc|CS\Dba|CS\File|CS\Memcached|CS\Memory|CS\Pdo|CS\Redis|CS\WinCache
*/
public static function storage($storage = 'memory')
public static function storage($storage = null)
{
if ($storage === null) {
$storage = Config::get('cache.storage') ?: 'memory';
}

if (!isset(static::$storages[$storage])) {
static::$storages[$storage] = static::factory($storage);
}
Expand Down
2 changes: 1 addition & 1 deletion core/Pimf/Cache/Storages/Dba.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function put($key, $value, $minutes)
return;
}

$value = $this->expiration($minutes) . serialize($value);
$value = self::expiration($minutes) . serialize($value);

if (true === $this->has($key)) {
return dba_replace($key, $value, $this->dba);
Expand Down
25 changes: 20 additions & 5 deletions core/Pimf/Cache/Storages/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,35 @@ public function __construct($path)
$this->path = $path;
}

/**
* Determine if an item exists in the cache.
*
* @param $key
*
* @return bool
*/
public function has($key)
{
return file_exists($this->path . $key);
}

/**
* @param string $key
*
* @return bool|mixed|null
*/
protected function retrieve($key)
{
if (!file_exists($this->path . $key)) {
if (!$this->has($key)) {
return null;
}

$cache = file_get_contents($this->path . $key);

// compare the timestamp to the current time when we read the file.
if (time() >= substr($cache = file_get_contents($this->path . $key), 0, 10)) {
return $this->forget($key);
if (time() >= substr($cache, 0, 10)) {
$this->forget($key);
return null;
}

return unserialize(substr($cache, 10));
Expand All @@ -68,7 +83,7 @@ public function put($key, $value, $minutes)
return null;
}

$value = $this->expiration($minutes) . serialize($value);
$value = self::expiration($minutes) . serialize($value);

return file_put_contents($this->path . $key, $value, LOCK_EX);
}
Expand All @@ -95,7 +110,7 @@ public function forever($key, $value)
*/
public function forget($key)
{
if (file_exists($this->path . $key)) {
if ($this->has($key)) {

unlink($this->path . $key);

Expand Down
2 changes: 1 addition & 1 deletion core/Pimf/Cache/Storages/Pdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function put($key, $value, $minutes)
{
$key = $this->key . $key;
$value = serialize($value);
$expiration = $this->expiration($minutes);
$expiration = self::expiration($minutes);

try {
$sth = $this->pdo->prepare(
Expand Down
72 changes: 70 additions & 2 deletions core/Pimf/Cache/Storages/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @package Cache_Storages
* @author Gjero Krsteski <[email protected]>
*/
abstract class Storage
abstract class Storage implements \ArrayAccess
{
/**
* Determine if an item exists in the cache.
Expand All @@ -26,6 +26,20 @@ public function has($key)
return ($this->get($key) !== null);
}

/**
* Determine if an item exists in the cache.
*
* Enables you to use: isset($storage[$key])
*
* @param $offset
*
* @return bool
*/
public function offsetExists($offset)
{
return $this->has($offset);
}

/**
* Get an item from the cache.
*
Expand All @@ -47,6 +61,25 @@ public function get($key, $default = null)
return (!is_null($item = $this->retrieve($key))) ? $item : $default;
}

/**
* Get an item from the cache.
*
* Enables you to use: $storage[$key]
*
* <code>
* // Get an item from the cache storage
* $name = Cache::storage()['name'];
* </code>
*
* @param $offset
*
* @return mixed|null
*/
public function offsetGet($offset)
{
return $this->get($offset);
}

/**
* Retrieve an item from the cache storage.
*
Expand All @@ -72,6 +105,19 @@ abstract protected function retrieve($key);
*/
abstract public function put($key, $value, $minutes);

/**
* Write an item to the cache for indefinite-term storage.
*
* Enables you to use: $storage[$key] = $value;
*
* @param $key
* @param $value
*/
public function offsetSet($key, $value)
{
$this->forever($key, value);
}

/**
* Get an item from the cache, or cache and return the default value.
*
Expand Down Expand Up @@ -101,6 +147,16 @@ public function remember($key, $default, $minutes, $function = 'put')
return $default;
}

/**
* Write an item to the cache for indefinite-term storage.
*
* @param $key
* @param $value
*
* @return mixed Depends on implementation
*/
abstract public function forever($key, $value);

/**
* Get an item from the cache, or cache the default value forever.
*
Expand All @@ -123,14 +179,26 @@ public function sear($key, $default)
*/
abstract public function forget($key);

/**
* Delete an item from the cache.
*
* Enables you to use: unset($storage[$key]);
*
* @param string $key
*/
public function offsetUnset($key)
{
$this->forget($key);
}

/**
* Get the expiration time as a UNIX timestamp.
*
* @param int $minutes
*
* @return int
*/
protected function expiration($minutes)
protected static function expiration($minutes)
{
return time() + ($minutes * 60);
}
Expand Down

0 comments on commit 9cdf200

Please sign in to comment.