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 3917 2009-01-21 03:06:22Z zombor $
11 * @copyright (c) 2007-2008 Kohana Team
12 * @license http://kohanaphp.com/license.html
16 // For garbage collection
17 protected static $loaded;
26 * Returns a singleton instance of Cache.
28 * @param array configuration
31 public static function instance($config = array())
35 // Create the Cache instance
36 ($obj === NULL) and $obj = new Cache($config);
42 * Loads the configured driver and validates it.
44 * @param array|string custom configuration or config group name
47 public function __construct($config = FALSE)
49 if (is_string($config))
53 // Test the config group name
54 if (($config = Kohana
::config('cache.'.$config)) === NULL)
55 throw new Kohana_Exception('cache.undefined_group', $name);
58 if (is_array($config))
60 // Append the default configuration options
61 $config +
= Kohana
::config('cache.default');
65 // Load the default group
66 $config = Kohana
::config('cache.default');
69 // Cache the config in the object
70 $this->config
= $config;
73 $driver = 'Cache_'.ucfirst($this->config
['driver']).'_Driver';
76 if ( ! Kohana
::auto_load($driver))
77 throw new Kohana_Exception('core.driver_not_found', $this->config
['driver'], get_class($this));
79 // Initialize the driver
80 $this->driver
= new $driver($this->config
['params']);
82 // Validate the driver
83 if ( ! ($this->driver
instanceof Cache_Driver
))
84 throw new Kohana_Exception('core.driver_implements', $this->config
['driver'], get_class($this), 'Cache_Driver');
86 Kohana
::log('debug', 'Cache Library initialized');
88 if (self
::$loaded !== TRUE)
90 $this->config
['requests'] = (int) $this->config
['requests'];
92 if ($this->config
['requests'] > 0 AND mt_rand(1, $this->config
['requests']) === 1)
94 // Do garbage collection
95 $this->driver
->delete_expired();
97 Kohana
::log('debug', 'Cache: Expired caches deleted.');
100 // Cache has been loaded once
101 self
::$loaded = TRUE;
106 * Fetches a cache by id. Non-string cache items are automatically
107 * unserialized before the cache is returned. NULL is returned when
108 * a cache item is not found.
110 * @param string cache id
111 * @return mixed cached data or NULL
113 public function get($id)
115 // Change slashes to colons
116 $id = str_replace(array('/', '\\'), '=', $id);
118 if ($data = $this->driver
->get($id))
120 if (substr($data, 0, 14) === '<{serialized}>')
122 // Data has been serialized, unserialize now
123 $data = unserialize(substr($data, 14));
131 * Fetches all of the caches for a given tag. An empty array will be
132 * returned when no matching caches are found.
134 * @param string cache tag
135 * @return array all cache items matching the tag
137 public function find($tag)
139 if ($ids = $this->driver
->find($tag))
142 foreach ($ids as $id)
144 // Load each cache item and add it to the array
145 if (($cache = $this->get($id)) !== NULL)
158 * Set a cache item by id. Tags may also be added and a custom lifetime
159 * can be set. Non-string data is automatically serialized.
161 * @param string unique cache id
162 * @param mixed data to cache
163 * @param array tags for this item
164 * @param integer number of seconds until the cache expires
167 function set($id, $data, $tags = NULL, $lifetime = NULL)
169 if (is_resource($data))
170 throw new Kohana_Exception('cache.resources');
172 // Change slashes to colons
173 $id = str_replace(array('/', '\\'), '=', $id);
175 if ( ! is_string($data))
177 // Serialize all non-string data, so that types can be preserved
178 $data = '<{serialized}>'.serialize($data);
181 // Make sure that tags is an array
182 $tags = empty($tags) ?
array() : (array) $tags;
184 if ($lifetime === NULL)
186 // Get the default lifetime
187 $lifetime = $this->config
['lifetime'];
190 return $this->driver
->set($id, $data, $tags, $lifetime);
194 * Delete a cache item by id.
196 * @param string cache id
199 public function delete($id)
201 // Change slashes to colons
202 $id = str_replace(array('/', '\\'), '=', $id);
204 return $this->driver
->delete($id);
208 * Delete all cache items with a given tag.
210 * @param string cache tag name
213 public function delete_tag($tag)
215 return $this->driver
->delete(FALSE, $tag);
219 * Delete ALL cache items items.
223 public function delete_all()
225 return $this->driver
->delete(TRUE);