banker/src/Driver/NullDriver.php

160 lines
2.5 KiB
PHP
Raw Normal View History

2016-09-05 16:43:37 -04:00
<?php
/**
* Banker
*
* A Caching library implementing psr/cache
*
* PHP version 5.6
*
* @package Banker
* @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2016 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 1.0.0
* @link https://git.timshomepage.net/timw4mail/banker
*/
namespace Aviat\Banker\Driver;
/**
* Cache backend for use without a cache server. Only does transient
* in-memory caching
*/
2016-09-06 17:03:43 -04:00
class NullDriver extends AbstractDriver {
2016-09-05 16:43:37 -04:00
/**
* In memory store
*
* @var array
*/
protected $store = [];
/**
* NullDriver constructor.
*
* @param array $config
*/
public function __construct(array $config = [])
{
$this->store = [];
}
2016-09-06 17:03:43 -04:00
/**
* Clean up nothing
*/
public function __destruct()
{
//noop
}
2016-09-05 16:43:37 -04:00
/**
* See if a key currently exists in the cache
*
* @param string $key
* @return bool
*/
public function exists($key)
{
return \array_key_exists($key, $this->store);
}
/**
* Get the value for the selected cache key
*
* @param string $key
* @return mixed
*/
public function get($key)
{
2016-09-06 17:03:43 -04:00
return ($this->exists($key))
? $this->store[$key]
: NULL;
2016-09-05 16:43:37 -04:00
}
/**
* Retrieve a set of values by their cache key
*
* @param string[] $keys
* @return array
*/
public function getMultiple(array $keys = [])
{
$output = [];
foreach($keys as $key)
{
$output[$key] = $this->get($key);
}
return $output;
}
/**
* Set a cached value
*
* @param string $key
* @param mixed $value
* @param int $expires
* @return DriverInterface
*/
public function set($key, $value, $expires = 0)
{
$this->store[$key] = $value;
return $this;
}
/**
* Remove an item from the cache
*
* @param string $key
* @return boolean
*/
public function delete($key)
{
unset($this->store[$key]);
return ( ! array_key_exists($key, $this->store));
}
/**
* Remove multiple items from the cache
*
* @param string[] $keys
* @return boolean
*/
public function deleteMultiple(array $keys = [])
{
$res = TRUE;
foreach($keys as $key)
{
$res = $res && $this->delete($key);
}
return $res;
}
/**
* Empty the cache
*
* @return boolean
*/
public function flush()
{
$this->store = [];
return TRUE;
}
2016-09-06 17:03:43 -04:00
/**
* Set the specified key to expire at the given time
*
* @param string $key
* @param int $expires
* @return boolean
*/
public function expiresAt($key, $expires)
{
//noop
return TRUE;
}
2016-09-05 16:43:37 -04:00
}