rhymix/common/framework/drivers/cache/xcache.php
2016-04-24 16:30:20 +09:00

168 lines
3.2 KiB
PHP

<?php
namespace Rhymix\Framework\Drivers\Cache;
/**
* The XCache cache driver.
*/
class XCache implements \Rhymix\Framework\Drivers\CacheInterface
{
/**
* Set this flag to false to disable cache prefixes.
*/
public $prefix = true;
/**
* The singleton instance is stored here.
*/
protected static $_instance = null;
/**
* Direct invocation of the constructor is not permitted.
*/
protected function __construct()
{
}
/**
* Create a new instance of the current cache driver, using the given settings.
*
* @param array $config
* @return void
*/
public static function getInstance(array $config)
{
if (self::$_instance === null)
{
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Check if the current cache driver is supported on this server.
*
* This method returns true on success and false on failure.
*
* @return bool
*/
public static 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 static 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
* @param bool $force
* @return bool
*/
public function set($key, $value, $ttl = 0, $force = false)
{
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;
}
}