Class.php
5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Cache
* @subpackage Frontend
* @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/**
* Zend_Cache_Core
*/
require_once 'Zend/Cache/Core.php';
/**
* @package Zend_Cache
* @subpackage Frontend
* @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Cache_Frontend_Class extends Zend_Cache_Core
{
// ------------------
// --- Properties ---
// ------------------
/**
* Available options
*
* ====> (mixed) cachedEntity :
* - if set to a class name, we will cache an abstract class and will use only static calls
* - if set to an object, we will cache this object methods
*
* ====> (boolean) cacheByDefault :
* - if true, method calls will be cached by default
*
* ====> (array) cachedMethods :
* - an array of method names which will be cached (even if cacheByDefault = false)
*
* ====> (array) nonCachedMethods :
* - an array of method names which won't be cached (even if cacheByDefault = true)
*
* @var array available options
*/
protected $_specificOptions = array(
'cachedEntity' => null,
'cacheByDefault' => true,
'cachedMethods' => array(),
'nonCachedMethods' => array()
);
/**
* Tags array
*
* @var array
*/
private $_tags = array();
/**
* SpecificLifetime value
*
* false => no specific life time
*
* @var int
*/
private $_specificLifetime = false;
/**
* The cached object or the name of the cached abstract class
*
* @var mixed
*/
private $_cachedEntity = null;
// ----------------------
// --- Public methods ---
// ----------------------
/**
* Constructor
*
* @param array $options associative array of options
*/
public function __construct($options = array())
{
if (!isset($options['cachedEntity'])) {
Zend_Cache::throwException('cachedEntity must be set !');
} else {
if (!is_string($options['cachedEntity']) && !is_object($options['cachedEntity'])) {
Zend_Cache::throwException('cachedEntity must be an object or a class name');
}
}
$this->_cachedEntity = $options['cachedEntity'];
while (list($name, $value) = each($options)) {
$this->setOption($name, $value);
}
$this->setOption('automaticSerialization', true);
}
/**
* Set a specific life time
*
* @param int $specificLifetime
*/
public function setSpecificLifetime($specificLifetime = false)
{
$this->_specificLifetime = $specificLifetime;
}
/**
* Set the cache array
*
* @param array $tags
*/
public function setTagsArray($tags = array())
{
$this->_tags = $tags;
}
/**
* Main method : call the specified method or get the result from cache
*
* @param string $name method name
* @param array $parameters method parameters
* @return mixed result
*/
public function __call($name, $parameters)
{
$cacheBool1 = $this->_specificOptions['cacheByDefault'];
$cacheBool2 = in_array($name, $this->_specificOptions['cachedMethods']);
$cacheBool3 = in_array($name, $this->_specificOptions['nonCachedMethods']);
$cache = (($cacheBool1 || $cacheBool2) && (!$cacheBool3));
if (!$cache) {
// We do not have not cache
return call_user_func_array(array($this->_cachedEntity, $name), $parameters);
}
$id = $this->_makeId($name, $parameters);
if ($this->test($id)) {
// A cache is available
$result = $this->load($id);
$output = $result[0];
$return = $result[1];
} else {
// A cache is not available
ob_start();
ob_implicit_flush(false);
$return = call_user_func_array(array($this->_cachedEntity, $name), $parameters);
$output = ob_get_contents();
ob_end_clean();
$data = array($output, $return);
$this->save($data, $id, $this->_tags, $this->_specificLifetime);
}
echo $output;
return $return;
}
// ------------------------------------
// --- Private or protected methods ---
// ------------------------------------
/**
* Make a cache id from the method name and parameters
*
* @param string $name method name
* @param array $parameters method parameters
* @return string cache id
*/
private function _makeId($name, $parameters)
{
return md5($name . serialize($parameters));
}
}