Created
August 12, 2011 18:46
-
-
Save nmmmnu/1142681 to your computer and use it in GitHub Desktop.
Memcached 2 Redis wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
/* | |
========================= | |
Memcached 2 Redis wrapper | |
========================= | |
Version 1.0.1, CopyLeft 2011.AUG.11 | |
This piece of software is sponsored by Redis4You: | |
http://redis4you.com/ | |
This class utilize phpredis extension: | |
https://github.com/nicolasff/phpredis | |
This class is distributed under GNU General Public License | |
http://www.gnu.org/copyleft/gpl.html | |
If you wish commercial licence or support, please contact us at: | |
[email protected] | |
===================================================== | |
The class have some limitations. Please see the list. | |
===================================================== | |
Lame Implemented in non atomic way: | |
prepend | |
Not implemented: | |
addByKey | |
appendByKey | |
casByKey | |
deleteByKey | |
getByKey | |
getDelayedByKey | |
getMultiByKey | |
prependByKey | |
replaceByKey | |
setByKey | |
setMultiByKey | |
cas | |
getServerByKey | |
getServerList | |
getDelayed | |
fetch | |
fetchAll | |
getResultCode | |
getResultMessage | |
Implemented in different way: | |
addServer | |
addServers | |
no CAS float for get and set | |
Implemented Empty/Fake functions | |
getStats | |
getVersion | |
getOption | |
setOption | |
===================================================== | |
*/ | |
class Memcached{ | |
var $r; | |
var $version; | |
function __construct($persistent=0){ | |
// Fake memcached verion string... | |
$this->version = "1.2.6"; | |
$this->r = new Redis(); | |
} | |
function add($key, $value, $expiration=0){ | |
$ret = $this->r->set($key, $value); | |
if (! $ret) | |
return false; | |
if ($expiration) | |
$this->r->expire($key, $expiration); | |
return true; | |
} | |
function addServer($host, $port, $weight = 0){ | |
try{ | |
$this->r->connect($host, $port); | |
}catch(Exception $e){ | |
return false; | |
} | |
return true; | |
} | |
// Add just first server... | |
function addServers($servers){ | |
return $this->addServer($servers[0][0], $servers[0][1], $servers[0][2]); | |
} | |
function append($key, $value){ | |
$expiration = $this->r->ttl($key); | |
$ret = $this->r->append($key, $value); | |
if (! $ret) | |
return false; | |
if ($expiration > 0) | |
$this->r->expire($key, $expiration); | |
return true; | |
} | |
function decrement($key, $offset = 1){ | |
$expiration = $this->r->ttl($key); | |
$ret = false; | |
// For performance reasons: | |
if ($offset == 1) | |
$ret = $this->r->decr($key); | |
else | |
$ret = $this->r->decrby($key, $offset); | |
if ($expiration > 0) | |
$this->r->expire($key, $expiration); | |
return $ret; | |
} | |
function delete($key, $time = 0){ | |
return $this->r->del($key); | |
} | |
function flush($delay = 0){ | |
return $this->r->flushdb(); | |
} | |
function get($key){ | |
return $this->r->get($key); | |
} | |
function getMulti($keys){ | |
/* | |
$okeys = array(); | |
foreach($keys as $key){ | |
$okeys[$key] = $this->get($key); | |
} | |
return $okeys; | |
*/ | |
return $this->r->mget($keys); | |
} | |
function getOption($option){ | |
return true; | |
} | |
function getStats(){ | |
return $this->r->info(); | |
} | |
function getVersion(){ | |
return $this->version; | |
} | |
function increment($key, $offset = 1){ | |
$expiration = $this->r->ttl($key); | |
$ret = false; | |
// For performance reasons: | |
if ($offset == 1) | |
$ret = $this->r->incr($key); | |
else | |
$ret = $this->r->incrby($key, $offset); | |
if ($expiration > 0) | |
$this->r->expire($key, $expiration); | |
return $ret; | |
} | |
function prepend($key, $value){ | |
$expiration = $this->r->ttl($key); | |
// I know this sucks... | |
$val = $this->r->get($key); | |
if ($val === false) | |
return false; | |
$ret = $this->r->set($key, $value . $val); | |
if ($expiration > 0) | |
$this->r->expire($key, $expiration); | |
return $ret; | |
} | |
function replace($key, $value, $expiration){ | |
$ret = $this->r->setnx($key, $value); | |
if (! $ret) | |
return false; | |
if ($expiration > 0) | |
$this->r->expire($key, $expiration); | |
return true; | |
} | |
function set($key, $value, $expiration){ | |
$ret = $this->r->set($key, $value); | |
if (! $ret) | |
return false; | |
if ($expiration > 0) | |
$this->r->expire($key, $expiration); | |
return true; | |
} | |
function setMulti($keys, $expiration){ | |
$ret = $this->r->mset($keys); | |
if (! $ret) | |
return false; | |
if ($expiration > 0){ | |
foreach($keys as $key){ | |
$this->r->expire(key, $expiration); | |
} | |
} | |
return true; | |
} | |
function setOption($option, $value){ | |
return true; | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment