PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/system/libraries/Cache.php

https://github.com/MHordecki/milionkostek
PHP | 228 lines | 148 code | 24 blank | 56 comment | 9 complexity | cb2be556825dc15fb94639f486ae210b 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 2667 2008-05-13 07:23:53Z Geert $
  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 object Cache
  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 custom configuration
  38. * @return void
  39. */
  40. public function __construct($config = array())
  41. {
  42. if (empty($config))
  43. {
  44. // Load the default group
  45. $config = Config::item('cache.default');
  46. }
  47. elseif (is_string($config))
  48. {
  49. $name = $config;
  50. // Test the config group name
  51. if (($config = Config::item('cache.'.$config)) === NULL)
  52. throw new Kohana_Exception('cache.undefined_group', $name);
  53. }
  54. // Load configuration
  55. $this->config = (array) $config + Config::item('cache.default');
  56. // Set driver name
  57. $driver = 'Cache_'.ucfirst($this->config['driver']).'_Driver';
  58. // Load the driver
  59. if ( ! Kohana::auto_load($driver))
  60. throw new Kohana_Exception('core.driver_not_found', $this->config['driver'], get_class($this));
  61. // Initialize the driver
  62. $this->driver = new $driver($this->config['params']);
  63. // Validate the driver
  64. if ( ! ($this->driver instanceof Cache_Driver))
  65. throw new Kohana_Exception('core.driver_implements', $this->config['driver'], get_class($this), 'Cache_Driver');
  66. Log::add('debug', 'Cache Library initialized');
  67. if (self::$loaded !== TRUE)
  68. {
  69. $this->config['requests'] = (int) $this->config['requests'];
  70. if ($this->config['requests'] > 0 AND mt_rand(1, $this->config['requests']) === 1)
  71. {
  72. // Do garbage collection
  73. $this->driver->delete_expired();
  74. Log::add('debug', 'Cache: Expired caches deleted.');
  75. }
  76. // Cache has been loaded once
  77. self::$loaded = TRUE;
  78. }
  79. }
  80. /**
  81. * Fetches a cache by id. Non-string cache items are automatically
  82. * unserialized before the cache is returned. NULL is returned when
  83. * a cache item is not found.
  84. *
  85. * @param string cache id
  86. * @return mixed cached data or NULL
  87. */
  88. public function get($id)
  89. {
  90. // Change slashes to colons
  91. $id = str_replace(array('/', '\\'), '=', $id);
  92. if ($data = $this->driver->get($id))
  93. {
  94. if (substr($data, 0, 14) === '<{serialized}>')
  95. {
  96. // Data has been serialized, unserialize now
  97. $data = unserialize(substr($data, 14));
  98. }
  99. }
  100. return $data;
  101. }
  102. /**
  103. * Fetches all of the caches for a given tag. An empty array will be
  104. * returned when no matching caches are found.
  105. *
  106. * @param string cache tag
  107. * @return array all cache items matching the tag
  108. */
  109. public function find($tag)
  110. {
  111. if ($ids = $this->driver->find($tag))
  112. {
  113. $data = array();
  114. foreach ($ids as $id)
  115. {
  116. // Load each cache item and add it to the array
  117. if (($cache = $this->get($id)) !== NULL)
  118. {
  119. $data[$id] = $cache;
  120. }
  121. }
  122. return $data;
  123. }
  124. return array();
  125. }
  126. /**
  127. * Set a cache item by id. Tags may also be added and a custom lifetime
  128. * can be set. Non-string data is automatically serialized.
  129. *
  130. * @param string unique cache id
  131. * @param mixed data to cache
  132. * @param array tags for this item
  133. * @param integer number of seconds until the cache expires
  134. * @return bool
  135. */
  136. function set($id, $data, $tags = NULL, $lifetime = NULL)
  137. {
  138. if (is_resource($data))
  139. throw new Kohana_Exception('cache.resources');
  140. // Change slashes to colons
  141. $id = str_replace(array('/', '\\'), '=', $id);
  142. if ( ! is_string($data))
  143. {
  144. // Serialize all non-string data, so that types can be preserved
  145. $data = '<{serialized}>'.serialize($data);
  146. }
  147. if (empty($tags))
  148. {
  149. $tags = array();
  150. }
  151. else
  152. {
  153. // Make sure that tags is an array
  154. $tags = (array) $tags;
  155. }
  156. if ($lifetime === NULL)
  157. {
  158. // Get the default lifetime
  159. $lifetime = $this->config['lifetime'];
  160. }
  161. return $this->driver->set($id, $data, $tags, $lifetime);
  162. }
  163. /**
  164. * Delete a cache item by id.
  165. *
  166. * @param string cache id
  167. * @return bool
  168. */
  169. public function delete($id)
  170. {
  171. // Change slashes to colons
  172. $id = str_replace(array('/', '\\'), '=', $id);
  173. return $this->driver->delete($id);
  174. }
  175. /**
  176. * Delete all cache items with a given tag.
  177. *
  178. * @param string cache tag name
  179. * @return bool
  180. */
  181. public function delete_tag($tag)
  182. {
  183. return $this->driver->delete(FALSE, $tag);
  184. }
  185. /**
  186. * Delete ALL cache items items.
  187. *
  188. * @return bool
  189. */
  190. public function delete_all()
  191. {
  192. return $this->driver->delete(TRUE);
  193. }
  194. } // End Cache