Add cache drivers for APC, file, Memcached, Redis, WinCache and XCache

This commit is contained in:
Kijin Sung 2016-04-17 12:49:59 +09:00
parent df641e8bf9
commit 6618593a11
6 changed files with 1127 additions and 0 deletions

156
common/framework/drivers/cache/apc.php vendored Normal file
View file

@ -0,0 +1,156 @@
<?php
namespace Rhymix\Framework\Drivers\Cache;
/**
* The APC cache driver.
*/
class APC implements \Rhymix\Framework\Drivers\CacheInterface
{
/**
* Create a new instance of the current cache driver, using the given settings.
*
* @param array $config
* @return void
*/
public function __construct(array $config)
{
}
/**
* Check if the current cache driver is supported on this server.
*
* This method returns true on success and false on failure.
*
* @return bool
*/
public function isSupported()
{
return function_exists('apc_exists');
}
/**
* Validate cache settings.
*
* This method returns true on success and false on failure.
*
* @param mixed $config
* @return bool
*/
public function validateSettings($config)
{
return true;
}
/**
* Get the value of a key.
*
* This method returns null if the key was not found.
*
* @param string $key
* @return mixed
*/
public function get($key)
{
$value = apc_fetch($key);
return $value === false ? null : $value;
}
/**
* Set the value to a key.
*
* This method returns true on success and false on failure.
* $ttl is measured in seconds. If it is zero, the key should not expire.
*
* @param string $key
* @param mixed $value
* @param int $ttl
* @return bool
*/
public function set($key, $value, $ttl)
{
return apc_store($key, $value, $ttl);
}
/**
* Delete a key.
*
* This method returns true on success and false on failure.
* If the key does not exist, it should return false.
*
* @param string $key
* @return bool
*/
public function delete($key)
{
return apc_delete($key);
}
/**
* Check if a key exists.
*
* This method returns true on success and false on failure.
*
* @param string $key
* @return bool
*/
public function exists($key)
{
return apc_exists($key);
}
/**
* Increase the value of a key by $amount.
*
* If the key does not exist, this method assumes that the current value is zero.
* This method returns the new value.
*
* @param string $key
* @param int $amount
* @return int
*/
public function incr($key, $amount)
{
$result = apc_inc($key, $amount);
if ($result === false)
{
apc_store($key, $amount);
$result = $amount;
}
return $result;
}
/**
* Decrease the value of a key by $amount.
*
* If the key does not exist, this method assumes that the current value is zero.
* This method returns the new value.
*
* @param string $key
* @param int $amount
* @return int
*/
public function decr($key, $amount)
{
$result = apc_dec($key, $amount);
if ($result === false)
{
apc_store($key, 0 - $amount);
$result = 0 - $amount;
}
return $result;
}
/**
* Clear all keys from the cache.
*
* This method returns true on success and false on failure.
*
* @return bool
*/
public function clear()
{
return apc_clear_cache('user');
}
}

183
common/framework/drivers/cache/file.php vendored Normal file
View file

