.gitignore000064400000000036144761607120006543 0ustar00/vendor/ /tests/ composer.lockcomposer.json000064400000000703144761607120007276 0ustar00{ "name": "boru/dhcache", "type": "library", "autoload": { "psr-4": { "boru\\dhcache\\": "src/" } }, "authors": [ { "name": "Daniel Hayes", "email": "dhayes@boruapps.com" } ], "require": { "boru/dhutils": "*" }, "repositories": [ { "type": "composer", "url": "https://satis.boruapps.com" } ] } instructions-composer.txt000064400000000302144761607120011701 0ustar00{ "require": { "boru/dhcache": "*" }, "repositories": [ { "type": "composer", "url": "https://satis.boruapps.com" } ] }src/backends/Memcached.php000064400000004672144761607120011505 0ustar00host = dhGlobal::getVal($options,"host","localhost"); $this->port = dhGlobal::getVal($options,"port",11211); $this->cache = new \Memcached(); $this->cache->addServer($this->host, $this->port); $this->prefix = $prefix; } protected function prefix($key) { return $this->prefix.$key; } public function set($key,$value,$expiration=0) { return $this->cache->set($this->prefix($key),$value,$expiration); } public function get($key,$default=false) { return $this->cache->get($this->prefix($key)); } public function lockVar($key, $value = 1, $expiration = 0) { return $this->cache->add($this->prefix($key),$value,$expiration); } public function unlockVar($key) { return $this->cache->delete($this->prefix($key)); } public function inc($key, $expiration = 0, $value = 1) { $this->lockVar($key,0,$expiration);//create the key if it doesnt exist return $this->cache->increment($this->prefix($key), $value); } public function dec($key, $expiration = 0, $value = 1) { $this->lockVar($key,0,$expiration);//create the key if it doesnt exist return $this->cache->decrement($this->prefix($key), $value); } public function atomicSet($key,$value,$expiration=0) { $lockKey = "__dhCachelock_".$this->prefix($key); $res = $this->cache->add($lockKey,1,10); if($res === true) { $success = $this->cache->set($this->prefix($key), $value, $expiration); $this->cache->delete($lockKey); return $success; } return false; } public function atomicGet($key) { $lockKey = "__dhCachelock_".$this->prefix($key); $res = $this->cache->add($lockKey,1,10); if($res === true) { $data = $this->cache->get($this->prefix($key)); $this->cache->delete($lockKey); return $data; } return false; } public function __call($name, $arguments) { if (! $this->enabled) return false; //Invoke method $name of $this->cache class with array of $arguments return call_user_func_array(array($this->cache, $name), $arguments); } }src/dhCache.php000064400000006513144761607120007400 0ustar00$memcached]); return $instance; } public function __construct($options=[]) { if(($expirationDefault = dhGlobal::getVal($options,"expirationDefault",false)) !== false) { $this->expirationDefault = $expirationDefault; } if(($expirationPlay = dhGlobal::getVal($options,"expirationPlay",false)) !== false) { $this->expirationPlay = $expirationPlay; } $enabled = dhGlobal::getVal($options,"enabled",null); if(!is_null($enabled)) { $this->enabled = $enabled; } if(($backend = dhGlobal::getVal($options,"backend",false)) !== false) { $this->backend($backend); } else { if(($memcachedOpts = dhGlobal::getVal($options,"memcached",false)) !== false) { $memcached = new Memcached($memcachedOpts); $this->backend($memcached); } } if(is_null($this->cache)) { if(class_exists("\\Memcached")) { $memcached = new Memcached(); $this->backend($memcached); } } } protected function backend($obj) { $this->cache = $obj; } public function set($key, $value, $expiration=NULL) { if (!$this->enabled) return false; return $this->cache->set($key, $value, $this->expiration($expiration)); } public function get($key) { if (!$this->enabled) return false; return $this->cache->get($key); } public function lockSet($key, $value, $expiration=NULL) { if (!$this->enabled) return false; return $this->cache->atomicSet($key,$value,$this->expiration($expiration)); } public function lockGet($key) { if (!$this->enabled) return false; return $this->cache->atomicGet($key); } public function atimicSet($key, $value, $expiration=NULL) { if (!$this->enabled) return false; return $this->cache->atomicSet($key,$value,$this->expiration($expiration)); } public function atomicGet($key) { if (!$this->enabled) return false; return $this->cache->atomicGet($key); } public function lockVar($key, $value = 1, $expiration = 0) { if (!$this->enabled) return false; return $this->cache->lockVar($key,$value,$expiration); } public function unlockVar($key) { if (!$this->enabled) return false; return $this->cache->unlockVar($key); } public function increment($key, $expiration = null, $value = 1) { if (!$this->enabled) return false; return $this->cache->inc($key,$this->expiration($expiration),$value); } public function decrement($key, $expiration = null, $value = 1) { if (!$this->enabled) return false; return $this->cache->dec($key,$this->expiration($expiration),$value); } public function __call($name, $arguments) { if (! $this->enabled) return false; return call_user_func_array(array($this->cache, $name), $arguments); } protected function expiration($expiration=null) { if (is_null($expiration)) { $expiration = $this->expirationDefault + rand( -$this->expirationPlay, $this->expirationPlay); } return $expiration; } }