1 <?php defined('SYSPATH') OR die('No direct access allowed.');
3 * Provides a driver-based interface for finding, creating, and deleting cached
4 * resources. Caches are identified by a unique string. Tagging of caches is
5 * also supported, and caches can be found and deleted by id or tag.
7 * $Id: Cache.php 4321 2009-05-04 01:39:44Z kiall $
11 * @copyright (c) 2007-2008 Kohana Team
12 * @license http://kohanaphp.com/license.html
16 protected static $instances = array();
18 // For garbage collection
19 protected static $loaded;
28 * Returns a singleton instance of Cache.
30 * @param string configuration
33 public static function & instance($config = FALSE)
35 if ( ! isset(Cache::$instances[$config]))
37 // Create a new instance
38 Cache::$instances[$config] = new Cache($config);
41 return Cache::$instances[$config];
45 * Loads the configured driver and validates it.
47 * @param array|string custom configuration or config group name
50 public function __construct($config = FALSE)
52 if (is_string($config))
56 // Test the config group name
57 if (($config = Kohana::config('cache.'.$config)) === NULL)
58 throw new Kohana_Exception('cache.undefined_group', $name);
61 if (is_array($config))
63 // Append the default configuration options
64 $config += Kohana::config('cache.default');
68 // Load the default group
69 $config = Kohana::config('cache.default');
72 // Cache the config in the object
73 $this->config = $config;
76 $driver = 'Cache_'.ucfirst($this->config['driver']).'_Driver';
79 if ( ! Kohana::auto_load($driver))
80 throw new Kohana_Exception('core.driver_not_found', $this->config['driver'], get_class($this));
82 // Initialize the driver
83 $this->driver = new $driver($this->config['params']);
85 // Validate the driver
86 if ( ! ($this->driver instanceof Cache_Driver))
87 throw new Kohana_Exception('core.driver_implements', $this->config['driver'], get_class($this), 'Cache_Driver');
89 Kohana::log('debug', 'Cache Library initialized');
91 if (Cache::$loaded !== TRUE)
93 $this->config['requests'] = (int) $this->config['requests'];
95 if ($this->config['requests'] > 0 AND mt_rand(1, $this->config['requests']) === 1)
97 // Do garbage collection
98 $this->driver->delete_expired();
100 Kohana::log('debug', 'Cache: Expired caches deleted.');
103 // Cache has been loaded once
104 Cache::$loaded = TRUE;
109 * Fetches a cache by id. NULL is returned when a cache item is not found.
111 * @param string cache id
112 * @return mixed cached data or NULL
114 public function get($id)
117 $id = $this->sanitize_id($id);
119 return $this->driver->get($id);
123 * Fetches all of the caches for a given tag. An empty array will be
124 * returned when no matching caches are found.
126 * @param string cache tag
127 * @return array all cache items matching the tag
129 public function find($tag)
131 return $this->driver->find($tag);
135 * Set a cache item by id. Tags may also be added and a custom lifetime
136 * can be set. Non-string data is automatically serialized.
138 * @param string unique cache id
139 * @param mixed data to cache
140 * @param array|string tags for this item
141 * @param integer number of seconds until the cache expires
144 function set($id, $data, $tags = NULL, $lifetime = NULL)
146 if (is_resource($data))
147 throw new Kohana_Exception('cache.resources');
150 $id = $this->sanitize_id($id);
152 if ($lifetime === NULL)
154 // Get the default lifetime
155 $lifetime = $this->config['lifetime'];
158 return $this->driver->set($id, $data, (array) $tags, $lifetime);
162 * Delete a cache item by id.
164 * @param string cache id
167 public function delete($id)
170 $id = $this->sanitize_id($id);
172 return $this->driver->delete($id);
176 * Delete all cache items with a given tag.
178 * @param string cache tag name
181 public function delete_tag($tag)
183 return $this->driver->delete($tag, TRUE);
187 * Delete ALL cache items items.
191 public function delete_all()
193 return $this->driver->delete(TRUE);
197 * Replaces troublesome characters with underscores.
199 * @param string cache id
202 protected function sanitize_id($id)
204 // Change slashes and spaces to underscores
205 return str_replace(array('/', '\\', ' '), '_', $id);