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.
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
22 /** @see Zend_Cache_Exception */
23 require_once 'Zend/Cache/Exception.php';
25 /** @see Zend_Cache */
26 require_once 'Zend/Cache.php';
31 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
32 * @license http://framework.zend.com/license/new-bsd New BSD License
34 class Zend_Cache_Manager
37 * Constant holding reserved name for default Page Cache
39 const PAGECACHE
= 'page';
42 * Constant holding reserved name for default Page Tag Cache
44 const PAGETAGCACHE
= 'pagetag';
47 * Array of caches stored by the Cache Manager instance
51 protected $_caches = array();
54 * Array of ready made configuration templates for lazy
59 protected $_optionTemplates = array(
60 // Null Cache (Enforce Null/Empty Values)
71 // Simple Common Default
76 'automatic_serialization' => true,
82 'cache_dir' => '../cache',
86 // Static Page HTML Cache
91 'ignore_user_abort' => true,
97 'public_dir' => '../public',
106 'automatic_serialization' => true,
113 'cache_dir' => '../cache',
114 'cache_file_umask' => 0644
121 * Set a new cache for the Cache Manager to contain
123 * @param string $name
124 * @param Zend_Cache_Core $cache
125 * @return Zend_Cache_Manager
127 public function setCache($name, Zend_Cache_Core
$cache)
129 $this->_caches
[$name] = $cache;
134 * Check if the Cache Manager contains the named cache object, or a named
135 * configuration template to lazy load the cache object
137 * @param string $name
140 public function hasCache($name)
142 if (isset($this->_caches
[$name])
143 ||
$this->hasCacheTemplate($name)
151 * Fetch the named cache object, or instantiate and return a cache object
152 * using a named configuration template
154 * @param string $name
155 * @return Zend_Cache_Core
157 public function getCache($name)
159 if (isset($this->_caches
[$name])) {
160 return $this->_caches
[$name];
162 if (isset($this->_optionTemplates
[$name])) {
163 if ($name == self
::PAGECACHE
164 && (!isset($this->_optionTemplates
[$name]['backend']['options']['tag_cache'])
165 ||
!$this->_optionTemplates
[$name]['backend']['options']['tag_cache'] instanceof Zend_Cache_Core
)
167 $this->_optionTemplates
[$name]['backend']['options']['tag_cache']
168 = $this->getCache(self
::PAGETAGCACHE
);
170 $this->_caches
[$name] = Zend_Cache
::factory(
171 $this->_optionTemplates
[$name]['frontend']['name'],
172 $this->_optionTemplates
[$name]['backend']['name'],
173 isset($this->_optionTemplates
[$name]['frontend']['options']) ?
$this->_optionTemplates
[$name]['frontend']['options'] : array(),
174 isset($this->_optionTemplates
[$name]['backend']['options']) ?
$this->_optionTemplates
[$name]['backend']['options'] : array(),
175 isset($this->_optionTemplates
[$name]['frontend']['customFrontendNaming']) ?
$this->_optionTemplates
[$name]['frontend']['customFrontendNaming'] : false,
176 isset($this->_optionTemplates
[$name]['backend']['customBackendNaming']) ?
$this->_optionTemplates
[$name]['backend']['customBackendNaming'] : false,
177 isset($this->_optionTemplates
[$name]['frontendBackendAutoload']) ?
$this->_optionTemplates
[$name]['frontendBackendAutoload'] : false
179 return $this->_caches
[$name];
184 * Set a named configuration template from which a cache object can later
187 * @param string $name
188 * @param array $options
189 * @return Zend_Cache_Manager
191 public function setCacheTemplate($name, $options)
193 if ($options instanceof Zend_Config
) {
194 $options = $options->toArray();
195 } elseif (!is_array($options)) {
196 require_once 'Zend/Cache/Exception.php';
197 throw new Zend_Cache_Exception('Options passed must be in'
198 . ' an associative array or instance of Zend_Config');
200 $this->_optionTemplates
[$name] = $options;
205 * Check if the named configuration template
207 * @param string $name
210 public function hasCacheTemplate($name)
212 if (isset($this->_optionTemplates
[$name])) {
219 * Get the named configuration template
221 * @param string $name
224 public function getCacheTemplate($name)
226 if (isset($this->_optionTemplates
[$name])) {
227 return $this->_optionTemplates
[$name];
232 * Pass an array containing changes to be applied to a named
236 * @param string $name
237 * @param array $options
238 * @return Zend_Cache_Manager
239 * @throws Zend_Cache_Exception for invalid options format or if option templates do not have $name
241 public function setTemplateOptions($name, $options)
243 if ($options instanceof Zend_Config
) {
244 $options = $options->toArray();
245 } elseif (!is_array($options)) {
246 require_once 'Zend/Cache/Exception.php';
247 throw new Zend_Cache_Exception('Options passed must be in'
248 . ' an associative array or instance of Zend_Config');
250 if (!isset($this->_optionTemplates
[$name])) {
251 throw new Zend_Cache_Exception('A cache configuration template'
252 . 'does not exist with the name "' . $name . '"');
254 $this->_optionTemplates
[$name]
255 = $this->_mergeOptions($this->_optionTemplates
[$name], $options);
260 * Simple method to merge two configuration arrays
262 * @param array $current
263 * @param array $options
266 protected function _mergeOptions(array $current, array $options)
268 if (isset($options['frontend']['name'])) {
269 $current['frontend']['name'] = $options['frontend']['name'];
271 if (isset($options['backend']['name'])) {
272 $current['backend']['name'] = $options['backend']['name'];
274 if (isset($options['frontend']['options'])) {
275 foreach ($options['frontend']['options'] as $key=>$value) {
276 $current['frontend']['options'][$key] = $value;
279 if (isset($options['backend']['options'])) {
280 foreach ($options['backend']['options'] as $key=>$value) {
281 $current['backend']['options'][$key] = $value;