PageRenderTime 39ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/cache/engines/WaxCacheBackgroundMemcache.php

http://github.com/phpwax/phpwax
PHP | 101 lines | 79 code | 15 blank | 7 comment | 29 complexity | 4d66bf4b2825bb4cf96547d9346c0fff MD5 | raw file
  1. <?php
  2. /**
  3. * @package PHP-Wax
  4. */
  5. /**
  6. * Engine for caching of data / objects to file.
  7. * @package PHP-Wax
  8. */
  9. class WaxCacheBackgroundMemcache implements CacheEngine{
  10. public $identifier = false;
  11. public $lifetime = 100;
  12. public $dir = false;
  13. public $marker = '';
  14. public $suffix = 'cache';
  15. public $meta_suffix = "-meta-";
  16. public $lock_suffix = "-lock-";
  17. public $server = "localhost";
  18. public $port = "11211";
  19. public $memcache = false;
  20. public function __construct($prefix=false, $lifetime=0, $suffix='cache', $identifier=false) {
  21. $this->lifetime = $lifetime;
  22. if($prefix) $this->prefix = $dir;
  23. else $this->prefix = 'mc-';
  24. if($identifier) $this->identifier = $identifier;
  25. else $this->indentifier = $this->make_identifier($_SERVER['HTTP_HOST']);
  26. if($suffix) $this->suffix = $suffix;
  27. $this->memcache = new Memcache;
  28. if(!$connected = $this->memcache->connect($this->server, $this->port)) $this->memcache = false;
  29. }
  30. public function get() {
  31. if($this->memcache && ($content = $this->valid()) ) return $content;
  32. else return false;
  33. }
  34. public function set($value) {
  35. if($this->memcache){
  36. $this->memcache->set($this->identifier, $value, 0, 0);
  37. $this->set_meta();
  38. }
  39. }
  40. public function locked(){
  41. if($this->memcache && $this->memcache->get($this->identifier.$this->lock_suffix)) return true;
  42. else return false;
  43. }
  44. public function set_meta(){
  45. $data = array('lock'=>$this->identifier.$this->lock_suffix, 'ident'=>$this->identifier,'location'=>"http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'], 'time'=>time(), 'post'=>serialize($_POST));
  46. if($this->memcache) $this->memcache->set($this->identifier.$this->meta_suffix, serialize($data), 0, 0);
  47. }
  48. public function get_meta(){
  49. if($this->memcache) return unserialize($this->memcache->get($this->identifier.$this->meta_suffix));
  50. else return array();
  51. }
  52. public function valid() {
  53. if(!$this->memcache || $_GET['no-wax-cache']) return false;
  54. $return = $this->memcache->get($this->identifier);
  55. $meta = $this->get_meta();
  56. $age = time() - $meta['time'];
  57. if(($age > $this->lifetime) && !$_GET['no-wax-cache'] && !$this->locked()){
  58. $cmd = "php ".dirname(__FILE__)."/WaxRegenMemcacheCache.php ".$this->identifier.$this->meta_suffix." {$this->server} {$this->port} > /dev/null &";
  59. exec($cmd);
  60. }
  61. return $return;
  62. }
  63. public function expire() {
  64. if($this->identifier && $this->memcache) $this->memcache->delete($this->identifier);
  65. }
  66. public function file() {
  67. return $this->identifier;
  68. }
  69. public function make_identifier($prefix=false){
  70. if(!$prefix) $prefix=$_SERVER['HTTP_HOST'];
  71. $str = $this->dir.$prefix;
  72. Session::unset_var('referrer');
  73. $uri = preg_replace('/([^a-z0-9A-Z\s])/', "", $_SERVER['REQUEST_URI']);
  74. while(strpos($uri, " ")) $uri = str_replace(" ", " ", $uri);
  75. if(strlen($uri)) $str.='-'.md5(str_replace("nowaxcache1", "", str_replace(" ", "-",$uri)));
  76. if(count($data)) $str .= "-d-".md5(serialize($data));
  77. if(count($sess)) $str .= "-s-".md5(serialize(Session::get()));
  78. if(count($_GET)){
  79. $get = $_GET;
  80. unset($get['route'], $get['no-wax-cache']);
  81. $str .= "-g-".md5(serialize($get));
  82. }
  83. if(count($_POST)) $str .= "-p-".md5(serialize($_POST));
  84. return $str;
  85. }
  86. }