@ -0,0 +1,183 @@
<?php
namespace Rhymix\Framework\Drivers\Cache;
use Rhymix\Framework\Storage;
/**
* The file cache driver.
*/
class File implements \Rhymix\Framework\Drivers\CacheInterface
{
/**
* The cache directory.
*/
protected $_dir;
/**
* Create a new instance of the current cache driver, using the given settings.
*
* @param array $config
* @return void
*/
public function __construct(array $config)
{
$this->_dir = \RX_BASEDIR . 'files/cache/store';
if (!Storage::isDirectory($this->_dir))
{
Storage::createDirectory($this->_dir);
}
}
/**
* Check if the current cache driver is supported on this server.
*
* This method returns true on success and false on failure.
*
* @return bool
*/
public function isSupported()
{
return true;
}
/**
* Validate cache settings.
*
* This method returns true on success and false on failure.
*
* @param mixed $config
* @return bool
*/
public function validateSettings($config)
{
return true;
}
/**
* Get the value of a key.
*
* This method returns null if the key was not found.
*
* @param string $key
* @return mixed
*/
public function get($key)
{
$filename = $this->_getFilename($key);
$data = Storage::readPHPData($filename);
if ($data === false)
{
return null;
}
elseif (!is_array($data) || count($data) < 2 || ($data[0] > 0 && $data[0] < time()))
{
Storage::delete($filename);
return null;
}
else
{
return $data[1];
}
}
/**
* Set the value to a key.
*
* This method returns true on success and false on failure.
* $ttl is measured in seconds. If it is zero, the key should not expire.
*
* @param string $key
* @param mixed $value
* @param int $ttl
* @return bool
*/
public function set($key, $value, $ttl)
{
return Storage::writePHPData($this->_getFilename($key), array($ttl ? (time() + $ttl) : 0, $value));
}
/**
* Delete a key.
*
* This method returns true on success and false on failure.
* If the key does not exist, it should return false.
*
* @param string $key
* @return bool
*/
public function delete($key)
{
return Storage::delete($this->_getFilename($key));
}
/**
* Check if a key exists.
*
* This method returns true on success and false on failure.
*
* @param string $key
* @return bool
*/
public function exists($key)
{
return $this->get($key) !== null;
}
/**
* Increase the value of a key by $amount.
*
* If the key does not exist, this method assumes that the current value is zero.
* This method returns the new value.
*
* @param string $key
* @param int $amount
* @return int
*/
public function incr($key, $amount)
{
$value = intval($this->get($key));
$success = $this->set($key, $value + $amount);
return $success ? ($value + $amount) : false;
}
/**
* Decrease the value of a key by $amount.
*
* If the key does not exist, this method assumes that the current value is zero.
* This method returns the new value.
*
* @param string $key
* @param int $amount
* @return int
*/
public function decr($key, $amount)
{
return $this->incr($key, 0 - $amount);
}
/**
* Clear all keys from the cache.
*
* This method returns true on success and false on failure.
*
* @return bool
*/
public function clear()
{
Storage::deleteDirectory($this->_dir);
}
/**
* Get the filename to store a key.
*
* @param string $key
* @return string
*/
protected function _getFilename($key)
{
$hash = sha1($key);
return $this->_dir . '/' . substr($hash, 0, 2) . '/' . substr($hash, 2, 2) . '/' . $hash . '.php';
}
}

View file

