Source for file Cache.class.php

Documentation is available at Cache.class.php

  1. <?php
  2. /**
  3. * XOAD_Cache file.
  4. *
  5. * <p>This file defines the {@link XOAD_Cache} Class.</p>
  6. * <p>Example:</p>
  7. * <code>
  8. * <?php
  9. *
  10. * XOAD_Cache::allowCaching(null, null, 30);
  11. *
  12. * ?>
  13. * </code>
  14. *
  15. * @author Stanimir Angeloff
  16. *
  17. * @package XOAD
  18. *
  19. * @subpackage XOAD_Cache
  20. *
  21. * @version 0.6.0.0
  22. *
  23. */
  24.  
  25. /**
  26. * Holds information about the classes and methods
  27. * that are allowed for caching.
  28. */
  29. $GLOBALS['_XOAD_CACHE_DATA'] = array();
  30.  
  31. /**
  32. * XOAD_Cache Class.
  33. *
  34. * <p>This class allows you to cache the callbacks to
  35. * the server. By default the caching is disabled and
  36. * you must call {@link XOAD_Cache::allowCaching} to
  37. * configure which classes and methods will be cached.</p>
  38. * <p>Example:</p>
  39. * <code>
  40. * <?php
  41. *
  42. * XOAD_Cache::allowCaching('ExampleClass', 'invokeMethod', 30);
  43. *
  44. * ?>
  45. * </code>
  46. *
  47. * @author Stanimir Angeloff
  48. *
  49. * @package XOAD
  50. *
  51. * @subpackage XOAD_Cache
  52. *
  53. * @version 0.6.0.0
  54. *
  55. */
  56. class XOAD_Cache
  57. {
  58. /**
  59. * Retrieves an instanse to the configurated XOAD_Cache storage provider.
  60. *
  61. * @access private
  62. *
  63. * @return object Singleton {@link XOAD_Cache_Storage} inherited class based
  64. * on the configuration (see {@link XOAD_CACHE_STORAGE_DSN}).
  65. *
  66. * @static
  67. *
  68. */
  69. function &getStorage()
  70. {
  71. static $instance;
  72.  
  73. if ( ! isset($instance)) {
  74.  
  75. $className = null;
  76.  
  77. $classParameters = null;
  78.  
  79. $separator = '://';
  80.  
  81. $position = strpos(XOAD_CACHE_STORAGE_DSN, $separator);
  82.  
  83. if ($position === false) {
  84.  
  85. $className = XOAD_CACHE_STORAGE_DSN;
  86.  
  87. } else {
  88.  
  89. $className = substr(XOAD_CACHE_STORAGE_DSN, 0, $position);
  90.  
  91. $classParameters = substr(XOAD_CACHE_STORAGE_DSN, $position + strlen($separator));
  92. }
  93.  
  94. if (empty($className)) {
  95.  
  96. return null;
  97. }
  98.  
  99. $fileName = XOAD_CACHE_BASE . '/classes/storage/' . $className . '.class.php';
  100.  
  101. /**
  102. * Load the file that defines the {@link XOAD_Cache_Storage} class.
  103. */
  104. require_once(XOAD_CACHE_BASE . '/classes/Storage.class.php');
  105.  
  106. /**
  107. * Load the file that defines the cache storage provider.
  108. */
  109. require_once($fileName);
  110.  
  111. $realClassName = 'XOAD_Cache_Storage_' . $className;
  112.  
  113. if ( ! class_exists($realClassName)) {
  114.  
  115. return null;
  116. }
  117.  
  118. $instance = new $realClassName($classParameters);
  119. }
  120.  
  121. return $instance;
  122. }
  123.  
  124. /**
  125. * This method is called on every request to the server.
  126. *
  127. * <p>If the request matches the configurated criterias for
  128. * caching this method will call {@link XOAD_Cache::dispatch}.
  129. *
  130. * @access public
  131. *
  132. * @param array $request The data that is associated with the
  133. * callback.
  134. *
  135. * @return bool True if the request is cached, false otherwise.
  136. *
  137. * @static
  138. *
  139. */
  140. function initialize(&$request)
  141. {
  142. if ( ! empty($GLOBALS['_XOAD_CACHE_DATA'])) {
  143.  
  144. foreach ($GLOBALS['_XOAD_CACHE_DATA'] as $key => $cacheData) {
  145.  
  146. $match = (
  147. (strcasecmp($request['className'], $cacheData['className']) == 0) ||
  148. ($cacheData['className'] === null));
  149.  
  150. $match &= (
  151. (strcasecmp($request['method'], $cacheData['method']) == 0) ||
  152. ($cacheData['method'] === null));
  153.  
  154. if ($match) {
  155.  
  156. return XOAD_Cache::dispatch($request, $cacheData);
  157. }
  158. }
  159. }
  160.  
  161. return false;
  162. }
  163.  
  164. /**
  165. * This method is called when the request matches the configurated
  166. * criterias for caching.
  167. *
  168. * <p>If the request is cached and it's not expired then the cached
  169. * response is used, otherwise the server dispatches the call
  170. * and the response is cached.</p>
  171. *
  172. * @access public
  173. *
  174. * @param array $request The data that is associated with the
  175. * callback.
  176. *
  177. * @param array $cacheData The data that is associated with the
  178. * caching criteria.
  179. *
  180. * @return bool True if the request is cached, false otherwise.
  181. *
  182. * @static
  183. *
  184. */
  185. function dispatch(&$request, &$cacheData)
  186. {
  187. $cacheId = 'c:';
  188. $cacheId .= strtolower($request['className']);
  189. $cacheId .= '__m:';
  190. $cacheId .= strtolower($request['method']);
  191.  
  192. if ($cacheData['arguments'] !== null) {
  193.  
  194. $argumentsList =& $cacheData['arguments'];
  195.  
  196. } else {
  197.  
  198. $argumentsList = array_keys($request['arguments']);
  199. }
  200.  
  201. $cacheId .= '__a:';
  202.  
  203. if ( ! empty($argumentsList)) {
  204.  
  205. foreach ($argumentsList as $key) {
  206.  
  207. if (array_key_exists($key, $request['arguments'])) {
  208.  
  209. $cacheId .= serialize($request['arguments'][$key]);
  210. }
  211. }
  212. }
  213.  
  214. $cacheId .= '__v:';
  215.  
  216. if ($cacheData['members'] !== null) {
  217.  
  218. $objectVars = array_map('strtolower', get_object_vars($request['source']));
  219.  
  220. foreach ($cacheData['members'] as $key) {
  221.  
  222. if (array_key_exists(strtolower($key), $objectVars)) {
  223.  
  224. $cacheId .= serialize($request['source']->$key);
  225. }
  226. }
  227. }
  228.  
  229. $storage =& XOAD_Cache::getStorage();
  230.  
  231. $cacheId = $storage->generateID($cacheId);
  232.  
  233. $storage->collectGarbage();
  234.  
  235. $cachedResponse = $storage->load($cacheId);
  236.  
  237. if ($cachedResponse != null) {
  238.  
  239. print $cachedResponse;
  240.  
  241. return true;
  242.  
  243. } else {
  244.  
  245. $GLOBALS['_XOAD_CACHE_ARGUMENTS'] = array(
  246. 'id' => $cacheId,
  247. 'data' => $cacheData
  248. );
  249.  
  250. /**
  251. * Defines a constant that is used to tell the server observer
  252. * to call {@link XOAD_Cache::cacheRequest} when the request
  253. * is dispatched.
  254. */
  255. define('XOAD_CACHE_REQUEST', true);
  256. }
  257.  
  258. return false;
  259. }
  260.  
  261. /**
  262. * This method is called when the request matches the configurated
  263. * criterias for caching, but there is no data in the cache.
  264. *
  265. * @access public
  266. *
  267. * @param array $request The data that is associated with the
  268. * callback.
  269. *
  270. * @param array $response The data that is associated with the
  271. * response.
  272. *
  273. * @return bool True if the request is cached, false otherwise.
  274. *
  275. * @static
  276. *
  277. */
  278. function cacheRequest(&$request, &$response)
  279. {
  280. if ( ! array_key_exists('_XOAD_CACHE_ARGUMENTS', $GLOBALS)) {
  281.  
  282. return false;
  283. }
  284.  
  285. $storage =& XOAD_Cache::getStorage();
  286.  
  287. $cacheId =& $GLOBALS['_XOAD_CACHE_ARGUMENTS']['id'];
  288. $cacheData =& $GLOBALS['_XOAD_CACHE_ARGUMENTS']['data'];
  289.  
  290. $cacheResponse = XOAD_Client::register($response);
  291.  
  292. return $storage->save($cacheId, $cacheData['expire'], $cacheResponse);
  293. }
  294.  
  295. /**
  296. * Installs a new caching criteria.
  297. *
  298. * <p>By default the caching is disabled and you must call this
  299. * method to configure which classes and methods will be cached.</p>
  300. * <p>If you call this method with no arguments, caching will be
  301. * enabled for all classes and methods.</p>
  302. * <p>When generating the ID for each request the class name, method name
  303. * and arguments list is used to build a long string which is later
  304. * used for the ID. You can configure {@link XOAD_Cache} to include
  305. * some of the class variables too.</p>
  306. * <p>Example:</p>
  307. * <code>
  308. * <?php
  309. *
  310. * // Allow caching for all classes.
  311. * XOAD_Cache::allowCaching();
  312. *
  313. * // Allow caching only for the 'Example' class.
  314. * XOAD_Cache::allowCaching('Example');
  315. *
  316. * // Allow caching only for the 'Example->invoke' method.
  317. * XOAD_Cache::allowCaching('Example', 'invoke');
  318. *
  319. * // Allow caching for the 'invoke' method in every class.
  320. * XOAD_Cache::allowCaching(null, 'invoke');
  321. *
  322. * ?>
  323. * </code>
  324. *
  325. * @access public
  326. *
  327. * @param array $className The class name filter. This value can be null.
  328. *
  329. * @param array $method The method name filter. This value can be null.
  330. *
  331. * @param array $expire The lifetime time in seconds for the
  332. * cached data. This value can be null.
  333. *
  334. * @param array $arguments Array that contains the list of arguments to
  335. * use when generating the request ID. By default
  336. * each argument is used. This value can be null.
  337. *
  338. * @param array $members Array that contains the list of class variables
  339. * to use when generating the request ID. By
  340. * default no class variables are used. This value
  341. * can be null.
  342. *
  343. * @return void
  344. *
  345. * @static
  346. *
  347. */
  348. function allowCaching($className = null, $method = null, $expire = null, $arguments = null, $members = null)
  349. {
  350. $GLOBALS['_XOAD_CACHE_DATA'][] = array(
  351. 'className' => $className,
  352. 'method' => $method,
  353. 'expire' => $expire,
  354. 'arguments' => $arguments,
  355. 'members' => $members
  356. );
  357. }
  358. }
  359.  
  360. ?>

Documentation generated on Sat, 12 Nov 2005 20:23:51 +0200 by phpDocumentor 1.3.0RC3