Change default cache driver to 'dummy' with minimal caching

This commit is contained in:
Kijin Sung 2016-04-22 23:26:04 +09:00
parent d4e2163e2b
commit ba10c91d0e
15 changed files with 95 additions and 120 deletions

View file

@ -64,7 +64,7 @@ class Cache
}
else
{
self::$_driver = new Drivers\Cache\File(array());
self::$_driver = new Drivers\Cache\Dummy(array());
}
if (self::$_driver->prefix)
@ -129,13 +129,34 @@ class Cache
/**
* Get the automatically generated cache prefix for this installation of Rhymix.
*
* return object|null
* @return object|null
*/
public static function getCachePrefix()
{
return self::$_prefix;
}
/**
* Get the default TTL.
*
* @return int
*/
public static function getDefaultTTL()
{
return self::$_ttl;
}
/**
* Set the default TTL.
*
* @param int $ttl
* @return void
*/
public static function setDefaultTTL($ttl)
{
self::$_ttl = $ttl;
}
/**
* Get the value of a key.
*
@ -160,26 +181,29 @@ class Cache
* 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.
* $ttl is measured in seconds. If it is not given, the default TTL is used.
* $force is used to cache essential data when using the default driver.
*
* @param string $key
* @param mixed $value
* @param int $ttl (optional)
* @param bool $force (optional)
* @return bool
*/
public static function set($key, $value, $ttl = null)
public static function set($key, $value, $ttl = 0, $force = false)
{
if (self::$_driver !== null)
{
$ttl = intval($ttl);
if ($ttl >= (3600 * 24 * 30))
{
$ttl = min(3600 * 24 * 30, max(0, $ttl - time()));
}
if ($ttl === null)
if ($ttl === 0)
{
$ttl = self::$_ttl;
}
return self::$_driver->set(self::getRealKey($key), $value, intval($ttl)) ? true : false;
return self::$_driver->set(self::getRealKey($key), $value, $ttl, $force) ? true : false;
}
else
{

View file

@ -71,9 +71,10 @@ class APC implements \Rhymix\Framework\Drivers\CacheInterface
* @param string $key
* @param mixed $value
* @param int $ttl
* @param bool $force
* @return bool
*/
public function set($key, $value, $ttl)
public function set($key, $value, $ttl = 0, $force = false)
{
return apc_store($key, $value, $ttl);
}

View file

@ -5,7 +5,7 @@ namespace Rhymix\Framework\Drivers\Cache;
/**
* The dummy cache driver.
*/
class Dummy implements \Rhymix\Framework\Drivers\CacheInterface
class Dummy extends File implements \Rhymix\Framework\Drivers\CacheInterface
{
/**
* Set this flag to false to disable cache prefixes.
@ -17,42 +17,6 @@ class Dummy implements \Rhymix\Framework\Drivers\CacheInterface
*/
public $data = array();
/**
* 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 true;
}
/**
* 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.
*
@ -63,7 +27,12 @@ class Dummy implements \Rhymix\Framework\Drivers\CacheInterface
*/
public function get($key)
{
if (isset($this->data[$key]))
$value = parent::get($key);
if ($value !== null)
{
return $value;
}
elseif (isset($this->data[$key]))
{
if ($this->data[$key][0] > 0 && $this->data[$key][0] < time())
{
@ -87,12 +56,20 @@ class Dummy implements \Rhymix\Framework\Drivers\CacheInterface
* @param string $key
* @param mixed $value
* @param int $ttl
* @param bool $force
* @return bool
*/
public function set($key, $value, $ttl)
public function set($key, $value, $ttl = 0, $force = false)
{
$this->data[$key] = array($ttl ? (time() + $ttl) : 0, $value);
return true;
if ($force)
{
return parent::set($key, $value, $ttl, $force);
}
else
{
$this->data[$key] = array($ttl ? (time() + $ttl) : 0, $value);
return true;
}
}
/**
@ -106,7 +83,11 @@ class Dummy implements \Rhymix\Framework\Drivers\CacheInterface
*/
public function delete($key)
{
if (isset($this->data[$key]))
if (parent::delete($key))
{
return true;
}
elseif (isset($this->data[$key]))
{
unset($this->data[$key]);
return true;
@ -127,46 +108,7 @@ class Dummy implements \Rhymix\Framework\Drivers\CacheInterface
*/
public function exists($key)
{
return isset($this->data[$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)
{
if (isset($this->data[$key]))
{
$this->data[$key][1] += $amount;
return $this->data[$key][1];
}
else
{
$this->set($key, $amount, 0);
return $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 $this->incr($key, 0 - $amount);
return parent::exists($key) || isset($this->data[$key]);
}
/**
@ -178,6 +120,7 @@ class Dummy implements \Rhymix\Framework\Drivers\CacheInterface
*/
public function clear()
{
parent::clear();
$this->data = array();
return true;
}

View file

@ -96,9 +96,10 @@ class File implements \Rhymix\Framework\Drivers\CacheInterface
* @param string $key
* @param mixed $value
* @param int $ttl
* @param bool $force
* @return bool
*/
public function set($key, $value, $ttl)
public function set($key, $value, $ttl = 0, $force = false)
{
return Storage::writePHPData($this->_getFilename($key), array($ttl ? (time() + $ttl) : 0, $value), $key);
}

View file

@ -136,9 +136,10 @@ class Memcached implements \Rhymix\Framework\Drivers\CacheInterface
* @param string $key
* @param mixed $value
* @param int $ttl
* @param bool $force
* @return bool
*/
public function set($key, $value, $ttl)
public function set($key, $value, $ttl = 0, $force = false)
{
if ($this->_ext === 'Memcached')
{

View file

@ -144,9 +144,10 @@ class Redis implements \Rhymix\Framework\Drivers\CacheInterface
* @param string $key
* @param mixed $value
* @param int $ttl
* @param bool $force
* @return bool
*/
public function set($key, $value, $ttl)
public function set($key, $value, $ttl = 0, $force = false)
{
try
{

View file

@ -130,9 +130,10 @@ class SQLite implements \Rhymix\Framework\Drivers\CacheInterface
* @param string $key
* @param mixed $value
* @param int $ttl
* @param bool $force
* @return bool
*/
public function set($key, $value, $ttl)
public function set($key, $value, $ttl = 0, $force = false)
{
$table = 'cache_' . (crc32($key) % 32);
$stmt = $this->_dbh->prepare('INSERT OR REPLACE INTO ' . $table . ' (k, v, exp) VALUES (:key, :val, :exp)');

View file

@ -71,9 +71,10 @@ class WinCache implements \Rhymix\Framework\Drivers\CacheInterface
* @param string $key
* @param mixed $value
* @param int $ttl
* @param bool $force
* @return bool
*/
public function set($key, $value, $ttl)
public function set($key, $value, $ttl = 0, $force = false)
{
return wincache_ucache_set($key, $value, $ttl);
}

View file

@ -71,9 +71,10 @@ class XCache implements \Rhymix\Framework\Drivers\CacheInterface
* @param string $key
* @param mixed $value
* @param int $ttl
* @param bool $force
* @return bool
*/
public function set($key, $value, $ttl)
public function set($key, $value, $ttl = 0, $force = false)
{
return xcache_set($key, $value, $ttl);
}

View file

@ -53,9 +53,10 @@ interface CacheInterface
* @param string $key
* @param mixed $value
* @param int $ttl
* @param bool $force
* @return bool
*/
public function set($key, $value, $ttl);
public function set($key, $value, $ttl = 0, $force = false);
/**
* Delete a key.