2016-12-15 08:20:54 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* [Discuz!] (C)2001-2099 Comsenz Inc.
|
|
|
|
* This is NOT a freeware, use is subject to license terms
|
|
|
|
*
|
|
|
|
* $Id: memory_driver_redis.php 33336 2013-05-29 02:05:10Z andyzheng $
|
|
|
|
*/
|
2017-02-07 23:41:58 -05:00
|
|
|
if (!defined('IN_DISCUZ')) {
|
|
|
|
exit('Access Denied');
|
|
|
|
}
|
|
|
|
|
|
|
|
class memory_driver_redis {
|
2016-12-15 08:20:54 -05:00
|
|
|
|
2017-02-07 23:41:58 -05:00
|
|
|
public $cacheName = 'Redis';
|
2016-12-15 08:20:54 -05:00
|
|
|
var $enable;
|
|
|
|
var $obj;
|
2017-02-07 23:41:58 -05:00
|
|
|
|
|
|
|
public function env() {
|
|
|
|
return extension_loaded('redis');
|
|
|
|
}
|
2016-12-15 08:20:54 -05:00
|
|
|
|
|
|
|
function init($config) {
|
2017-02-07 23:41:58 -05:00
|
|
|
if(!$this->env()) {
|
|
|
|
$this->enable = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($config['server'])) {
|
2016-12-15 08:20:54 -05:00
|
|
|
try {
|
|
|
|
$this->obj = new Redis();
|
2017-02-07 23:41:58 -05:00
|
|
|
if ($config['pconnect']) {
|
2016-12-15 08:20:54 -05:00
|
|
|
$connect = @$this->obj->pconnect($config['server'], $config['port']);
|
|
|
|
} else {
|
|
|
|
$connect = @$this->obj->connect($config['server'], $config['port']);
|
|
|
|
}
|
|
|
|
} catch (RedisException $e) {
|
2017-02-07 23:41:58 -05:00
|
|
|
|
2016-12-15 08:20:54 -05:00
|
|
|
}
|
|
|
|
$this->enable = $connect ? true : false;
|
2017-02-07 23:41:58 -05:00
|
|
|
if ($this->enable) {
|
|
|
|
if ($config['requirepass']) {
|
2016-12-15 08:20:54 -05:00
|
|
|
$this->obj->auth($config['requirepass']);
|
|
|
|
}
|
|
|
|
@$this->obj->setOption(Redis::OPT_SERIALIZER, $config['serializer']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function &instance() {
|
|
|
|
static $object;
|
2017-02-07 23:41:58 -05:00
|
|
|
if (empty($object)) {
|
2016-12-15 08:20:54 -05:00
|
|
|
$object = new memory_driver_redis();
|
|
|
|
$object->init(getglobal('config/memory/redis'));
|
|
|
|
}
|
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get($key) {
|
2017-02-07 23:41:58 -05:00
|
|
|
if (is_array($key)) {
|
2016-12-15 08:20:54 -05:00
|
|
|
return $this->getMulti($key);
|
|
|
|
}
|
|
|
|
return $this->obj->get($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getMulti($keys) {
|
|
|
|
$result = $this->obj->getMultiple($keys);
|
|
|
|
$newresult = array();
|
|
|
|
$index = 0;
|
2017-02-07 23:41:58 -05:00
|
|
|
foreach ($keys as $key) {
|
|
|
|
if ($result[$index] !== false) {
|
2016-12-15 08:20:54 -05:00
|
|
|
$newresult[$key] = $result[$index];
|
|
|
|
}
|
|
|
|
$index++;
|
|
|
|
}
|
|
|
|
unset($result);
|
|
|
|
return $newresult;
|
|
|
|
}
|
|
|
|
|
2017-02-07 23:41:58 -05:00
|
|
|
function select($db = 0) {
|
2016-12-15 08:20:54 -05:00
|
|
|
return $this->obj->select($db);
|
|
|
|
}
|
|
|
|
|
|
|
|
function set($key, $value, $ttl = 0) {
|
2017-02-07 23:41:58 -05:00
|
|
|
if ($ttl) {
|
2016-12-15 08:20:54 -05:00
|
|
|
return $this->obj->setex($key, $ttl, $value);
|
|
|
|
} else {
|
|
|
|
return $this->obj->set($key, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function rm($key) {
|
|
|
|
return $this->obj->delete($key);
|
|
|
|
}
|
|
|
|
|
2017-02-07 23:41:58 -05:00
|
|
|
function setMulti($arr, $ttl = 0) {
|
|
|
|
if (!is_array($arr)) {
|
2016-12-15 08:20:54 -05:00
|
|
|
return FALSE;
|
|
|
|
}
|
2017-02-07 23:41:58 -05:00
|
|
|
foreach ($arr as $key => $v) {
|
2016-12-15 08:20:54 -05:00
|
|
|
$this->set($key, $v, $ttl);
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
function inc($key, $step = 1) {
|
|
|
|
return $this->obj->incr($key, $step);
|
|
|
|
}
|
|
|
|
|
|
|
|
function dec($key, $step = 1) {
|
|
|
|
return $this->obj->decr($key, $step);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSet($key, $value) {
|
|
|
|
return $this->obj->getSet($key, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
function sADD($key, $value) {
|
|
|
|
return $this->obj->sADD($key, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
function sRemove($key, $value) {
|
|
|
|
return $this->obj->sRemove($key, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
function sMembers($key) {
|
|
|
|
return $this->obj->sMembers($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
function sIsMember($key, $member) {
|
|
|
|
return $this->obj->sismember($key, $member);
|
|
|
|
}
|
|
|
|
|
|
|
|
function keys($key) {
|
|
|
|
return $this->obj->keys($key);
|
|
|
|
}
|
|
|
|
|
2017-02-07 23:41:58 -05:00
|
|
|
function expire($key, $second) {
|
2016-12-15 08:20:54 -05:00
|
|
|
return $this->obj->expire($key, $second);
|
|
|
|
}
|
|
|
|
|
|
|
|
function sCard($key) {
|
|
|
|
return $this->obj->sCard($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
function hSet($key, $field, $value) {
|
|
|
|
return $this->obj->hSet($key, $field, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
function hDel($key, $field) {
|
|
|
|
return $this->obj->hDel($key, $field);
|
|
|
|
}
|
|
|
|
|
|
|
|
function hLen($key) {
|
|
|
|
return $this->obj->hLen($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
function hVals($key) {
|
|
|
|
return $this->obj->hVals($key);
|
|
|
|
}
|
|
|
|
|
2017-02-07 23:41:58 -05:00
|
|
|
function hIncrBy($key, $field, $incr) {
|
2016-12-15 08:20:54 -05:00
|
|
|
return $this->obj->hIncrBy($key, $field, $incr);
|
|
|
|
}
|
|
|
|
|
|
|
|
function hGetAll($key) {
|
|
|
|
return $this->obj->hGetAll($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
function sort($key, $opt) {
|
|
|
|
return $this->obj->sort($key, $opt);
|
|
|
|
}
|
|
|
|
|
|
|
|
function exists($key) {
|
|
|
|
return $this->obj->exists($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
function clear() {
|
|
|
|
return $this->obj->flushAll();
|
|
|
|
}
|
2017-02-07 23:41:58 -05:00
|
|
|
|
2016-12-15 08:20:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|