Refactor Cache::__construct() to make it easy to programmatically create cache instances. From: <> --- Cache.php | 24 +++++++++++------------- CacheRouter.php | 2 +- engines/file.php | 11 +++++------ engines/memcache.php | 21 +++++++++------------ 4 files changed, 26 insertions(+), 32 deletions(-) diff --git Cache.php Cache.php index bb5bcc4..edbfcb0 100644 --- Cache.php +++ Cache.php @@ -17,29 +17,27 @@ class Cache { var $static = FALSE; var $lifetime = 0; - function __construct($bin) { - global $conf; - + function __construct($bin, $options, $default_options) { $this->name = $bin; // Setup our prefixes so that we can prefix a particular bin, or if not set use the default prefix. - if (isset($conf['cacherouter'][$bin]['prefix'])) { - $this->prefix = $conf['cacherouter'][$bin]['prefix'] .'-'; + if (isset($options['prefix'])) { + $this->prefix = $options['prefix'] .'-'; } - else if (!empty($conf['cacherouter']['default']['prefix'])) { - $this->prefix = $conf['cacherouter']['default']['prefix'] .'-'; + else if (!empty($default_options['prefix'])) { + $this->prefix = $default_options['prefix'] .'-'; } // This allows us to turn off fast_cache for cache_page so that we can get anonymous statistics. - if (isset($conf['cacherouter']['default']['fast_cache'])) { - $this->fast_cache = $conf['cacherouter']['default']['fast_cache']; + if (isset($default_options['fast_cache'])) { + $this->fast_cache = $default_options['fast_cache']; } // This allows us to turn off static content caching for modules/bins that are already doing this. - if (isset($conf['cacherouter'][$bin]['static'])) { - $this->static = $conf['cacherouter'][$bin]['static']; + if (isset($options['static'])) { + $this->static = $options['static']; } - else if (isset($conf['cacherouter']['default']['static'])) { - $this->static = $conf['cacherouter']['default']['static']; + else if (isset($default_options['static'])) { + $this->static = $default_options['static']; } // Setup our prefixed lookup and lock table names for shared storage. diff --git CacheRouter.php CacheRouter.php index 7b58e0a..c3ad877 100644 --- CacheRouter.php +++ CacheRouter.php @@ -36,7 +36,7 @@ class CacheRouter { } $cache_engine = $type . 'Cache'; - $this->map[$bin] = new $cache_engine($bin); + $this->map[$bin] = new $cache_engine($bin, $conf['cacherouter'][$bin], $conf['cacherouter']['default']); } public function get($key, $bin) { diff --git engines/file.php engines/file.php index 3fbc89c..d3fb727 100644 --- engines/file.php +++ engines/file.php @@ -14,16 +14,15 @@ class fileCache extends Cache { return TRUE; } - function __construct($bin) { - global $conf; + function __construct($bin, $options, $default_options) { // Assign the path on the following order: bin specific -> default specific -> /tmp/filepath - if (!isset($conf['cacherouter'][$bin]['path'])) { - if (isset($conf['cacherouter']['default']['path'])) { - $this->fspath = $conf['cacherouter']['default']['path']; + if (!isset($options['path'])) { + if (isset($default_options['path'])) { + $this->fspath = $default_options['path']; } } else { - $this->fspath = $conf['cacherouter'][$bin]['path']; + $this->fspath = $options['path']; } parent::__construct($bin); } diff --git engines/memcache.php engines/memcache.php index e11ebe1..e17a5ce 100644 --- engines/memcache.php +++ engines/memcache.php @@ -13,21 +13,18 @@ class memcacheCache extends Cache { return $this->fast_cache; } - function __construct($bin) { - global $conf; + function __construct($bin, $options, $default_options) { // Assign the servers on the following order: bin specific -> default specific -> localhost port 11211 - if (isset($conf['cacherouter'][$bin]['server'])) { - $this->settings['servers'] = $conf['cacherouter'][$bin]['server']; - $this->settings['compress'] = isset($conf['cacherouter'][$bin]['compress']) ? MEMCACHE_COMPRESSED : 0; - $this->settings['shared'] = isset($conf['cacherouter'][$bin]['shared']) ? - $conf['cacherouter'][$bin]['shared'] : TRUE; + if (isset($options['server'])) { + $this->settings['servers'] = $options['server']; + $this->settings['compress'] = isset($options['compress']) ? MEMCACHE_COMPRESSED : 0; + $this->settings['shared'] = isset($options['shared']) ? $options['shared'] : TRUE; } else { - if (isset($conf['cacherouter']['default']['server'])) { - $this->settings['servers'] = $conf['cacherouter']['default']['server']; - $this->settings['compress'] = isset($conf['cacherouter']['default']['compress']) ? MEMCACHE_COMPRESSED : 0; - $this->settings['shared'] = isset($conf['cacherouter']['default']['shared']) ? - $conf['cacherouter']['default']['shared'] : TRUE; + if (isset($default_options['server'])) { + $this->settings['servers'] = $default_options['server']; + $this->settings['compress'] = isset($default_options['compress']) ? MEMCACHE_COMPRESSED : 0; + $this->settings['shared'] = isset($default_options['shared']) ? $default_options['shared'] : TRUE; } else { $this->settings['servers'] = array('localhost:11211');