diff options
author | Tim Almdal <tnalmdal@shaw.ca> | 2009-12-26 11:24:50 -0800 |
---|---|---|
committer | Tim Almdal <tnalmdal@shaw.ca> | 2009-12-26 11:24:50 -0800 |
commit | 3060a6f662da66008d57a461bf1c9b5b4aa2b002 (patch) | |
tree | 442fd290505817efc0324f2af6e01805cb7396aa /system/libraries/Cache.php | |
parent | 1cd6a615bb47a33794e4a4f690c87a348ab752d7 (diff) | |
parent | 32d25dafd5b033338b6a9bb8c7c53edab462543a (diff) |
Merge branch 'master' into talmdal_dev
Conflicts:
modules/gallery/controllers/albums.php
modules/gallery/controllers/movies.php
modules/gallery/controllers/photos.php
Diffstat (limited to 'system/libraries/Cache.php')
-rw-r--r-- | system/libraries/Cache.php | 202 |
1 files changed, 122 insertions, 80 deletions
diff --git a/system/libraries/Cache.php b/system/libraries/Cache.php index 8a02a905..024c1888 100644 --- a/system/libraries/Cache.php +++ b/system/libraries/Cache.php @@ -4,20 +4,17 @@ * resources. Caches are identified by a unique string. Tagging of caches is * also supported, and caches can be found and deleted by id or tag. * - * $Id: Cache.php 4321 2009-05-04 01:39:44Z kiall $ + * $Id: Cache.php 4605 2009-09-14 17:22:21Z kiall $ * * @package Cache * @author Kohana Team - * @copyright (c) 2007-2008 Kohana Team - * @license http://kohanaphp.com/license.html + * @copyright (c) 2007-2009 Kohana Team + * @license http://kohanaphp.com/license */ class Cache_Core { protected static $instances = array(); - // For garbage collection - protected static $loaded; - // Configuration protected $config; @@ -55,7 +52,7 @@ class Cache_Core { // Test the config group name if (($config = Kohana::config('cache.'.$config)) === NULL) - throw new Kohana_Exception('cache.undefined_group', $name); + throw new Cache_Exception('The :group: group is not defined in your configuration.', array(':group:' => $name)); } if (is_array($config)) @@ -77,132 +74,177 @@ class Cache_Core { // Load the driver if ( ! Kohana::auto_load($driver)) - throw new Kohana_Exception('core.driver_not_found', $this->config['driver'], get_class($this)); + throw new Cache_Exception('The :driver: driver for the :class: library could not be found', + array(':driver:' => $this->config['driver'], ':class:' => get_class($this))); // Initialize the driver $this->driver = new $driver($this->config['params']); // Validate the driver if ( ! ($this->driver instanceof Cache_Driver)) - throw new Kohana_Exception('core.driver_implements', $this->config['driver'], get_class($this), 'Cache_Driver'); + throw new Cache_Exception('The :driver: driver for the :library: library must implement the :interface: interface', + array(':driver:' => $this->config['driver'], ':library:' => get_class($this), ':interface:' => 'Cache_Driver')); + + Kohana_Log::add('debug', 'Cache Library initialized'); + } - Kohana::log('debug', 'Cache Library initialized'); + /** + * Set cache items + */ + public function set($key, $value = NULL, $tags = NULL, $lifetime = NULL) + { + if ($lifetime === NULL) + { + $lifetime = $this->config['lifetime']; + } - if (Cache::$loaded !== TRUE) + if ( ! is_array($key)) { - $this->config['requests'] = (int) $this->config['requests']; + $key = array($key => $value); + } - if ($this->config['requests'] > 0 AND mt_rand(1, $this->config['requests']) === 1) + if ($this->config['prefix'] !== NULL) + { + $key = $this->add_prefix($key); + + if ($tags !== NULL) { - // Do garbage collection - $this->driver->delete_expired(); - - Kohana::log('debug', 'Cache: Expired caches deleted.'); + $tags = $this->add_prefix($tags, FALSE); } - - // Cache has been loaded once - Cache::$loaded = TRUE; } + + return $this->driver->set($key, $tags, $lifetime); } /** - * Fetches a cache by id. NULL is returned when a cache item is not found. - * - * @param string cache id - * @return mixed cached data or NULL + * Get a cache items by key */ - public function get($id) + public function get($keys) { - // Sanitize the ID - $id = $this->sanitize_id($id); + $single = FALSE; - return $this->driver->get($id); + if ( ! is_array($keys)) + { + $keys = array($keys); + $single = TRUE; + } + + if ($this->config['prefix'] !== NULL) + { + $keys = $this->add_prefix($keys, FALSE); + + if ( ! $single) + { + return $this->strip_prefix($this->driver->get($keys, $single)); + } + + } + + return $this->driver->get($keys, $single); } /** - * Fetches all of the caches for a given tag. An empty array will be - * returned when no matching caches are found. - * - * @param string cache tag - * @return array all cache items matching the tag + * Get cache items by tags */ - public function find($tag) + public function get_tag($tags) { - return $this->driver->find($tag); + if ( ! is_array($tags)) + { + $tags = array($tags); + } + + if ($this->config['prefix'] !== NULL) + { + $tags = $this->add_prefix($tags, FALSE); + return $this->strip_prefix($this->driver->get_tag($tags)); + } + else + { + return $this->driver->get_tag($tags); + } } /** - * Set a cache item by id. Tags may also be added and a custom lifetime - * can be set. Non-string data is automatically serialized. - * - * @param string unique cache id - * @param mixed data to cache - * @param array|string tags for this item - * @param integer number of seconds until the cache expires - * @return boolean + * Delete cache item by key */ - function set($id, $data, $tags = NULL, $lifetime = NULL) + public function delete($keys) { - if (is_resource($data)) - throw new Kohana_Exception('cache.resources'); - - // Sanitize the ID - $id = $this->sanitize_id($id); + if ( ! is_array($keys)) + { + $keys = array($keys); + } - if ($lifetime === NULL) + if ($this->config['prefix'] !== NULL) { - // Get the default lifetime - $lifetime = $this->config['lifetime']; + $keys = $this->add_prefix($keys, FALSE); } - return $this->driver->set($id, $data, (array) $tags, $lifetime); + return $this->driver->delete($keys); } /** - * Delete a cache item by id. - * - * @param string cache id - * @return boolean + * Delete cache items by tag */ - public function delete($id) + public function delete_tag($tags) { - // Sanitize the ID - $id = $this->sanitize_id($id); + if ( ! is_array($tags)) + { + $tags = array($tags); + } + + if ($this->config['prefix'] !== NULL) + { + $tags = $this->add_prefix($tags, FALSE); + } - return $this->driver->delete($id); + return $this->driver->delete_tag($tags); } /** - * Delete all cache items with a given tag. - * - * @param string cache tag name - * @return boolean + * Empty the cache */ - public function delete_tag($tag) + public function delete_all() { - return $this->driver->delete($tag, TRUE); + return $this->driver->delete_all(); } /** - * Delete ALL cache items items. - * - * @return boolean + * Add a prefix to keys or tags */ - public function delete_all() + protected function add_prefix($array, $to_key = TRUE) { - return $this->driver->delete(TRUE); + $out = array(); + + foreach($array as $key => $value) + { + if ($to_key) + { + $out[$this->config['prefix'].$key] = $value; + } + else + { + $out[$key] = $this->config['prefix'].$value; + } + } + + return $out; } /** - * Replaces troublesome characters with underscores. - * - * @param string cache id - * @return string + * Strip a prefix to keys or tags */ - protected function sanitize_id($id) + protected function strip_prefix($array) { - // Change slashes and spaces to underscores - return str_replace(array('/', '\\', ' '), '_', $id); + $out = array(); + + $start = strlen($this->config['prefix']); + + foreach($array as $key => $value) + { + $out[substr($key, $start)] = $value; + } + + return $out; } -} // End Cache +} // End Cache Library
\ No newline at end of file |