@ -0,0 +1,228 @@
<?php
namespace Rhymix\Framework\Drivers\Cache;
/**
* The Memcached cache driver.
*/
class Memcached implements \Rhymix\Framework\Drivers\CacheInterface
{
/**
* The Memcached connection is stored here.
*/
protected $_conn = null;
protected $_ext = null;
/**
* Create a new instance of the current cache driver, using the given settings.
*
* @param array $config
* @return void
*/
public function __construct(array $config)
{
if (class_exists('\\Memcached'))
{
$this->_conn = new \Memcached;
$this->_ext = 'Memcached';
}
elseif (class_exists('\\Memcache'))
{
$this->_conn = new \Memcache;
$this->_ext = 'Memcache';
}
else
{
return;
}
foreach ($config as $url)
{
$info = parse_url($url);
if (isset($info['host']) && isset($info['port']))
{
$this->_conn->addServer($info['host'], $info['port']);
}
}
}
/**
* Check if the current cache driver is supported on this server.
*
* This method returns true on success and false on failure.
*
* @return bool
*/
public function isSupported()
{
return class_exists('\\Memcached') || class_exists('\\Memcache');
}
/**
* Validate cache settings.
*
* This method returns true on success and false on failure.
*
* @param mixed $config
* @return bool
*/
public function validateSettings($config)
{
if (class_exists('\\Memcached'))
{
$conn = new \Memcached;
$ext = 'Memcached';
}
elseif (class_exists('\\Memcache'))
{
$conn = new \Memcache;
$ext = 'Memcache';
}
else
{
return false;
}
foreach ($config as $url)
{
$info = parse_url($url);
if (isset($info['host']) && isset($info['port']))
{
$conn->addServer($info['host'], $info['port']);
}
}
for ($i = 0; $i < 5; $i++)
{
$key = 'rhymix:test:' . md5($i);
$status = ($ext === 'Memcached') ? $conn->set($key, $i, 2) : $conn->set($key, $i, 0, 2);
if (!$status) return false;
}
return true;
}
/**
* Get the value of a key.
*
* This method returns null if the key was not found.
*
* @param string $key
* @return mixed
*/
public function get($key)
{
$value = $this->_conn->get($key);
if ($value === false)
{
return null;
}
else
{
return $value;
}
}
/**
* Set the value to a key.
*
* This method returns true on success and false on failure.
* $ttl is measured in seconds. If it is zero, the key should not expire.
*
* @param string $key
* @param mixed $value
* @param int $ttl
* @return bool
*/
public function set($key, $value, $ttl)
{
if ($this->_ext === 'Memcached')
{
return $this->_conn->set($key, $value, $ttl);
}
else
{
return $this->_conn->set($key, $value, MEMCACHE_COMPRESSED, $ttl);
}
}
/**
* Delete a key.
*
* This method returns true on success and false on failure.
* If the key does not exist, it should return false.
*
* @param string $key
* @return bool
*/
public function delete($key)
{
return $this->_conn->delete($key);
}
/**
* Check if a key exists.
*
* This method returns true on success and false on failure.
*
* @param string $key
* @return bool
*/
public function exists($key)
{
return $this->_conn->get($key) !== false;
}
/**
* Increase the value of a key by $amount.
*
* If the key does not exist, this method assumes that the current value is zero.
* This method returns the new value.
*
* @param string $key
* @param int $amount
* @return int
*/
public function incr($key, $amount)
{
$result = $this->_conn->increment($key, $amount);
if ($result === false)
{
$this->set($key, $amount);
$result = $amount;
}
return $result;
}
/**
* Decrease the value of a key by $amount.
*
* If the key does not exist, this method assumes that the current value is zero.
* This method returns the new value.
*
* @param string $key
* @param int $amount
* @return int
*/
public function decr($key, $amount)
{
$result = $this->_conn->decrement($key, $amount);
if ($result === false)
{
$this->set($key, 0 - $amount);
$result = 0 - $amount;
}
return $result;
}
/**
* Clear all keys from the cache.
*
* This method returns true on success and false on failure.
*
* @return bool
*/
public function clear()
{
return $this->_conn->flush();
}
}

259
common/framework/drivers/cache/redis.php vendored Normal file
View file

