Moving private members/methods to protected.

Changing Cache methods/members to static.
Removing singleton access to Cache.
This commit is contained in:
Mark Story 2010-04-14 23:56:44 -04:00
parent 17949ff9fc
commit 23b75f4ed2

View file

@ -33,47 +33,29 @@ class Cache {
* These settings are used to reset the engines after temporary modification. * These settings are used to reset the engines after temporary modification.
* *
* @var array * @var array
* @access private
*/ */
private $__config = array(); protected static $_config = array();
/** /**
* Holds name of the current configuration name being used. * Holds name of the current configuration name being used.
* *
* @var array * @var array
* @access private
*/ */
private $__name = 'default'; protected static $_name = 'default';
/** /**
* Whether to reset the settings with the next call to Cache::set(); * Whether to reset the settings with the next call to Cache::set();
* *
* @var array * @var array
* @access private
*/ */
private $__reset = false; protected static $_reset = false;
/** /**
* Engine instances keyed by configuration name. * Engine instances keyed by configuration name.
* *
* @var array * @var array
*/ */
protected $_engines = array(); protected static $_engines = array();
/**
* Returns a singleton instance
*
* @return object
* @access public
* @static
*/
function &getInstance() {
static $instance = array();
if (!$instance) {
$instance[0] =& new Cache();
}
return $instance[0];
}
/** /**
* Set the cache configuration to use. config() can * Set the cache configuration to use. config() can
@ -93,40 +75,37 @@ class Cache {
* @param string $name Name of the configuration * @param string $name Name of the configuration
* @param array $settings Optional associative array of settings passed to the engine * @param array $settings Optional associative array of settings passed to the engine
* @return array(engine, settings) on success, false on failure * @return array(engine, settings) on success, false on failure
* @access public
* @static
*/ */
function config($name = null, $settings = array()) { public static function config($name = null, $settings = array()) {
$self =& Cache::getInstance();
if (is_array($name)) { if (is_array($name)) {
$settings = $name; $settings = $name;
} }
if ($name === null || !is_string($name)) { if ($name === null || !is_string($name)) {
$name = $self->__name; $name = self::$_name;
} }
$current = array(); $current = array();
if (isset($self->__config[$name])) { if (isset(self::$_config[$name])) {
$current = $self->__config[$name]; $current = self::$_config[$name];
} }
if (!empty($settings)) { if (!empty($settings)) {
$self->__config[$name] = array_merge($current, $settings); self::$_config[$name] = array_merge($current, $settings);
} }
if (empty($self->__config[$name]['engine'])) { if (empty(self::$_config[$name]['engine'])) {
return false; return false;
} }
$engine = $self->__config[$name]['engine']; $engine = self::$_config[$name]['engine'];
$self->__name = $name; self::$_name = $name;
if (!isset($self->_engines[$name])) { if (!isset(self::$_engines[$name])) {
$self->_buildEngine($name); self::_buildEngine($name);
$settings = $self->__config[$name] = $self->settings($name); $settings = self::$_config[$name] = self::settings($name);
} elseif ($settings = $self->set($self->__config[$name])) { } elseif ($settings = self::set(self::$_config[$name])) {
$self->__config[$name] = $settings; self::$_config[$name] = $settings;
} }
return compact('engine', 'settings'); return compact('engine', 'settings');
} }
@ -137,19 +116,19 @@ class Cache {
* @param string $name Name of the config array that needs an engine instance built * @param string $name Name of the config array that needs an engine instance built
* @return void * @return void
*/ */
protected function _buildEngine($name) { protected static function _buildEngine($name) {
$config = $this->__config[$name]; $config = self::$_config[$name];
list($plugin, $class) = pluginSplit($config['engine']); list($plugin, $class) = pluginSplit($config['engine']);
$cacheClass = $class . 'Engine'; $cacheClass = $class . 'Engine';
if (!class_exists($cacheClass) && $this->__loadEngine($class, $plugin) === false) { if (!class_exists($cacheClass) && self::_loadEngine($class, $plugin) === false) {
return false; return false;
} }
$cacheClass = $class . 'Engine'; $cacheClass = $class . 'Engine';
$this->_engines[$name] =& new $cacheClass(); self::$_engines[$name] = new $cacheClass();
if ($this->_engines[$name]->init($config)) { if (self::$_engines[$name]->init($config)) {
if (time() % $this->_engines[$name]->settings['probability'] === 0) { if (time() % self::$_engines[$name]->settings['probability'] === 0) {
$this->_engines[$name]->gc(); self::$_engines[$name]->gc();
} }
return true; return true;
} }
@ -161,9 +140,8 @@ class Cache {
* *
* @return array Array of configured Cache config names. * @return array Array of configured Cache config names.
*/ */
function configured() { public static function configured() {
$self =& Cache::getInstance(); return array_keys(self::$_config);
return array_keys($self->__config);
} }
/** /**
@ -174,13 +152,12 @@ class Cache {
* @param string $name A currently configured cache config you wish to remove. * @param string $name A currently configured cache config you wish to remove.
* @return boolen success of the removal, returns false when the config does not exist. * @return boolen success of the removal, returns false when the config does not exist.
*/ */
function drop($name) { public static function drop($name) {
$self =& Cache::getInstance(); if (!isset(self::$_config[$name])) {
if (!isset($self->__config[$name])) {
return false; return false;
} }
unset($self->__config[$name]); unset(self::$_config[$name]);
unset($self->_engines[$name]); unset(self::$_engines[$name]);
return true; return true;
} }
@ -189,9 +166,8 @@ class Cache {
* *
* @param $name Name of the engine (without 'Engine') * @param $name Name of the engine (without 'Engine')
* @return mixed $engine object or null * @return mixed $engine object or null
* @access private
*/ */
function __loadEngine($name, $plugin = null) { protected static function _loadEngine($name, $plugin = null) {
if ($plugin) { if ($plugin) {
return App::import('Lib', $plugin . '.cache' . DS . $name, false); return App::import('Lib', $plugin . '.cache' . DS . $name, false);
} else { } else {
@ -212,35 +188,32 @@ class Cache {
* @param mixed $settings Optional string for simple name-value pair or array * @param mixed $settings Optional string for simple name-value pair or array
* @param string $value Optional for a simple name-value pair * @param string $value Optional for a simple name-value pair
* @return array Array of settings. * @return array Array of settings.
* @access public
* @static
*/ */
function set($settings = array(), $value = null) { public static function set($settings = array(), $value = null) {
$self =& Cache::getInstance(); if (!isset(self::$_config[self::$_name]) || !isset(self::$_engines[self::$_name])) {
if (!isset($self->__config[$self->__name]) || !isset($self->_engines[$self->__name])) {
return false; return false;
} }
$name = $self->__name; $name = self::$_name;
if (!empty($settings)) { if (!empty($settings)) {
$self->__reset = true; self::$_reset = true;
} }
if ($self->__reset === true) { if (self::$_reset === true) {
if (empty($settings)) { if (empty($settings)) {
$self->__reset = false; self::$_reset = false;
$settings = $self->__config[$name]; $settings = self::$_config[$name];
} else { } else {
if (is_string($settings) && $value !== null) { if (is_string($settings) && $value !== null) {
$settings = array($settings => $value); $settings = array($settings => $value);
} }
$settings = array_merge($self->__config[$name], $settings); $settings = array_merge(self::$_config[$name], $settings);
if (isset($settings['duration']) && !is_numeric($settings['duration'])) { if (isset($settings['duration']) && !is_numeric($settings['duration'])) {
$settings['duration'] = strtotime($settings['duration']) - time(); $settings['duration'] = strtotime($settings['duration']) - time();
} }
} }
$self->_engines[$name]->settings = $settings; self::$_engines[$name]->settings = $settings;
} }
return $self->settings($name); return self::settings($name);
} }
/** /**
@ -249,12 +222,9 @@ class Cache {
* Permanently remove all expired and deleted data * Permanently remove all expired and deleted data
* *
* @return void * @return void
* @access public
* @static
*/ */
function gc() { public static function gc() {
$self =& Cache::getInstance(); self::$_engines[self::$_name]->gc();
$self->_engines[$self->__name]->gc();
} }
/** /**
@ -276,31 +246,27 @@ class Cache {
* @param mixed $value Data to be cached - anything except a resource * @param mixed $value Data to be cached - anything except a resource
* @param string $config Optional string configuration name to write to. * @param string $config Optional string configuration name to write to.
* @return boolean True if the data was successfully cached, false on failure * @return boolean True if the data was successfully cached, false on failure
* @access public
* @static
*/ */
function write($key, $value, $config = null) { public static function write($key, $value, $config = null) {
$self =& Cache::getInstance();
if (!$config) { if (!$config) {
$config = $self->__name; $config = self::$_name;
} }
$settings = $self->settings($config); $settings = self::settings($config);
if (empty($settings)) { if (empty($settings)) {
return null; return null;
} }
if (!$self->isInitialized($config)) { if (!self::isInitialized($config)) {
return false; return false;
} }
$key = $self->_engines[$config]->key($key); $key = self::$_engines[$config]->key($key);
if (!$key || is_resource($value) || $settings['duration'] < 1) { if (!$key || is_resource($value) || $settings['duration'] < 1) {
return false; return false;
} }
$success = $self->_engines[$config]->write($settings['prefix'] . $key, $value, $settings['duration']); $success = self::$_engines[$config]->write($settings['prefix'] . $key, $value, $settings['duration']);
$self->set(); self::set();
return $success; return $success;
} }
@ -322,31 +288,27 @@ class Cache {
* @param string $key Identifier for the data * @param string $key Identifier for the data
* @param string $config optional name of the configuration to use. * @param string $config optional name of the configuration to use.
* @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
* @access public
* @static
*/ */
function read($key, $config = null) { public static function read($key, $config = null) {
$self =& Cache::getInstance();
if (!$config) { if (!$config) {
$config = $self->__name; $config = self::$_name;
} }
$settings = $self->settings($config); $settings = self::settings($config);
if (empty($settings)) { if (empty($settings)) {
return null; return null;
} }
if (!$self->isInitialized($config)) { if (!self::isInitialized($config)) {
return false; return false;
} }
$key = $self->_engines[$config]->key($key); $key = self::$_engines[$config]->key($key);
if (!$key) { if (!$key) {
return false; return false;
} }
$success = $self->_engines[$config]->read($settings['prefix'] . $key); $success = self::$_engines[$config]->read($settings['prefix'] . $key);
if ($config !== null && $config !== $self->__name) { if ($config !== null && $config !== self::$_name) {
$self->set(); self::set();
} }
return $success; return $success;
} }
@ -361,27 +323,25 @@ class Cache {
* @return mixed new value, or false if the data doesn't exist, is not integer, * @return mixed new value, or false if the data doesn't exist, is not integer,
* or if there was an error fetching it. * or if there was an error fetching it.
*/ */
public function increment($key, $offset = 1, $config = null) { public static function increment($key, $offset = 1, $config = null) {
$self =& Cache::getInstance();
if (!$config) { if (!$config) {
$config = $self->__name; $config = self::$_name;
} }
$settings = $self->settings($config); $settings = self::settings($config);
if (empty($settings)) { if (empty($settings)) {
return null; return null;
} }
if (!$self->isInitialized($config)) { if (!self::isInitialized($config)) {
return false; return false;
} }
$key = $self->_engines[$config]->key($key); $key = self::$_engines[$config]->key($key);
if (!$key || !is_integer($offset) || $offset < 0) { if (!$key || !is_integer($offset) || $offset < 0) {
return false; return false;
} }
$success = $self->_engines[$config]->increment($settings['prefix'] . $key, $offset); $success = self::$_engines[$config]->increment($settings['prefix'] . $key, $offset);
$self->set(); self::set();
return $success; return $success;
} }
/** /**
@ -394,27 +354,25 @@ class Cache {
* @return mixed new value, or false if the data doesn't exist, is not integer, * @return mixed new value, or false if the data doesn't exist, is not integer,
* or if there was an error fetching it * or if there was an error fetching it
*/ */
public function decrement($key, $offset = 1, $config = null) { public static function decrement($key, $offset = 1, $config = null) {
$self =& Cache::getInstance();
if (!$config) { if (!$config) {
$config = $self->__name; $config = self::$_name;
} }
$settings = $self->settings($config); $settings = self::settings($config);
if (empty($settings)) { if (empty($settings)) {
return null; return null;
} }
if (!$self->isInitialized($config)) { if (!self::isInitialized($config)) {
return false; return false;
} }
$key = $self->_engines[$config]->key($key); $key = self::$_engines[$config]->key($key);
if (!$key || !is_integer($offset) || $offset < 0) { if (!$key || !is_integer($offset) || $offset < 0) {
return false; return false;
} }
$success = $self->_engines[$config]->decrement($settings['prefix'] . $key, $offset); $success = self::$_engines[$config]->decrement($settings['prefix'] . $key, $offset);
$self->set(); self::set();
return $success; return $success;
} }
/** /**
@ -435,29 +393,26 @@ class Cache {
* @param string $key Identifier for the data * @param string $key Identifier for the data
* @param string $config name of the configuration to use * @param string $config name of the configuration to use
* @return boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed * @return boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed
* @access public
* @static
*/ */
function delete($key, $config = null) { public static function delete($key, $config = null) {
$self =& Cache::getInstance();
if (!$config) { if (!$config) {
$config = $self->__name; $config = self::$_name;
} }
$settings = $self->settings($config); $settings = self::settings($config);
if (empty($settings)) { if (empty($settings)) {
return null; return null;
} }
if (!$self->isInitialized($config)) { if (!self::isInitialized($config)) {
return false; return false;
} }
$key = $self->_engines[$config]->key($key); $key = self::$_engines[$config]->key($key);
if (!$key) { if (!$key) {
return false; return false;
} }
$success = $self->_engines[$config]->delete($settings['prefix'] . $key); $success = self::$_engines[$config]->delete($settings['prefix'] . $key);
$self->set(); self::set();
return $success; return $success;
} }
@ -467,25 +422,22 @@ class Cache {
* @param boolean $check if true will check expiration, otherwise delete all * @param boolean $check if true will check expiration, otherwise delete all
* @param string $config name of the configuration to use * @param string $config name of the configuration to use
* @return boolean True if the cache was succesfully cleared, false otherwise * @return boolean True if the cache was succesfully cleared, false otherwise
* @access public
* @static
*/ */
function clear($check = false, $config = null) { public static function clear($check = false, $config = null) {
$self =& Cache::getInstance();
if (!$config) { if (!$config) {
$config = $self->__name; $config = self::$_name;
} }
$settings = $self->settings($config); $settings = self::settings($config);
if (empty($settings)) { if (empty($settings)) {
return null; return null;
} }
if (!$self->isInitialized($config)) { if (!self::isInitialized($config)) {
return false; return false;
} }
$success = $self->_engines[$config]->clear($check); $success = self::$_engines[$config]->clear($check);
$self->set(); self::set();
return $success; return $success;
} }
@ -495,18 +447,15 @@ class Cache {
* @param string $engine Name of the engine * @param string $engine Name of the engine
* @param string $config Name of the configuration setting * @param string $config Name of the configuration setting
* @return bool Whether or not the config name has been initialized. * @return bool Whether or not the config name has been initialized.
* @access public
* @static
*/ */
function isInitialized($name = null) { public static function isInitialized($name = null) {
if (Configure::read('Cache.disable')) { if (Configure::read('Cache.disable')) {
return false; return false;
} }
$self =& Cache::getInstance(); if (!$name && isset(self::$_config[self::$_name])) {
if (!$name && isset($self->__config[$self->__name])) { $name = self::$_name;
$name = $self->__name;
} }
return isset($self->_engines[$name]); return isset(self::$_engines[$name]);
} }
/** /**
@ -520,13 +469,12 @@ class Cache {
* @access public * @access public
* @static * @static
*/ */
function settings($name = null) { public static function settings($name = null) {
$self =& Cache::getInstance(); if (!$name && isset(self::$_config[self::$_name])) {
if (!$name && isset($self->__config[$self->__name])) { $name = self::$_name;
$name = $self->__name;
} }
if (!empty($self->_engines[$name])) { if (!empty(self::$_engines[$name])) {
return $self->_engines[$name]->settings(); return self::$_engines[$name]->settings();
} }
return array(); return array();
} }