.gitignore000064400000000036144761607100006541 0ustar00/vendor/ /tests/ composer.lockcomposer.json000064400000000703144761607100007274 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.txt000064400000000302144761607100011677 0ustar00{ "require": { "boru/dhcache": "*" }, "repositories": [ { "type": "composer", "url": "https://satis.boruapps.com" } ] }src/dhCache.php000064400000010063144761607100007371 0ustar00host = dhGlobal::getVal($options,"host","localhost"); $this->port = dhGlobal::getVal($options,"port",11211); $this->cache = new \Memcached(); $this->cache->addServer($this->host, $this->port); } else { $this->cache = $memcached; } $this->prefix = $prefix; } /** * Wrapper around memcache->set * Do not store values if memcache is disabled **/ public function set($key, $value, $expiration=NULL) { if (! $this->enabled) return false; if (empty($expiration)) $expiration = $this->expirationDefault + rand( -$this->expirationPlay, $this->expirationPlay); return $this->cache->set($this->prefix . $key, $value, $expiration); } /** * Wrapper around memcache->get * Always return false if memcache is disabled **/ public function get($key, $cache_cb = NULL, &$cas_token = NULL) { if (!$this->enabled) { return false; } $data = $this->cache->get($this->prefix . $key); if ($data) { return $data; } else { return false; } } public function lockSet($key, $value, $expiration=NULL) { if (!$this->enabled) { return false; } $lockKey = "__dhCachelock_".$this->prefix.$key; $res = $this->cache->add($lockKey,1,10); if($res === true) { if (empty($expiration)) $expiration = $this->expirationDefault + rand( -$this->expirationPlay, $this->expirationPlay); $success = $this->cache->set($this->prefix . $key, $value, $expiration); $this->cache->delete($lockKey); return $success; } return false; } public function lockGet($key) { if (!$this->enabled) { return false; } $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 lockVar($key, $value = 1, $expiration = 0) { if (!$this->enabled) { return false; } $res = $this->cache->add($this->prefix . $key, $value,$expiration); if($res === true) { return true; } return false; } public function increment($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 decrement($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 unlockVar($key) { if (!$this->enabled) { return false; } $this->cache->delete($this->prefix . $key); return true; } /** * Another wrapper, we want to store data in memcache and return the actual data * for further processing * @param key string Our memcache key * @param data mixed Our data to store in Memcache * @param expiration time Our expiration time, see Memcached documentation * @return data mixed Return our stored data unchanged **/ public function setCache($key, $data, $expiration=NULL) { if ($this->enabled) { $this->set($key, $data, $expiration); } //return $data; } /** * This method is invoked if the called method was not realised in this class **/ 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); } }