/libraries/gantry/core/utilities/cache/cache.class.php

https://bitbucket.org/nlabyt/bcf-ball-4eb2 · PHP · 253 lines · 108 code · 24 blank · 121 comment · 32 complexity · 15a28cef1b5bc2c922593c3e91d3c6da MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id: cache.class.php 2325 2012-08-13 17:46:48Z btowles $
  4. * @author RocketTheme http://www.rockettheme.com
  5. * @copyright Copyright (C) 2007 - 2012 RocketTheme, LLC
  6. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  7. *
  8. *
  9. *
  10. * Original Author and Licence
  11. * @author Mateusz 'MatheW' W??jcik, <mat.wojcik@gmail.com>
  12. * @link http://mwojcik.pl
  13. * @version 1.0
  14. * @license GPL
  15. */
  16. require_once 'cacheDriver.interface.php';
  17. /**
  18. * @throws CacheException
  19. */
  20. class GantryCacheLib
  21. {
  22. /**
  23. * Array of driver strategies
  24. *
  25. * @var array
  26. */
  27. protected $drivers = array();
  28. /**
  29. * Name of default driver
  30. *
  31. * @var string
  32. */
  33. protected $defaultDriver = 'NULL';
  34. /**
  35. * Default cache lifetime in seconds
  36. *
  37. * @var int
  38. */
  39. protected $defaultLifeTime = 300;
  40. /**
  41. * Specifies if debug mode is on
  42. *
  43. * @var boolean
  44. */
  45. protected $debugMode = false;
  46. /**
  47. * Constructor
  48. *
  49. * @param int $defaultLifeTime Default cache lifetime in seconds
  50. *
  51. * @return void
  52. */
  53. public function __construct($defaultLifeTime = 0)
  54. {
  55. if (is_numeric($defaultLifeTime) && $defaultLifeTime > 0) $this->defaultLifeTime = $defaultLifeTime;
  56. }
  57. /**
  58. * Short description of method addDriver
  59. *
  60. * @param string $name Name of driver strategy
  61. * @param CacheDriver $driver Driver strategy
  62. * @param boolean $default Default strategy
  63. *
  64. * @return boolean
  65. */
  66. public function addDriver($name, GantryCacheLibDriver &$driver, $default = TRUE)
  67. {
  68. if (isset($this->drivers[$name])) return false;
  69. $this->drivers[$name] = $driver;
  70. if ($default) $this->defaultDriver = $name;
  71. return true;
  72. }
  73. /**
  74. * Gets driver strategy
  75. *
  76. * @param string $name Name of driver strategy
  77. *
  78. * @throws CacheException
  79. * @return CacheDriver
  80. */
  81. protected function getDriver($name = NULL)
  82. {
  83. if (empty($name) || !array_key_exists($name, $this->drivers)) $name = $this->defaultDriver; else return $this->drivers[$name];
  84. if (empty($name) || !array_key_exists($name, $this->drivers)) foreach ($this->drivers as $drvName=> $driver) {
  85. return $this->drivers[$drvName];
  86. } else return $this->drivers[$name];
  87. throw new CacheException('No driver strategy set!');
  88. }
  89. /**
  90. * Save data to cache
  91. *
  92. * @param string $groupName Name of group of cache
  93. * @param string $identifier Identifier of data - it should be unique in group
  94. * @param mixed $data Data
  95. * @param string $driver Driver strategy
  96. *
  97. * @throws CacheException
  98. * @return boolean
  99. */
  100. public function set($groupName, $identifier, $data, $driver = NULL)
  101. {
  102. try {
  103. return $this->getDriver($driver)->set($groupName, $identifier, serialize($data));
  104. } catch (CacheException $e) {
  105. if ($this->debugMode) throw $e; else return false;
  106. }
  107. }
  108. /**
  109. * Gets data from cache
  110. *
  111. * @param string $groupName Name of group
  112. * @param string $identifier Identifier of data
  113. * @param string $driver Driver strategy
  114. *
  115. * @throws CacheException
  116. * @return mixed
  117. */
  118. public function get($groupName, $identifier, $driver = NULL)
  119. {
  120. try {
  121. $drv = $this->getDriver($driver);
  122. if (!$drv->exists($groupName, $identifier)) return false;
  123. $data = $drv->get($groupName, $identifier);
  124. if ($data === false) return false;
  125. return unserialize($data);
  126. } catch (CacheException $e) {
  127. if ($this->debugMode) throw $e; else return false;
  128. }
  129. }
  130. /**
  131. * Clears all cache generated by this class with one/all drivers
  132. *
  133. * @param string $driver Name of driver strategy
  134. *
  135. * @return boolean
  136. */
  137. public function clearAllCache($driver = NULL)
  138. {
  139. try {
  140. if (empty($driver) || !array_key_exists($driver, $this->drivers)) {
  141. foreach ($this->drivers as $drv) $drv->clearAllCache();
  142. return true;
  143. }
  144. return $this->drivers[$driver]->clearAllCache();
  145. } catch (CacheException $e) {
  146. if ($this->debugMode) throw $e; else return false;
  147. }
  148. }
  149. /**
  150. * Sets the lifetime
  151. *
  152. * @param string $driver Name of driver strategy
  153. *
  154. * @return boolean
  155. */
  156. public function setLifeTime($lifetime, $driver = NULL)
  157. {
  158. try {
  159. if (empty($driver) || !array_key_exists($driver, $this->drivers)) {
  160. foreach ($this->drivers as $drv) $drv->setLifeTime($lifetime);
  161. return true;
  162. }
  163. return $this->drivers[$driver]->setLifeTime($lifetime);
  164. } catch (CacheException $e) {
  165. if ($this->debugMode) throw $e; else return false;
  166. }
  167. }
  168. /**
  169. * Clears cache of specified group with one/all drivers
  170. *
  171. * @param string $groupName Name of group
  172. * @param string $driver Name of driver strategy
  173. *
  174. * @return boolean
  175. */
  176. public function clearGroupCache($groupName, $driver = NULL)
  177. {
  178. try {
  179. if (empty($driver) || !array_key_exists($driver, $this->drivers)) {
  180. foreach ($this->drivers as $drv) $drv->clearGroupCache($groupName);
  181. return true;
  182. }
  183. return $this->drivers[$driver]->clearGroupCache($groupName);
  184. } catch (CacheException $e) {
  185. if ($this->debugMode) throw $e; else return false;
  186. }
  187. }
  188. /**
  189. * Clears cache of specified identifier of group with one/all drivers
  190. *
  191. * @param string $groupName Name of group
  192. * @param string $identifier Identifier
  193. * @param string $driver Name of driver strategy
  194. *
  195. * @return boolean
  196. */
  197. public function clearCache($groupName, $identifier, $driver = NULL)
  198. {
  199. try {
  200. if (empty($driver) || !array_key_exists($driver, $this->drivers)) {
  201. foreach ($this->drivers as $drv) $drv->clearCache($groupName, $identifier);
  202. return true;
  203. }
  204. return $this->drivers[$driver]->clearCache($groupName, $identifier);
  205. } catch (CacheException $e) {
  206. if ($this->debugMode) throw $e; else return false;
  207. }
  208. }
  209. /**
  210. * Turns debug mode on. Exceptions will be thrown
  211. *
  212. */
  213. public function debugModeOn()
  214. {
  215. $this->debugMode = true;
  216. }
  217. /**
  218. * Turns debug mode off. No exceptions will be thrown
  219. *
  220. */
  221. public function debugModeOff()
  222. {
  223. $this->debugMode = false;
  224. }
  225. } /* end of class Cache */
  226. class CacheException extends Exception
  227. {
  228. }
  229. ?>