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
* @subpackage Zend_Cache_Frontend
18
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
19
* @license http://framework.zend.com/license/new-bsd New BSD License
23
* @see Zend_Cache_Core
25
require_once 'Zend/Cache/Core.php';
30
* @subpackage Zend_Cache_Frontend
31
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
32
* @license http://framework.zend.com/license/new-bsd New BSD License
34
class Zend_Cache_Frontend_Class extends Zend_Cache_Core
39
* ====> (mixed) cached_entity :
40
* - if set to a class name, we will cache an abstract class and will use only static calls
41
* - if set to an object, we will cache this object methods
43
* ====> (boolean) cache_by_default :
44
* - if true, method calls will be cached by default
46
* ====> (array) cached_methods :
47
* - an array of method names which will be cached (even if cache_by_default = false)
49
* ====> (array) non_cached_methods :
50
* - an array of method names which won't be cached (even if cache_by_default = true)
52
* @var array available options
54
protected $_specificOptions = array(
55
'cached_entity' => null,
56
'cache_by_default' => true,
57
'cached_methods' => array(),
58
'non_cached_methods' => array()
66
private $_tags = array();
69
* SpecificLifetime value
71
* false => no specific life time
75
private $_specificLifetime = false;
78
* The cached object or the name of the cached abstract class
82
private $_cachedEntity = null;
85
* The class name of the cached object or cached abstract class
87
* Used to differentiate between different classes with the same method calls.
91
private $_cachedEntityLabel = '';
94
* Priority (used by some particular backends)
98
private $_priority = 8;
103
* @param array $options Associative array of options
104
* @throws Zend_Cache_Exception
107
public function __construct(array $options = array())
109
while (list($name, $value) = each($options)) {
110
$this->setOption($name, $value);
112
if ($this->_specificOptions['cached_entity'] === null) {
113
Zend_Cache::throwException('cached_entity must be set !');
115
$this->setCachedEntity($this->_specificOptions['cached_entity']);
116
$this->setOption('automatic_serialization', true);
120
* Set a specific life time
122
* @param int $specificLifetime
125
public function setSpecificLifetime($specificLifetime = false)
127
$this->_specificLifetime = $specificLifetime;
131
* Set the priority (used by some particular backends)
133
* @param int $priority integer between 0 (very low priority) and 10 (maximum priority)
135
public function setPriority($priority)
137
$this->_priority = $priority;
141
* Public frontend to set an option
143
* Just a wrapper to get a specific behaviour for cached_entity
145
* @param string $name Name of the option
146
* @param mixed $value Value of the option
147
* @throws Zend_Cache_Exception
150
public function setOption($name, $value)
152
if ($name == 'cached_entity') {
153
$this->setCachedEntity($value);
155
parent::setOption($name, $value);
160
* Specific method to set the cachedEntity
162
* if set to a class name, we will cache an abstract class and will use only static calls
163
* if set to an object, we will cache this object methods
165
* @param mixed $cachedEntity
167
public function setCachedEntity($cachedEntity)
169
if (!is_string($cachedEntity) && !is_object($cachedEntity)) {
170
Zend_Cache::throwException('cached_entity must be an object or a class name');
172
$this->_cachedEntity = $cachedEntity;
173
$this->_specificOptions['cached_entity'] = $cachedEntity;
174
if (is_string($this->_cachedEntity)){
175
$this->_cachedEntityLabel = $this->_cachedEntity;
177
$ro = new ReflectionObject($this->_cachedEntity);
178
$this->_cachedEntityLabel = $ro->getName();
183
* Set the cache array
188
public function setTagsArray($tags = array())
190
$this->_tags = $tags;
194
* Main method : call the specified method or get the result from cache
196
* @param string $name Method name
197
* @param array $parameters Method parameters
198
* @return mixed Result
200
public function __call($name, $parameters)
202
$cacheBool1 = $this->_specificOptions['cache_by_default'];
203
$cacheBool2 = in_array($name, $this->_specificOptions['cached_methods']);
204
$cacheBool3 = in_array($name, $this->_specificOptions['non_cached_methods']);
205
$cache = (($cacheBool1 || $cacheBool2) && (!$cacheBool3));
207
// We do not have not cache
208
return call_user_func_array(array($this->_cachedEntity, $name), $parameters);
210
$id = $this->_makeId($name, $parameters);
211
if ($this->test($id)) {
212
// A cache is available
213
$result = $this->load($id);
214
$output = $result[0];
215
$return = $result[1];
217
// A cache is not available
219
ob_implicit_flush(false);
220
$return = call_user_func_array(array($this->_cachedEntity, $name), $parameters);
221
$output = ob_get_contents();
223
$data = array($output, $return);
224
$this->save($data, $id, $this->_tags, $this->_specificLifetime, $this->_priority);
231
* Make a cache id from the method name and parameters
233
* @param string $name Method name
234
* @param array $parameters Method parameters
235
* @return string Cache id
237
private function _makeId($name, $parameters)
239
return md5($this->_cachedEntityLabel . '__' . $name . '__' . serialize($parameters));