PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/_plugins_/memoization/inc/memoization.php

https://bitbucket.org/pombredanne/spip-zone-treemap
PHP | 230 lines | 162 code | 34 blank | 34 comment | 11 complexity | e0bbc0881135f9accded8fce473bf99a MD5 | raw file
  1. <?php
  2. /* prototype de backend */
  3. class MCacheBackend {
  4. function get($key) {}
  5. function set($key, $value, $ttl=null) {}
  6. function exists($key) {}
  7. function del($key) {}
  8. function inc($key, $value=null, $ttl=null) {}
  9. function dec($key, $value=null, $ttl=null) {}
  10. function lock($key, /* private */ $unlock = false) {}
  11. function unlock($key) {}
  12. function init($params = null) {}
  13. }
  14. /* objet MCache */
  15. class MCache {
  16. var $methode;
  17. var $backend;
  18. function memory() {
  19. return !in_array($this->methode, array('filecache', 'nocache'));
  20. }
  21. function MCache($methode=null, $params=array()) {
  22. // autodetect
  23. $this->methode = $methode ? $methode : $this->methode();
  24. $f = find_in_path($this->methode.'.inc',"memo/");
  25. require_once $f;
  26. $obj = 'MCacheBackend_'.$this->methode;
  27. $this->backend = new $obj;
  28. $this->backend->init($params);
  29. }
  30. function methode($methode = null) {
  31. if (!$methode) {
  32. $methodes = array('apc', 'xcache', 'eaccelerator', 'filecache', 'nocache');
  33. while (!MCache::methode($methode = array_shift($methodes))){};
  34. return $methode;
  35. }
  36. switch($methode) {
  37. case 'apc':
  38. return function_exists('apc_exists');
  39. case 'xcache':
  40. if (!function_exists('xcache_set'))
  41. return false;
  42. @xcache_set('xcache_autodetect',1234);
  43. return @xcache_get('xcache_autodetect')==1234;
  44. case 'memcache':
  45. return function_exists('memcache_set');
  46. case 'eaccelerator':
  47. return function_exists('eaccelerator_put');
  48. case 'filecache':
  49. case 'nocache':
  50. return true;
  51. }
  52. }
  53. /* mixed */
  54. function get($key) {
  55. return $this->backend->get($key);
  56. }
  57. /* bool */
  58. function set($key, $value, $ttl=null) {
  59. return $this->backend->set($key, $value, $ttl);
  60. }
  61. /* bool */
  62. function exists($key) {
  63. return $this->backend->exists($key);
  64. }
  65. /* bool */
  66. function del($key) {
  67. return $this->backend->del($key);
  68. }
  69. /* int */
  70. function inc($key, $value=null, $ttl=null) {
  71. return $this->backend->inc($key, $value, $ttl);
  72. }
  73. /* int */
  74. function dec($key, $value=null, $ttl=null) {
  75. return $this->backend->dec($key, $value, $ttl);
  76. }
  77. /* null */
  78. function lock($key) {
  79. return $this->backend->lock($key);
  80. }
  81. /* null */
  82. function unlock($key) {
  83. return $this->backend->unlock($key);
  84. }
  85. /* mixed */
  86. function size() {
  87. if (method_exists($this->backend, 'size'))
  88. return $this->backend->size();
  89. }
  90. }
  91. if (!isset($GLOBALS['meta']['cache_namespace'])){
  92. include_spip('inc/acces');
  93. ecrire_meta('cache_namespace', dechex(crc32($_SERVER["DOCUMENT_ROOT"] . $_SERVER["SERVER_SIGNATURE"] . creer_uniqid())), 'non');
  94. }
  95. if (!defined('_CACHE_NAMESPACE'))
  96. define('_CACHE_NAMESPACE', $_SERVER['HTTP_HOST'].':'.$GLOBALS['meta']['cache_namespace'].':');
  97. global $Memoization;
  98. $cfg = @unserialize($GLOBALS['meta']['memoization']);
  99. $Memoization = new MCache(preg_replace(",\W,","",$cfg['methode']));
  100. /* mode procedural */
  101. /* mixed */
  102. function cache_get($key) {
  103. global $Memoization;
  104. return $Memoization->get($key);
  105. }
  106. /* bool */
  107. function cache_set($key, $value, $ttl=null) {
  108. global $Memoization;
  109. return $Memoization->set($key, $value, $ttl);
  110. }
  111. /* bool */
  112. function cache_exists($key) {
  113. global $Memoization;
  114. return $Memoization->exists($key);
  115. }
  116. function cache_isset($key) { # obsolete
  117. global $Memoization;
  118. return $Memoization->exists($key);
  119. }
  120. /* bool */
  121. function cache_del($key) {
  122. global $Memoization;
  123. return $Memoization->del($key);
  124. }
  125. function cache_unset($key) { # obsolete
  126. global $Memoization;
  127. return $Memoization->del($key);
  128. }
  129. /* int */
  130. function cache_inc($key, $value=null, $ttl=null) {
  131. global $Memoization;
  132. return $Memoization->inc($key, $value, $ttl);
  133. }
  134. /* int */
  135. function cache_dec($key, $value=null, $ttl=null) {
  136. global $Memoization;
  137. return $Memoization->dec($key, $value, $ttl);
  138. }
  139. /* null */
  140. function cache_lock($key) {
  141. global $Memoization;
  142. return $Memoization->lock($key, $value, $ttl);
  143. }
  144. /* null */
  145. function cache_unlock($key) {
  146. global $Memoization;
  147. return $Memoization->unlock($key, $value, $ttl);
  148. }
  149. /* filtre pour la page de cfg */
  150. function memoization_methode($methode=null) {
  151. return MCache::methode($methode);
  152. }
  153. //
  154. // Cache a function's result cache_me()
  155. // (c) Fil 2009 - Double-licensed under the GNU/LGPL and MIT licenses
  156. // http://zzz.rezo.net/-SPIP-
  157. // $ttl = time to live
  158. // $vars = other variables that could change the result
  159. // (the function's variables are automatically taken into account)
  160. //
  161. // Usage: include_spip('inc/memoization');
  162. // In any cacheable function add at top:
  163. // if(!is_null($c=cache_me())) return$c;
  164. if (!function_exists('debug_backtrace')) {
  165. function cache_me() {return;}
  166. } else {
  167. function cache_me($vars=null, $ttl=3600) {
  168. $trace = debug_backtrace();
  169. $trace = $trace[1];
  170. if (isset($trace['object']))
  171. $fun = array($trace['object'], $trace['function']);
  172. else
  173. $fun = $trace['function'];
  174. $key = md5(
  175. $fun
  176. .serialize($trace['args'])
  177. .serialize($vars)
  178. );
  179. if (!cache_exists($key)) {
  180. cache_set($key, null, $ttl);
  181. $r = call_user_func_array($fun, $trace['args']);
  182. cache_set($key, $r, $ttl);
  183. return $r;
  184. }
  185. return cache_get($key);
  186. }
  187. }
  188. // outil pour memcache (hosts et ports a configurer dans le CFG)
  189. function cfg_memcache_servers() {
  190. $cfg = @unserialize($GLOBALS['meta']['memoization']);
  191. if (!$cfg = $cfg['memcache_servers'])
  192. $cfg = 'localhost:11211';
  193. preg_match_all('/[a-z0-9._-]*(?::\d+)/', $cfg, $s, PREG_PATTERN_ORDER);
  194. return $s[0];
  195. }
  196. ?>