Add Cache class and CacheInterface for drivers

This commit is contained in:
Kijin Sung 2016-04-17 00:36:25 +09:00
parent 30fbf83659
commit 6d5480ecfb
3 changed files with 365 additions and 0 deletions

View file

@ -202,3 +202,8 @@ Rhymix\Framework\Debug::registerErrorHandlers(error_reporting());
*/
$internal_timezone = Rhymix\Framework\DateTime::getTimezoneNameByOffset(config('locale.internal_timezone'));
date_default_timezone_set($internal_timezone);
/**
* Initialize the cache handler.
*/
Rhymix\Framework\Cache::init(Rhymix\Framework\Config::get('cache'));

247
common/framework/cache.php Normal file
View file

@ -0,0 +1,247 @@
<?php
namespace Rhymix\Framework;
/**
* The cache class.
*/
class Cache
{
/**
* The currently enabled cache driver.
*/
protected static $_driver = null;
/**
* The automatically generated cache prefix.
*/
protected static $_prefix = null;
/**
* Initialize the cache system.
*
* @param array $config
* @return void
*/
public static function init($config)
{
if (!$config)
{
return;
}
elseif (!is_array($config))
{
$config = array($config);
}
if (isset($config['driver']))
{
$class_name = '\\Rhymix\\Framework\\Drivers\\Cache\\' . $config['driver'];
}
elseif (preg_match('/^(apc|memcache|redis|wincache|file)/', strval(array_first($config)), $matches))
{
$class_name = '\\Rhymix\\Framework\\Drivers\\Cache\\' . $matches[1] . ($matches[1] === 'memcache' ? 'd' : '');
}
else
{
return;
}
if (class_exists($class_name) && $class_name::isSupported())
{
self::$_driver = new $class_name($config);
}
}
/**
* Get the list of supported cache drivers.
*
* @return array
*/
public static function getSupportedDrivers()
{
$result = array();
foreach (Storage::readDirectory(__DIR__ . '/drivers/cache', false) as $filename)
{
$driver_name = substr($filename, 0, -4);
$class_name = '\Rhymix\Framework\Drivers\Cache\'' . $driver_name;
if ($class_name::isSupported())
{
$result[] = $driver_name;
}
}
return $result;
}
/**
* Get the currently enabled cache driver.
*
* return object|null
*/
public static function getCacheDriver()
{
return self::$_driver;
}
/**
* Get the automatically generated cache prefix for this installation of Rhymix.
*
* return object|null
*/
public static function getCachePrefix()
{
if (self::$_prefix === null)
{
self::$_prefix = substr(sha1(\RX_BASEDIR), 0, 10) . ':' . \RX_VERSION . ':';
}
return self::$_prefix;
}
/**
* Get the value of a key.
*
* This method returns null if the key was not found.
*
* @param string $key
* @param string $group_name (optional)
* @return mixed
*/
public static function get($key, $group_name = null)
{
if (self::$_driver !== null)
{
return self::$_driver::get(self::getRealKey($key, $group_name));
}
else
{
return 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 (optional)
* @param string $group_name (optional)
* @return bool
*/
public static function set($key, $value, $ttl = 0, $group_name = null)
{
if (self::$_driver !== null)
{
return self::$_driver::set(self::getRealKey($key, $group_name), $value, $ttl) ? true : false;
}
else
{
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
* @param string $group_name (optional)
* @return bool
*/
public static function delete($key, $group_name = null)
{
if (self::$_driver !== null)
{
return self::$_driver::delete(self::getRealKey($key, $group_name)) ? true : false;
}
else
{
return false;
}
}
/**
* Check if a key exists.
*
* This method returns true on success and false on failure.
*
* @param string $key
* @param string $group_name (optional)
* @return bool
*/
public static function exists($key, $group_name = null)
{
if (self::$_driver !== null)
{
return self::$_driver::exists(self::getRealKey($key, $group_name)) ? true : false;
}
else
{
return false;
}
}
/**
* Clear a group of keys from the cache.
*
* This method returns true on success and false on failure.
*
* @param string $group_name
* @return bool
*/
public static function clearGroup($group_name)
{
if (self::$_driver !== null)
{
return self::$_driver->incr(self::getRealKey('#GROUP:' . $group_name . ':v'), 1) ? true : false;
}
else
{
return false;
}
}
/**
* Clear all keys from the cache.
*
* This method returns true on success and false on failure.
*
* @return bool
*/
public static function clearAll()
{
if (self::$_driver !== null)
{
return self::$_driver->clear() ? true : false;
}
else
{
return false;
}
}
/**
* Get the actual key used by Rhymix.
*
* @param string $key
* @param string $group_name (optional)
* @return string
*/
public static function getRealKey($key, $group_name = null)
{
if ($group_name)
{
$group_version = intval(self::get('#GROUP:' . $group_name . ':v'));
return self::getCachePrefix() . '#GROUP:' . $group_name . ':' . $group_version . ':' . $key;
}
else
{
return self::getCachePrefix() . $key;
}
}
}

View file

@ -0,0 +1,113 @@
<?php
namespace Rhymix\Framework\Drivers;
/**
* The cache driver interface.
*/
interface 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();
/**
* Validate cache settings.
*
* This method returns true on success and false on failure.
*
* @param mixed $config
* @return bool
*/
public function validateSettings($config);
/**
* 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);
/**
* 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);
/**
* 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);
/**
* Check if a key exists.
*
* This method returns true on success and false on failure.
*
* @param string $key
* @return bool
*/
public function 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);
/**
* 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);
/**
* Clear all keys from the cache.
*
* This method returns true on success and false on failure.
*
* @return bool
*/
public function clear();
}