Viewing Paste 403

Formated Paste

  1. <?php
  2. /*
  3. ** The settings in Settings.php should look like this:
  4. // Select a system. Probably want to leave this up to the installer or admin panel.
  5. $cache = array(
  6. // memcache, eaccelerator, mmcache, output_cache, xcache, apc, file, or false.
  7. 'system' => '',
  8. // The level of caching. The higher the number, the more gets cached. An integer between 0 and 3 (0 turns caching off).
  9. 'level' => 0,
  10. // This is generally only used for memcache. Should be a string of 'server:port, server:port'.
  11. 'servers' => '',
  12. // Persistent connection (only of use to memcache).
  13. 'persist' => false,
  14. // The directory where you want to store your cache files (if you are using the 'file' system).
  15. 'dir' => '',
  16. // Boolean value. If true, it logs how many hits/misses and how long they took.
  17. 'debug' => false,
  18. );
  19. */
  20.  
  21. $cache = new cache($cache);
  22. class cache
  23. {
  24. public $hits = 0;
  25. public $count = 0;
  26. protected $settings = array();
  27.  
  28. // Check all of the variables and setup what functions to use.
  29. function __construct ($settings = false)
  30. {
  31. $this->settings = $settings;
  32.  
  33. if ($this->cache == false)
  34. return;
  35.  
  36. $this->settings['level'] = !empty($this->settings['level']) ? (int) $this->settings['level'] : false;
  37. $this->settings['system'] = !empty($this->settings['system']) ? $this->settings['system'] : false;
  38. $this->settings['dir'] = !empty($this->settings['dir']) && file_exists($this->settings['dir']) ? $this->settings['dir'] : false;
  39.  
  40. if (!$this->settings['system'] || !$this->settings['level'])
  41. $this->settings = false;
  42.  
  43. // Define the Get, Set, and sometimes Remove functions.
  44. // Just added: remove, increment, decrement, status.
  45. switch ($this->settings['system'])
  46. {
  47. case 'apc':
  48. $this->get = 'apc_fetch';
  49. $this->set = 'apc_store';
  50. $this->rm = 'apc_delete';
  51. $this->clear = 'apc_clear_cache';
  52. break;
  53. case 'memcache':
  54. $this->get = 'memcache_get';
  55. $this->set = 'memcache_set';
  56. $this->rm = 'memcache_delete';
  57. $this->clear = 'memcache_flush';
  58. break;
  59. case 'eaccelerator':
  60. $this->get = 'eaccelerator_get';
  61. $this->set = 'eaccelerator_put';
  62. $this->rm = 'eaccelerator_rm';
  63. $this->gc = 'eaccelerator_gc';
  64. // Might have to use eaccelerator_list_keys() and rm all of them.
  65. //$this->clear = 'eaccelerator_gc';
  66. break;
  67. case 'mmcache':
  68. $this->get = 'mmcache_get';
  69. $this->set = 'mmcache_put';
  70. $this->rm = 'mmcache_rm';
  71. $this->gc = 'mmcache_gc';
  72. break;
  73. case 'output_cache':
  74. $this->get = 'output_cache_get';
  75. $this->set = 'output_cache_put';
  76. // Always one that has to be different. We must find those that check for TTL in get.
  77. $this->settings['req_ttl'] = true;
  78. $this->rm = 'output_cache_remove_key';
  79. // No idea how to implement clear() in this one.
  80. break;
  81. case 'xcache':
  82. $this->get = 'xcache_get';
  83. $this->set = 'xcache_set';
  84. $this->rm = 'xcache_unset';
  85. $this->clear = 'xcache_clear_cache';
  86. break;
  87. case: 'file':
  88. $this->get = array($this, 'get');
  89. $this->set = array($this, 'set');
  90. $this->rm = array($this, 'rm');
  91. $this->clear = array($this, 'clear');
  92. //$this->version = array($this, 'version'),
  93. default:
  94. $this->settings = false;
  95. }
  96.  
  97. // Do we need to connect to the memcached server?
  98. if ($this->settings['system'] == 'memcache')
  99. {
  100. if (!empty($this->settings['servers']))
  101. {
  102. // Not connected yet?
  103. if (empty($this->res))
  104. {
  105. $this->settings['servers'] = explode(',', $this->settings['servers']);
  106. get_memcached_server();
  107. }
  108. if (!$this->res)
  109. $this->settings = false;
  110. }
  111. else
  112. $this->settings = false;
  113. }
  114.  
  115. // Do we need to do garbage collection?
  116. if (isset($this->gc) && mt_rand(0, 10) == 1)
  117. call_user_func($this->gc);
  118.  
  119. // Let someone know that caching isn't working.
  120. if ($this->settings === false)
  121. trigger_error('Invalid cache type: ' . $this->settings['system'], E_USER_NOTICE);
  122.  
  123. if (!is_callable($this->get) || !is_callable($this->set))
  124. trigger_error($this->get .' or ' . $this->set . ' functions not found', E_USER_NOTICE);
  125. }
  126.  
  127. // Connect to a memcached server.
  128. private function get_memcached_server($level = 3)
  129. {
  130. $server = explode(':', trim($this->settings['servers'][array_rand($this->settings['servers'])]));
  131.  
  132. // Don't try more times than we have servers!
  133. $level = min(count($this->settings['servers']), $level);
  134.  
  135. // Don't wait too long. It might be faster to just run without caching.
  136. if ($this->settings['persist'])
  137. $this->res = memcache_connect($server[0], empty($server[1]) ? 11211 : $server[1]);
  138. else
  139. $this->res = memcache_pconnect($server[0], empty($server[1]) ? 11211 : $server[1]);
  140.  
  141. if (!$this->res && $level > 0)
  142. get_memcached_server($level - 1);
  143. }
  144.  
  145. // Put an item in the cache.
  146. function put($key, $value, $ttl = 120, $level = 0)
  147. {
  148. if (!$this->settings)
  149. return;
  150.  
  151. if ($this->settings['debug'])
  152. {
  153. $this->count++;
  154. $this->hits[$this->count] = array('k' => $key, 'd' => 'put', 's' => $value === null ? 0 : strlen(serialize($value)));
  155. $st = microtime();
  156. }
  157.  
  158. $key = $this->get_key($key);
  159. $value = $value === null ? null : serialize($value);
  160.  
  161. if ($value === null)
  162. $this->rm($key);
  163.  
  164. if ($this->settings['system'] == 'file')
  165. {
  166. // Create a PHP file with the info.
  167. if ($value !== null)
  168. file_put_contents($this->settings['dir'] . '/data_' . $key . '.php', '<?php if (' . (time() + $ttl) . ' < time()) $expired = true; else{$expired = false; $value = \'' . addcslashes($value, '\\\'') . '\';}?>', LOCK_EX);
  169. else
  170. $this->rm($this->settings['dir'] . '/data_' . $key . '.php');
  171. }
  172. // Some caches are accessed through a resource.
  173. elseif (isset($this->res))
  174. $cache['put']($this->res, $key, $value, 0, $ttl);
  175. else
  176. $cache['put']($key, $value, $ttl);
  177.  
  178. if ($this->settings['debug'])
  179. $this->hits[$this->count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st));
  180. }
  181.  
  182. // Return a cached item.
  183. function get($key, $ttl = 120, $level = 0)
  184. {
  185. if (!$this->settings)
  186. return;
  187.  
  188. if ($this->settings['debug'])
  189. {
  190. $this->count++;
  191. $this->hits[$this->count] = array('k' => $key, 'd' => 'get');
  192. $st = microtime();
  193. }
  194.  
  195. $key = $this->get_key($key);
  196.  
  197. if ($this->settings['system'] == 'file' && file_exists($this->settings['dir'] . '/data_' . $key . '.php') && filesize($this->settings['dir'] . '/data_' . $key . '.php') > 10)
  198. {
  199. require($this->settings['dir'] . '/data_' . $key . '.php');
  200. if (!empty($expired) && isset($value))
  201. {
  202. $this->rm($this->settings['dir'] . '/data_' . $key . '.php');
  203. unset($value);
  204. }
  205. }
  206. elseif (isset($this->res))
  207. $value = $this->get($this->res, $key);
  208. elseif ($this->settings['req_ttl'])
  209. $value = $this->get($key, $ttl);
  210. else
  211. $value = $this->get($key);
  212.  
  213. if ($this->settings['debug'])
  214. {
  215. $this->hits[$this->count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st));
  216. $this->hits[$this->count]['s'] = isset($value) ? strlen($value) : 0;
  217. }
  218.  
  219. if (!empty($value))
  220. return unserialize($value);
  221. }
  222.  
  223. // Increment.
  224. function increment($key, $value = 1, $ttl = 120)
  225. {
  226. if (!$this->settings)
  227. return;
  228.  
  229. if (!isset($this->inc))
  230. {
  231. $cached_value = call_user_func(array($this, 'get'), $key);
  232. if (is_int($cached_value))
  233. {
  234. call_user_func(array($this, 'put'), $cached_value + $value);
  235. return $cached_value + $value;
  236. }
  237. }
  238. elseif ($this->settings['req_ttl'])
  239. return call_user_func($this->inc, $value, $ttl);
  240. else
  241. return call_user_func($this->inc, $value);
  242. }
  243.  
  244. // Decrement.
  245. function decrement($key, $value = 1, $ttl = 120)
  246. {
  247. if (!$this->settings)
  248. return;
  249.  
  250. if (!isset($this->dec))
  251. {
  252. $cached_value = call_user_func(array($this, 'get'), $key);
  253. if (is_int($cached_value))
  254. {
  255. call_user_func(array($this, 'put'), $cached_value - $value);
  256. return $cached_value + $value;
  257. }
  258. }
  259. elseif ($this->settings['req_ttl'])
  260. return call_user_func($this->dec, $value, $ttl);
  261. else
  262. return call_user_func($this->dec, $value);
  263. }
  264.  
  265. // Remove a cached item.
  266. function rm($key)
  267. {
  268. if (!$this->settings)
  269. return;
  270.  
  271. if ($this->settings['system'] == 'file' && file_exists($this->settings['dir'] . '/data_' . $key . '.php'))
  272. unlink($this->settings['dir'] . '/data_' . $key . '.php');
  273. else
  274. call_user_func($this->rm);
  275. }
  276.  
  277. // Clears the entire cache. Nothing will be left!
  278. function clear()
  279. {
  280. if (!$this->settings)
  281. return;
  282.  
  283. if ($this->settings['system'] == 'file')
  284. {
  285. $files = scandir($this->settings['dir']);
  286. foreach ($files as $file)
  287. unlink($this->settings . '/' . $file);
  288. }
  289.  
  290. // If there is no clear function defined, try to find all of the keys and delete those?
  291. // If we can't delete all of them, try dropping everything and then restarting?
  292. }
  293.  
  294. // Return the version of the current cache.
  295. function version()
  296. {
  297. if (!$this->settings)
  298. return;
  299.  
  300. switch($this->settings['system'])
  301. {
  302. case 'apc':
  303. $version = array('title' => 'Alternative PHP Cache', 'version' => phpversion('apc'));
  304. break;
  305. case 'memcache':
  306. $version = array('title' => 'Memcached', 'version' => memcache_get_version($this->resource));
  307. break;
  308. case 'eaccelerator':
  309. $version = array('title' => 'eAccelerator', 'version' => EACCELERATOR_VERSION);
  310. break;
  311. case 'mmcache':
  312. $version = array('title' => 'Turck MMCache', 'version' => MMCACHE_VERSION);
  313. break;
  314. case 'output_cache':
  315. global $_PHPA;
  316. $version = array('title' => 'Zend', 'version' => $_PHPA['VERSION']);
  317. break;
  318. case 'xcache':
  319. $version = array('title' => 'XCache', 'version' => XCACHE_VERSION);
  320. break;
  321. default:
  322. $version = array('title' => 'file', 'version' => 1);
  323. }
  324. return $version;
  325. }
  326.  
  327. // One function to get the key so we can change how the keys are formatted easily.
  328. private function get_key($key)
  329. {
  330. //return md5(__DIR__ . filemtime(__FILE__) . __FILE__ . strtr($key, ':', '-'));
  331. return strtr($key, ':', '-');
  332. }
  333. }
  334.  
  335. /* Development Notes:
  336. * I removed the @ operator from unlink because if we can't remove a file, we don't want to fill up our filesystem with cached files.
  337. I'd rather have it fill up an error log and let someone know about it.
  338. * Webpages for support:
  339. MMCache: http://turck-mmcache.sourceforge.net/index_old.html#api
  340. Xcache: http://xcache.lighttpd.net/wiki/XcacheApi
  341. memcache: http://www.php.net/memcache
  342. APC: http://www.php.net/apc
  343. eAccelerator: http://bart.eaccelerator.net/doc/phpdoc/
  344. Zend: http://files.zend.com/help/Zend-Platform/partial_and_preemptive_page_caching.htm
  345. */
  346. ?>
Name:
Email:
Code/text to paste:
  • Enable code highlighting
  • Code Language:
  • A duck, cat and a goose walk into a bar. How many animals walked into a bar?:
Highslide for Wordpress Plugin