Migrate cache configuration format, keeping backward compatibility

This commit is contained in:
Kijin Sung 2016-04-17 19:52:31 +09:00
parent 194cf3fa70
commit 050a507707
8 changed files with 70 additions and 28 deletions

View file

@ -12,6 +12,11 @@ class Cache
*/
protected static $_driver = null;
/**
* The default TTL.
*/
protected static $_ttl = 86400;
/**
* The automatically generated cache prefix.
*/
@ -30,9 +35,14 @@ class Cache
$config = array($config);
}
if (isset($config['driver']))
if (isset($config['type']))
{
$class_name = '\\Rhymix\\Framework\\Drivers\\Cache\\' . $config['driver'];
$class_name = '\\Rhymix\\Framework\\Drivers\\Cache\\' . $config['type'];
if (isset($config['ttl']))
{
self::$_ttl = intval($config['ttl']);
}
$config = isset($config['servers']) ? $config['servers'] : array();
}
elseif (preg_match('/^(apc|dummy|file|memcache|redis|sqlite|wincache|xcache)/', strval(array_first($config)), $matches))
{
@ -148,7 +158,7 @@ class Cache
* @param string $group_name (optional)
* @return bool
*/
public static function set($key, $value, $ttl = 0, $group_name = null)
public static function set($key, $value, $ttl = null, $group_name = null)
{
if (self::$_driver !== null)
{
@ -156,6 +166,10 @@ class Cache
{
$ttl = min(3600 * 24 * 30, max(0, $ttl - time()));
}
if ($ttl === null)
{
$ttl = self::$_ttl;
}
return self::$_driver->set(self::getRealKey($key, $group_name), $value, intval($ttl)) ? true : false;
}
else