@ -0,0 +1,259 @@
<?php
namespace Rhymix\Framework\Drivers\Cache;
/**
* The Redis cache driver.
*/
class Redis implements \Rhymix\Framework\Drivers\CacheInterface
{
/**
* The Redis connection is stored here.
*/
protected $_conn = null;
/**
* Create a new instance of the current cache driver, using the given settings.
*
* @param array $config
* @return void
*/
public function __construct(array $config)
{
try
{
$this->_conn = new \Redis;
foreach ($config as $url)
{
$info = parse_url($url);
if (isset($info['host']) && isset($info['port']))
{
$this->_conn->connect($info['host'], $info['port'], 0.15);
if(isset($info['user']) || isset($info['pass']))
{
$this->_conn->auth(isset($info['user']) ? $info['user'] : $info['pass']);
}
if(isset($info['path']) && $dbnum = intval(substr($info['path'], 1)))
{
$this->_conn->select($dbnum);
}
break;
}
}
$this->_conn = null;
}
catch (\RedisException $e)
{
$this->_conn = null;
}
}
/**
* Check if the current cache driver is supported on this server.
*
* This method returns true on success and false on failure.
*
* @return bool
*/
public function isSupported()
{
return class_exists('\\Redis');
}
/**
* Validate cache settings.
*
* This method returns true on success and false on failure.
*
* @param mixed $config
* @return bool
*/
public function validateSettings($config)
{
try
{
$conn = new \Redis;
foreach ($config as $url)
{
$info = parse_url($url);
if (isset($info['host']) && isset($info['port']))
{
$conn->connect($info['host'], $info['port'], 0.15);
if(isset($info['user']) || isset($info['pass']))
{
$conn->auth(isset($info['user']) ? $info['user'] : $info['pass']);
}
if(isset($info['path']) && $dbnum = intval(substr($info['path'], 1)))
{
$conn->select($dbnum);
}
return true;
}
}
return false;
}
catch (\RedisException $e)
{
return false;
}
}
/**
* Get the value of a key.
*
* This method returns null if the key was not found.
*
* @param string $key
* @return mixed
*/
public function get($key)
{
try
{
$value = $this->_conn->get($key);
}
catch (\RedisException $e)
{
return null;
}
if ($value === false)
{
return null;
}
$value = unserialize($value);
if ($value === false)
{
return null;
}
return $value;
}
/**
* Set the value to a key.
*
* This method returns true on success and false on failure.
* $ttl is measured in seconds. If it is zero, the key should not expire.
*
* @param string $key
* @param mixed $value
* @param int $ttl
* @return bool
*/
public function set($key, $value, $ttl)
{
try
{
return $this->_conn->setex($key, $ttl, serialize($value)) ? true : false;
}
catch (\RedisException $e)
{
return false;
}
}
/**
* Delete a key.
*
* This method returns true on success and false on failure.
* If the key does not exist, it should return false.
*
* @param string $key
* @return bool
*/
public function delete($key)
{
try
{
return $this->_conn->del($key) ? true : false;
}
catch (\RedisException $e)
{
return false;
}
}
/**
* Check if a key exists.
*
* This method returns true on success and false on failure.
*
* @param string $key
* @return bool
*/
public function exists($key)
{
try
{
return $this->_conn->exists($key);
}
catch (\RedisException $e)
{
return false;
}
}
/**
* Increase the value of a key by $amount.
*
* If the key does not exist, this method assumes that the current value is zero.
* This method returns the new value.
*
* @param string $key
* @param int $amount
* @return int
*/
public function incr($key, $amount)
{
try
{
return $this->_conn->incrBy($key, $amount);
}
catch (\RedisException $e)
{
return false;
}
}
/**
* Decrease the value of a key by $amount.
*
* If the key does not exist, this method assumes that the current value is zero.
* This method returns the new value.
*
* @param string $key
* @param int $amount
* @return int
*/
public function decr($key, $amount)
{
try
{
return $this->_conn->decrBy($key, $amount);
}
catch (\RedisException $e)
{
return false;
}
}
/**
* Clear all keys from the cache.
*
* This method returns true on success and false on failure.
*
* @return bool
*/
public function clear()
{
try
{
return $this->_conn->flushDB() ? true : false;
}
catch (\RedisException $e)
{
return false;
}
}
}

View file

