13 * to license@zend.com so we can send you a copy immediately. |
13 * to license@zend.com so we can send you a copy immediately. |
14 * |
14 * |
15 * @category Zend |
15 * @category Zend |
16 * @package Zend_Cache |
16 * @package Zend_Cache |
17 * @subpackage Zend_Cache_Frontend |
17 * @subpackage Zend_Cache_Frontend |
18 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
18 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) |
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
20 * @version $Id: Class.php 24593 2012-01-05 20:35:02Z matthew $ |
20 * @version $Id$ |
21 */ |
21 */ |
22 |
22 |
23 /** |
23 /** |
24 * @see Zend_Cache_Core |
24 * @see Zend_Cache_Core |
25 */ |
25 */ |
51 * - an array of method names which won't be cached (even if cache_by_default = true) |
51 * - an array of method names which won't be cached (even if cache_by_default = true) |
52 * |
52 * |
53 * @var array available options |
53 * @var array available options |
54 */ |
54 */ |
55 protected $_specificOptions = array( |
55 protected $_specificOptions = array( |
56 'cached_entity' => null, |
56 'cached_entity' => null, |
57 'cache_by_default' => true, |
57 'cache_by_default' => true, |
58 'cached_methods' => array(), |
58 'cached_methods' => array(), |
59 'non_cached_methods' => array() |
59 'non_cached_methods' => array() |
60 ); |
60 ); |
61 |
61 |
62 /** |
62 /** |
63 * Tags array |
63 * Tags array |
64 * |
64 * |
65 * @var array |
65 * @var array |
66 */ |
66 */ |
67 private $_tags = array(); |
67 protected $_tags = array(); |
68 |
68 |
69 /** |
69 /** |
70 * SpecificLifetime value |
70 * SpecificLifetime value |
71 * |
71 * |
72 * false => no specific life time |
72 * false => no specific life time |
73 * |
73 * |
74 * @var int |
74 * @var bool|int |
75 */ |
75 */ |
76 private $_specificLifetime = false; |
76 protected $_specificLifetime = false; |
77 |
77 |
78 /** |
78 /** |
79 * The cached object or the name of the cached abstract class |
79 * The cached object or the name of the cached abstract class |
80 * |
80 * |
81 * @var mixed |
81 * @var mixed |
82 */ |
82 */ |
83 private $_cachedEntity = null; |
83 protected $_cachedEntity = null; |
84 |
84 |
85 /** |
85 /** |
86 * The class name of the cached object or cached abstract class |
86 * The class name of the cached object or cached abstract class |
87 * |
87 * |
88 * Used to differentiate between different classes with the same method calls. |
88 * Used to differentiate between different classes with the same method calls. |
89 * |
89 * |
90 * @var string |
90 * @var string |
91 */ |
91 */ |
92 private $_cachedEntityLabel = ''; |
92 protected $_cachedEntityLabel = ''; |
93 |
93 |
94 /** |
94 /** |
95 * Priority (used by some particular backends) |
95 * Priority (used by some particular backends) |
96 * |
96 * |
97 * @var int |
97 * @var int |
98 */ |
98 */ |
99 private $_priority = 8; |
99 protected $_priority = 8; |
100 |
100 |
101 /** |
101 /** |
102 * Constructor |
102 * Constructor |
103 * |
103 * |
104 * @param array $options Associative array of options |
104 * @param array $options Associative array of options |
105 * @throws Zend_Cache_Exception |
105 * @throws Zend_Cache_Exception |
106 * @return void |
|
107 */ |
106 */ |
108 public function __construct(array $options = array()) |
107 public function __construct(array $options = array()) |
109 { |
108 { |
110 while (list($name, $value) = each($options)) { |
109 foreach ($options as $name => $value) { |
111 $this->setOption($name, $value); |
110 $this->setOption($name, $value); |
112 } |
111 } |
113 if ($this->_specificOptions['cached_entity'] === null) { |
112 if ($this->_specificOptions['cached_entity'] === null) { |
114 Zend_Cache::throwException('cached_entity must be set !'); |
113 Zend_Cache::throwException('cached_entity must be set !'); |
115 } |
114 } |
166 * @param mixed $cachedEntity |
165 * @param mixed $cachedEntity |
167 */ |
166 */ |
168 public function setCachedEntity($cachedEntity) |
167 public function setCachedEntity($cachedEntity) |
169 { |
168 { |
170 if (!is_string($cachedEntity) && !is_object($cachedEntity)) { |
169 if (!is_string($cachedEntity) && !is_object($cachedEntity)) { |
171 Zend_Cache::throwException('cached_entity must be an object or a class name'); |
170 Zend_Cache::throwException( |
172 } |
171 'cached_entity must be an object or a class name' |
173 $this->_cachedEntity = $cachedEntity; |
172 ); |
|
173 } |
|
174 |
|
175 $this->_cachedEntity = $cachedEntity; |
174 $this->_specificOptions['cached_entity'] = $cachedEntity; |
176 $this->_specificOptions['cached_entity'] = $cachedEntity; |
175 if (is_string($this->_cachedEntity)){ |
177 |
|
178 if (is_string($this->_cachedEntity)) { |
176 $this->_cachedEntityLabel = $this->_cachedEntity; |
179 $this->_cachedEntityLabel = $this->_cachedEntity; |
177 } else { |
180 } else { |
178 $ro = new ReflectionObject($this->_cachedEntity); |
181 $ro = new ReflectionObject($this->_cachedEntity); |
179 $this->_cachedEntityLabel = $ro->getName(); |
182 $this->_cachedEntityLabel = $ro->getName(); |
180 } |
183 } |
195 * Main method : call the specified method or get the result from cache |
198 * Main method : call the specified method or get the result from cache |
196 * |
199 * |
197 * @param string $name Method name |
200 * @param string $name Method name |
198 * @param array $parameters Method parameters |
201 * @param array $parameters Method parameters |
199 * @return mixed Result |
202 * @return mixed Result |
|
203 * @throws Exception |
200 */ |
204 */ |
201 public function __call($name, $parameters) |
205 public function __call($name, $parameters) |
202 { |
206 { |
203 $callback = array($this->_cachedEntity, $name); |
207 $callback = array($this->_cachedEntity, $name); |
204 |
208 |
207 } |
211 } |
208 |
212 |
209 $cacheBool1 = $this->_specificOptions['cache_by_default']; |
213 $cacheBool1 = $this->_specificOptions['cache_by_default']; |
210 $cacheBool2 = in_array($name, $this->_specificOptions['cached_methods']); |
214 $cacheBool2 = in_array($name, $this->_specificOptions['cached_methods']); |
211 $cacheBool3 = in_array($name, $this->_specificOptions['non_cached_methods']); |
215 $cacheBool3 = in_array($name, $this->_specificOptions['non_cached_methods']); |
212 $cache = (($cacheBool1 || $cacheBool2) && (!$cacheBool3)); |
216 $cache = (($cacheBool1 || $cacheBool2) && (!$cacheBool3)); |
|
217 |
213 if (!$cache) { |
218 if (!$cache) { |
214 // We do not have not cache |
219 // We do not have not cache |
215 return call_user_func_array($callback, $parameters); |
220 return call_user_func_array($callback, $parameters); |
216 } |
221 } |
217 |
222 |
218 $id = $this->_makeId($name, $parameters); |
223 $id = $this->makeId($name, $parameters); |
219 if ( ($rs = $this->load($id)) && isset($rs[0], $rs[1]) ) { |
224 if (($rs = $this->load($id)) && (array_key_exists(0, $rs)) |
|
225 && (array_key_exists(1, $rs)) |
|
226 ) { |
220 // A cache is available |
227 // A cache is available |
221 $output = $rs[0]; |
228 $output = $rs[0]; |
222 $return = $rs[1]; |
229 $return = $rs[1]; |
223 } else { |
230 } else { |
224 // A cache is not available (or not valid for this frontend) |
231 // A cache is not available (or not valid for this frontend) |
226 ob_implicit_flush(false); |
233 ob_implicit_flush(false); |
227 |
234 |
228 try { |
235 try { |
229 $return = call_user_func_array($callback, $parameters); |
236 $return = call_user_func_array($callback, $parameters); |
230 $output = ob_get_clean(); |
237 $output = ob_get_clean(); |
231 $data = array($output, $return); |
238 $data = array($output, $return); |
232 $this->save($data, $id, $this->_tags, $this->_specificLifetime, $this->_priority); |
239 |
|
240 $this->save( |
|
241 $data, $id, $this->_tags, $this->_specificLifetime, |
|
242 $this->_priority |
|
243 ); |
233 } catch (Exception $e) { |
244 } catch (Exception $e) { |
234 ob_end_clean(); |
245 ob_end_clean(); |
235 throw $e; |
246 throw $e; |
236 } |
247 } |
237 } |
248 } |