PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/system/libraries/Cache.php

https://github.com/Toushi/flow
PHP | 228 lines | 146 code | 25 blank | 57 comment | 9 complexity | e12e26e45a15f8d7a08ca176696677b3 MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  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.
  6. *
  7. * $Id: Cache.php 3263 2008-08-05 17:57:50Z PugFish $
  8. *
  9. * @package Cache
  10. * @author Kohana Team
  11. * @copyright (c) 2007-2008 Kohana Team
  12. * @license http://kohanaphp.com/license.html
  13. */
  14. class Cache_Core {
  15. // For garbage collection
  16. protected static $loaded;
  17. // Configuration
  18. protected $config;
  19. // Driver object
  20. protected $driver;
  21. /**
  22. * Returns a singleton instance of Cache.
  23. *
  24. * @param array configuration
  25. * @return Cache_Core
  26. */
  27. public static function instance($config = array())
  28. {
  29. static $obj;
  30. // Create the Cache instance
  31. ($obj === NULL) and $obj = new Cache($config);
  32. return $obj;
  33. }
  34. /**
  35. * Loads the configured driver and validates it.
  36. *
  37. * @param array|string custom configuration or config group name
  38. * @return void
  39. */
  40. public function __construct($config = FALSE)
  41. {
  42. if (is_string($config))
  43. {
  44. $name = $config;
  45. // Test the config group name
  46. if (($config = Kohana::config('cache.'.$config)) === NULL)
  47. throw new Kohana_Exception('cache.undefined_group', $name);
  48. }
  49. if (is_array($config))
  50. {
  51. // Append the default configuration options
  52. $config += Kohana::config('cache.default');
  53. }
  54. else
  55. {
  56. // Load the default group
  57. $config = Kohana::config('cache.default');
  58. }
  59. // Cache the config in the object
  60. $this->config = $config;
  61. // Set driver name
  62. $driver = 'Cache_'.ucfirst($this->config['driver']).'_Driver';
  63. // Load the driver
  64. if ( ! Kohana::auto_load($driver))
  65. throw new Kohana_Exception('core.driver_not_found', $this->config['driver'], get_class($this));
  66. // Initialize the driver
  67. $this->driver = new $driver($this->config['params']);
  68. // Validate the driver
  69. if ( ! ($this->driver instanceof Cache_Driver))
  70. throw new Kohana_Exception('core.driver_implements', $this->config['driver'], get_class($this), 'Cache_Driver');
  71. Kohana::log('debug', 'Cache Library initialized');
  72. if (self::$loaded !== TRUE)
  73. {
  74. $this->config['requests'] = (int) $this->config['requests'];
  75. if ($this->config['requests'] > 0 AND mt_rand(1, $this->config['requests']) === 1)
  76. {
  77. // Do garbage collection
  78. $this->driver->delete_expired();
  79. Kohana::log('debug', 'Cache: Expired caches deleted.');
  80. }
  81. // Cache has been loaded once
  82. self::$loaded = TRUE;
  83. }
  84. }
  85. /**
  86. * Fetches a cache by id. Non-string cache items are automatically
  87. * unserialized before the cache is returned. NULL is returned when
  88. * a cache item is not found.
  89. *
  90. * @param string cache id
  91. * @return mixed cached data or NULL
  92. */
  93. public function get($id)
  94. {
  95. // Change slashes to colons
  96. $id = str_replace(array('/', '\\'), '=', $id);
  97. if ($data = $this->driver->get($id))
  98. {
  99. if (substr($data, 0, 14) === '<{serialized}>')
  100. {
  101. // Data has been serialized, unserialize now
  102. $data = unserialize(substr($data, 14));
  103. }
  104. }
  105. return $data;
  106. }
  107. /**
  108. * Fetches all of the caches for a given tag. An empty array will be
  109. * returned when no matching caches are found.
  110. *
  111. * @param string cache tag
  112. * @return array all cache items matching the tag
  113. */
  114. public function find($tag)
  115. {
  116. if ($ids = $this->driver->find($tag))
  117. {
  118. $data = array();
  119. foreach ($ids as $id)
  120. {
  121. // Load each cache item and add it to the array
  122. if (($cache = $this->get($id)) !== NULL)
  123. {
  124. $data[$id] = $cache;
  125. }
  126. }
  127. return $data;
  128. }
  129. return array();
  130. }
  131. /**
  132. * Set a cache item by id. Tags may also be added and a custom lifetime
  133. * can be set. Non-string data is automatically serialized.
  134. *
  135. * @param string unique cache id
  136. * @param mixed data to cache
  137. * @param array tags for this item
  138. * @param integer number of seconds until the cache expires
  139. * @return boolean
  140. */
  141. function set($id, $data, $tags = NULL, $lifetime = NULL)
  142. {
  143. if (is_resource($data))
  144. throw new Kohana_Exception('cache.resources');
  145. // Change slashes to colons
  146. $id = str_replace(array('/', '\\'), '=', $id);
  147. if ( ! is_string($data))
  148. {
  149. // Serialize all non-string data, so that types can be preserved
  150. $data = '<{serialized}>'.serialize($data);
  151. }
  152. // Make sure that tags is an array
  153. $tags = empty($tags) ? array() : (array) $tags;
  154. if ($lifetime === NULL)
  155. {
  156. // Get the default lifetime
  157. $lifetime = $this->config['lifetime'];
  158. }
  159. return $this->driver->set($id, $data, $tags, $lifetime);
  160. }
  161. /**
  162. * Delete a cache item by id.
  163. *
  164. * @param string cache id
  165. * @return boolean
  166. */
  167. public function delete($id)
  168. {
  169. // Change slashes to colons
  170. $id = str_replace(array('/', '\\'), '=', $id);
  171. return $this->driver->delete($id);
  172. }
  173. /**
  174. * Delete all cache items with a given tag.
  175. *
  176. * @param string cache tag name
  177. * @return boolean
  178. */
  179. public function delete_tag($tag)
  180. {
  181. return $this->driver->delete(FALSE, $tag);
  182. }
  183. /**
  184. * Delete ALL cache items items.
  185. *
  186. * @return boolean
  187. */
  188. public function delete_all()
  189. {
  190. return $this->driver->delete(TRUE);
  191. }
  192. } // End Cache