@ -0,0 +1,156 @@
<?php
namespace Rhymix\Framework\Drivers\Cache;
/**
* The WinCache cache driver.
*/
class WinCache implements \Rhymix\Framework\Drivers\CacheInterface
{
/**
* Create a new instance of the current cache driver, using the given settings.
*
* @param array $config
* @return void
*/
public function __construct(array $config)
{
}
/**
* Check if the current cache driver is supported on this server.
*
* This method returns true on success and false on failure.
*
* @return bool
*/
public function isSupported()
{
return function_exists('wincache_ucache_get');
}
/**
* Validate cache settings.
*
* This method returns true on success and false on failure.
*
* @param mixed $config
* @return bool
*/
public function validateSettings($config)
{
return true;
}
/**
* Get the value of a key.
*
* This method returns null if the key was not found.
*
* @param string $key
* @return mixed
*/
public function get($key)
{
$value = wincache_ucache_get($key, $success);
return $success ? $value : null;
}
/**
* Set the value to a key.
*
* This method returns true on success and false on failure.
* $ttl is measured in seconds. If it is zero, the key should not expire.
*
* @param string $key
* @param mixed $value
* @param int $ttl
* @return bool
*/
public function set($key, $value, $ttl)
{
return wincache_ucache_set($key, $value, $ttl);
}
/**
* Delete a key.
*
* This method returns true on success and false on failure.
* If the key does not exist, it should return false.
*
* @param string $key
* @return bool
*/
public function delete($key)
{
return wincache_ucache_delete($key);
}
/**
* Check if a key exists.
*
* This method returns true on success and false on failure.
*
* @param string $key
* @return bool
*/
public function exists($key)
{
return wincache_ucache_exists($key);
}
/**
* Increase the value of a key by $amount.
*
* If the key does not exist, this method assumes that the current value is zero.
* This method returns the new value.
*
* @param string $key
* @param int $amount
* @return int
*/
public function incr($key, $amount)
{
$result = wincache_ucache_inc($key, $amount);
if ($result === false)
{
wincache_ucache_set($key, $amount);
$result = $amount;
}
return $result;
}
/**
* Decrease the value of a key by $amount.
*
* If the key does not exist, this method assumes that the current value is zero.
* This method returns the new value.
*
* @param string $key
* @param int $amount
* @return int
*/
public function decr($key, $amount)
{
$result = wincache_ucache_dec($key, $amount);
if ($result === false)
{
wincache_ucache_set($key, 0 - $amount);
$result = 0 - $amount;
}
return $result;
}
/**
* Clear all keys from the cache.
*
* This method returns true on success and false on failure.
*
* @return bool
*/
public function clear()
{
return wincache_ucache_clear();
}
}

View file

@ -0,0 +1,145 @@
<?php
namespace Rhymix\Framework\Drivers\Cache;
/**
* The XCache cache driver.
*/
class XCache implements \Rhymix\Framework\Drivers\CacheInterface
{
/**
* Create a new instance of the current cache driver, using the given settings.
*
* @param array $config
* @return void
*/
public function __construct(array $config)
{
}
/**
* Check if the current cache driver is supported on this server.
*
* This method returns true on success and false on failure.
*
* @return bool
*/
public function isSupported()
{
return function_exists('xcache_get');
}
/**
* Validate cache settings.
*
* This method returns true on success and false on failure.
*
* @param mixed $config
* @return bool
*/
public function validateSettings($config)
{
return true;
}
/**
* Get the value of a key.
*
* This method returns null if the key was not found.
*
* @param string $key
* @return mixed
*/
public function get($key)
{
$value = xcache_get($key);
return $value === false ? null : $value;
}
/**
* Set the value to a key.
*
* This method returns true on success and false on failure.
* $ttl is measured in seconds. If it is zero, the key should not expire.
*
* @param string $key
* @param mixed $value
* @param int $ttl
* @return bool
*/
public function set($key, $value, $ttl)
{
return xcache_set($key, $value, $ttl);
}
/**
* Delete a key.
*
* This method returns true on success and false on failure.
* If the key does not exist, it should return false.
*
* @param string $key
* @return bool
*/
public function delete($key)
{
return xcache_unset($key);
}
/**
* Check if a key exists.
*
* This method returns true on success and false on failure.
*
* @param string $key
* @return bool
*/
public function exists($key)
{
return xcache_isset($key);
}
/**
* Increase the value of a key by $amount.
*
* If the key does not exist, this method assumes that the current value is zero.
* This method returns the new value.
*
* @param string $key
* @param int $amount
* @return int
*/
public function incr($key, $amount)
{
return xcache_inc($key, $amount);
}
/**
* Decrease the value of a key by $amount.
*
* If the key does not exist, this method assumes that the current value is zero.
* This method returns the new value.
*
* @param string $key
* @param int $amount
* @return int
*/
public function decr($key, $amount)
{
return xcache_dec($key, $amount);
}
/**
* Clear all keys from the cache.
*
* This method returns true on success and false on failure.
*
* @return bool
*/
public function clear()
{
xcache_clear_cache(XC_TYPE_VAR);
return true;
}
}