ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
CachedClient.php
Go to the documentation of this file.
1<?php
2
3/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
4
5// LICENSE AGREEMENT. If folded, press za here to unfold and read license {{{
6
40// }}}
41
42// dependencies {{{
43require_once('Cache/Lite.php');
44require_once('XML/RPC2/Exception.php');
45// }}}
46
58
59 // {{{ properties
60
66 private $_options;
67
73 private $_uri;
74
80 private $_debug = false;
81
87 private $_cacheOptions = array();
88
99 private $_cachedMethods = array();
100
108 private $_notCachedMethods = array();
109
115 private $_cacheByDefault = true;
116
122 private $_cacheObject = null;
123
129 private $_clientObject = null;
130
136 private $_defaultCacheGroup = 'xml_rpc2_client';
137
143 private $_cacheDebug = false;
144
145 // }}}
146 // {{{ constructor
147
156 protected function __construct($uri, $options = array())
157 {
158 if (isset($options['cacheOptions'])) {
159 $array = $options['cacheOptions'];
160 if (isset($array['defaultCacheGroup'])) {
161 $this->_defaultCacheGroup = $array['defaultCacheGroup'];
162 unset($array['defaultCacheGroup']); // this is a "non standard" option for Cache_Lite
163 }
164 if (isset($array['cachedMethods'])) {
165 $this->_cachedMethods = $array['cachedMethods'];
166 unset($array['cachedMethods']); // this is a "non standard" option for Cache_Lite
167 }
168 if (isset($array['notCachedMethods'])) {
169 $this->_notCachedMethods = $array['notCachedMethods'];
170 unset($array['notCachedMethods']); // this is a "non standard" option for Cache_Lite
171 }
172 if (isset($array['cacheByDefault'])) {
173 $this->_cacheByDefault = $array['cacheByDefault'];
174 unset($array['CacheByDefault']); // this is a "non standard" option for Cache_Lite
175 }
176 $array['automaticSerialization'] = false; // datas are already serialized in this class
177 if (!isset($array['lifetime'])) {
178 $array['lifetime'] = 3600; // we need a default lifetime
179 }
180 unset($options['cacheOptions']); // this is a "non standard" option for XML/RPC2/Client
181 } else { // no cache options ?
182 $array = array(
183 'lifetime' => 3600, // we need a default lifetime
184 'automaticSerialization' => false // datas are already serialized in this class
185 );
186 }
187 if (isset($options['cacheDebug'])) {
188 $this->_cacheDebug = $options['cacheDebug'];
189 unset($options['cacheDebug']); // this a "non standard" option for XML/RPC2/Client
190 }
191 $this->_cacheOptions = $array;
192 $this->_cacheObject = new Cache_Lite($this->_cacheOptions);
193 $this->_options = $options;
194 $this->_uri = $uri;
195 }
196
197 // }}}
198 // {{{ create()
199
210 public static function create($uri, $options = array())
211 {
212 return new XML_RPC2_CachedClient($uri, $options);
213 }
214
215 // }}}
216 // {{{ __call()
217
230 public function __call($methodName, $parameters)
231 {
232 if (!isset($this->_cacheObject)) {
233 $this->_cacheObject = new Cache_Lite($this->_cacheOptions);
234 }
235 if (in_array($methodName, $this->_notCachedMethods)) {
236 // if the called method is listed in _notCachedMethods => no cache
237 if ($this->_cacheDebug) {
238 print "CACHE DEBUG : the called method is listed in _notCachedMethods => no cache !\n";
239 }
240 return $this->_workWithoutCache___($methodName, $parameters);
241 }
242 if (!($this->_cacheByDefault)) {
243 if ((!(isset($this->_cachedMethods[$methodName]))) and (!(in_array($methodName, $this->_cachedMethods)))) {
244 // if cache is not on by default and if the called method is not described in _cachedMethods array
245 // => no cache
246 if ($this->_cacheDebug) {
247 print "CACHE DEBUG : cache is not on by default and the called method is not listed in _cachedMethods => no cache !\n";
248 }
249 return $this->_workWithoutCache___($methodName, $parameters);
250 }
251 }
252 if (isset($this->_cachedMethods[$methodName])) {
253 if ($this->_cachedMethods[$methodName] == -1) {
254 // if a method is described with a lifetime value of -1 => no cache
255 if ($this->_cacheDebug) {
256 print "CACHE DEBUG : called method has a -1 lifetime value => no cache !\n";
257 }
258 return $this->_workWithoutCache___($methodName, $parameters);
259 }
260 // if a method is described with a specific (and <> -1) lifetime
261 // => we fix this new lifetime
262 $this->_cacheObject->setLifetime($this->_cachedMethods[$methodName]);
263 } else {
264 // there is no specific lifetime, let's use the default one
265 $this->_cacheObject->setLifetime($this->_cacheOptions['lifetime']);
266 }
267 $cacheId = $this->_makeCacheId___($methodName, $parameters);
268 $data = $this->_cacheObject->get($cacheId, $this->_defaultCacheGroup);
269 if (is_string($data)) {
270 // cache is hit !
271 if ($this->_cacheDebug) {
272 print "CACHE DEBUG : cache is hit !\n";
273 }
274 return unserialize($data);
275 }
276 // the cache is not hit, let's call the "real" XML_RPC client
277 if ($this->_cacheDebug) {
278 print "CACHE DEBUG : cache is not hit !\n";
279 }
280 $result = $this->_workWithoutCache___($methodName, $parameters);
281 $this->_cacheObject->save(serialize($result)); // save in cache for next time...
282 return $result;
283 }
284
285 // }}}
286 // {{{ _workWithoutCache___()
287
298 private function _workWithoutCache___($methodName, $parameters)
299 {
300 if (!(isset($this->_clientObject))) {
301 // If the XML_RPC2_Client object is not available, let's build it
302 require_once('XML/RPC2/Client.php');
303 $this->_clientObject = XML_RPC2_Client::create($this->_uri, $this->_options);
304 }
305 // the real function call...
306 return call_user_func_array(array($this->_clientObject, $methodName), $parameters);
307 }
308
309 // }}}
310 // {{{ _makeCacheId___()
311
322 private function _makeCacheId___($methodName, $parameters)
323 {
324 return md5($methodName . serialize($parameters) . serialize($this->_uri) . serialize($this->_options));
325 }
326
327 // }}}
328 // {{{ dropCacheFile___()
329
339 public function dropCacheFile___($methodName, $parameters)
340 {
341 $id = $this->_makeCacheId___($methodName, $parameters);
342 $this->_cacheObject->remove($id, $this->_defaultCacheGroup);
343 }
344
345 // }}}
346 // {{{ clean___()
347
354 public function clean___()
355 {
356 $this->_cacheObject->clean($this->_defaultCacheGroup, 'ingroup');
357 }
358
359}
360
361?>
$result
static create($uri, $options=array())
"Emulated Factory" method to get the same API than XML_RPC2_Client class
__call($methodName, $parameters)
__call Catchall
_makeCacheId___($methodName, $parameters)
make a cache id depending on method called (and corresponding parameters) but depending on "environne...
_workWithoutCache___($methodName, $parameters)
Do the real call if no cache available.
__construct($uri, $options=array())
Constructor.
dropCacheFile___($methodName, $parameters)
Drop the cache file corresponding to the given method call.
clean___()
Clean all the cache.
static create($uri, $options=array())
Factory method to select, create and return a XML_RPC2_Client backend.
Definition: Client.php:205
$data
if(! $in) print
if(!is_array($argv)) $options