Manager.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Cache
  17. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /** @see Zend_Cache_Exception */
  22. require_once 'Zend/Cache/Exception.php';
  23. /** @see Zend_Cache */
  24. require_once 'Zend/Cache.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Cache
  28. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Cache_Manager
  32. {
  33. /**
  34. * Constant holding reserved name for default Page Cache
  35. */
  36. const PAGECACHE = 'page';
  37. /**
  38. * Constant holding reserved name for default Page Tag Cache
  39. */
  40. const PAGETAGCACHE = 'pagetag';
  41. /**
  42. * Array of caches stored by the Cache Manager instance
  43. *
  44. * @var array
  45. */
  46. protected $_caches = array();
  47. /**
  48. * Array of ready made configuration templates for lazy
  49. * loading caches.
  50. *
  51. * @var array
  52. */
  53. protected $_optionTemplates = array(
  54. // Null Cache (Enforce Null/Empty Values)
  55. 'skeleton' => array(
  56. 'frontend' => array(
  57. 'name' => null,
  58. 'options' => array(),
  59. ),
  60. 'backend' => array(
  61. 'name' => null,
  62. 'options' => array(),
  63. ),
  64. ),
  65. // Simple Common Default
  66. 'default' => array(
  67. 'frontend' => array(
  68. 'name' => 'Core',
  69. 'options' => array(
  70. 'automatic_serialization' => true,
  71. ),
  72. ),
  73. 'backend' => array(
  74. 'name' => 'File',
  75. 'options' => array(
  76. 'cache_dir' => '../cache',
  77. ),
  78. ),
  79. ),
  80. // Static Page HTML Cache
  81. 'page' => array(
  82. 'frontend' => array(
  83. 'name' => 'Capture',
  84. 'options' => array(
  85. 'ignore_user_abort' => true,
  86. ),
  87. ),
  88. 'backend' => array(
  89. 'name' => 'Static',
  90. 'options' => array(
  91. 'public_dir' => '../public',
  92. ),
  93. ),
  94. ),
  95. // Tag Cache
  96. 'pagetag' => array(
  97. 'frontend' => array(
  98. 'name' => 'Core',
  99. 'options' => array(
  100. 'automatic_serialization' => true,
  101. 'lifetime' => null
  102. ),
  103. ),
  104. 'backend' => array(
  105. 'name' => 'File',
  106. 'options' => array(
  107. 'cache_dir' => '../cache',
  108. 'cache_file_umask' => 0644
  109. ),
  110. ),
  111. ),
  112. );
  113. /**
  114. * Set a new cache for the Cache Manager to contain
  115. *
  116. * @param string $name
  117. * @param Zend_Cache_Core $cache
  118. * @return Zend_Cache_Manager
  119. */
  120. public function setCache($name, Zend_Cache_Core $cache)
  121. {
  122. $this->_caches[$name] = $cache;
  123. return $this;
  124. }
  125. /**
  126. * Check if the Cache Manager contains the named cache object, or a named
  127. * configuration template to lazy load the cache object
  128. *
  129. * @param string $name
  130. * @return bool
  131. */
  132. public function hasCache($name)
  133. {
  134. if (isset($this->_caches[$name])
  135. || $this->hasCacheTemplate($name)
  136. ) {
  137. return true;
  138. }
  139. return false;
  140. }
  141. /**
  142. * Fetch the named cache object, or instantiate and return a cache object
  143. * using a named configuration template
  144. *
  145. * @param string $name
  146. * @return Zend_Cache_Core
  147. */
  148. public function getCache($name)
  149. {
  150. if (isset($this->_caches[$name])) {
  151. return $this->_caches[$name];
  152. }
  153. if (isset($this->_optionTemplates[$name])) {
  154. if ($name == self::PAGECACHE
  155. && (!isset($this->_optionTemplates[$name]['backend']['options']['tag_cache'])
  156. || !$this->_optionTemplates[$name]['backend']['options']['tag_cache'] instanceof Zend_Cache_Core)
  157. ) {
  158. $this->_optionTemplates[$name]['backend']['options']['tag_cache']
  159. = $this->getCache(self::PAGETAGCACHE );
  160. }
  161. $this->_caches[$name] = Zend_Cache::factory(
  162. $this->_optionTemplates[$name]['frontend']['name'],
  163. $this->_optionTemplates[$name]['backend']['name'],
  164. isset($this->_optionTemplates[$name]['frontend']['options']) ? $this->_optionTemplates[$name]['frontend']['options'] : array(),
  165. isset($this->_optionTemplates[$name]['backend']['options']) ? $this->_optionTemplates[$name]['backend']['options'] : array()
  166. );
  167. return $this->_caches[$name];
  168. }
  169. }
  170. /**
  171. * Set a named configuration template from which a cache object can later
  172. * be lazy loaded
  173. *
  174. * @param string $name
  175. * @param array $options
  176. * @return Zend_Cache_Manager
  177. */
  178. public function setCacheTemplate($name, $options)
  179. {
  180. if ($options instanceof Zend_Config) {
  181. $options = $options->toArray();
  182. } elseif (!is_array($options)) {
  183. require_once 'Zend/Cache/Exception.php';
  184. throw new Zend_Cache_Exception('Options passed must be in'
  185. . ' an associative array or instance of Zend_Config');
  186. }
  187. $this->_optionTemplates[$name] = $options;
  188. return $this;
  189. }
  190. /**
  191. * Check if the named configuration template
  192. *
  193. * @param string $name
  194. * @return bool
  195. */
  196. public function hasCacheTemplate($name)
  197. {
  198. if (isset($this->_optionTemplates[$name])) {
  199. return true;
  200. }
  201. return false;
  202. }
  203. /**
  204. * Get the named configuration template
  205. *
  206. * @param string $name
  207. * @return array
  208. */
  209. public function getCacheTemplate($name)
  210. {
  211. if (isset($this->_optionTemplates[$name])) {
  212. return $this->_optionTemplates[$name];
  213. }
  214. }
  215. /**
  216. * Pass an array containing changes to be applied to a named
  217. * configuration
  218. * template
  219. *
  220. * @param string $name
  221. * @param array $options
  222. * @return Zend_Cache_Manager
  223. * @throws Zend_Cache_Exception for invalid options format or if option templates do not have $name
  224. */
  225. public function setTemplateOptions($name, $options)
  226. {
  227. if ($options instanceof Zend_Config) {
  228. $options = $options->toArray();
  229. } elseif (!is_array($options)) {
  230. require_once 'Zend/Cache/Exception.php';
  231. throw new Zend_Cache_Exception('Options passed must be in'
  232. . ' an associative array or instance of Zend_Config');
  233. }
  234. if (!isset($this->_optionTemplates[$name])) {
  235. throw new Zend_Cache_Exception('A cache configuration template'
  236. . 'does not exist with the name "' . $name . '"');
  237. }
  238. $this->_optionTemplates[$name]
  239. = $this->_mergeOptions($this->_optionTemplates[$name], $options);
  240. return $this;
  241. }
  242. /**
  243. * Simple method to merge two configuration arrays
  244. *
  245. * @param array $current
  246. * @param array $options
  247. * @return array
  248. */
  249. protected function _mergeOptions(array $current, array $options)
  250. {
  251. if (isset($options['frontend']['name'])) {
  252. $current['frontend']['name'] = $options['frontend']['name'];
  253. }
  254. if (isset($options['backend']['name'])) {
  255. $current['backend']['name'] = $options['backend']['name'];
  256. }
  257. if (isset($options['frontend']['options'])) {
  258. foreach ($options['frontend']['options'] as $key=>$value) {
  259. $current['frontend']['options'][$key] = $value;
  260. }
  261. }
  262. if (isset($options['backend']['options'])) {
  263. foreach ($options['backend']['options'] as $key=>$value) {
  264. $current['backend']['options'][$key] = $value;
  265. }
  266. }
  267. return $current;
  268. }
  269. }