First commit
This commit is contained in:
0
source/class/memory/index.htm
Normal file
0
source/class/memory/index.htm
Normal file
465
source/class/memory/memory_common_session.php
Normal file
465
source/class/memory/memory_common_session.php
Normal file
@@ -0,0 +1,465 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* [Discuz!] (C)2001-2099 Comsenz Inc.
|
||||
* This is NOT a freeware, use is subject to license terms
|
||||
*
|
||||
* $Id: table_common_session.php 28051 2012-02-21 10:36:56Z zhangguosheng $
|
||||
*/
|
||||
|
||||
if(!defined('IN_DISCUZ')) {
|
||||
exit('Access Denied');
|
||||
}
|
||||
|
||||
class memory_common_session
|
||||
{
|
||||
private $_pre_cache_key;
|
||||
|
||||
const LUA_RETURN_DATA = <<<LUA
|
||||
local rs = {}
|
||||
for _, key in ipairs(sids) do
|
||||
local row = redis.call("hmget", prefix..key, "sid", "ip", "uid", "username", "groupid", "invisible", "action", "lastactivity", "lastolupdate", "fid", "tid")
|
||||
rs[#rs + 1] = row
|
||||
end
|
||||
return rs
|
||||
LUA;
|
||||
|
||||
public function __construct() {
|
||||
$this->_pre_cache_key = 'common_session_';
|
||||
}
|
||||
|
||||
public function fetch($sid, $ip = false, $uid = false) {
|
||||
if(empty($sid)) {
|
||||
return array();
|
||||
}
|
||||
$session = $this->get_data_by_pk($sid);
|
||||
if($session && $ip !== false && $ip != "{$session['ip']}") {
|
||||
$session = array();
|
||||
}
|
||||
if($session && $uid !== false && $uid != $session['uid']) {
|
||||
$session = array();
|
||||
}
|
||||
return $session;
|
||||
}
|
||||
|
||||
public function fetch_member($ismember = 0, $invisible = 0, $start = 0, $limit = 0) {
|
||||
if ($ismember < 1 || $ismember > 2) $ismember = 0; // $ismember只有0, 1, 2是合法值
|
||||
if ($invisible < 1 || $invisible > 2) $invisible = 0; // invisible只有0, 1, 2是合法值
|
||||
|
||||
list($ss, $ee) = $this->get_start_and_end($start, $limit);
|
||||
($invisible == 2) ? $inv_idx = 0 : $inv_idx = 1; // $invisible == 2,相当于sql条件 invisible = 0
|
||||
($ismember == 2) ? $uid_idx = 0 : $uid_idx = 1; // $ismember == 2,相当于sql条件 uid = 0
|
||||
|
||||
if ($ismember == 0 && $invisible == 0) {
|
||||
$script = <<<LUA
|
||||
local prefix = ARGV[1]
|
||||
local start = ARGV[2]
|
||||
local stop = ARGV[3]
|
||||
local sids = redis.call('ZREVRANGE', prefix..'idx_lastactivity', start, stop)
|
||||
LUA;
|
||||
$data = memory('eval', $script . self::LUA_RETURN_DATA, array($ss, $ee), "fetch_member_1st", $this->_pre_cache_key);
|
||||
} elseif ($ismember == 0) {
|
||||
$script = <<<LUA
|
||||
local prefix = ARGV[1]
|
||||
local inv_idx = ARGV[2]
|
||||
local start = ARGV[3]
|
||||
local stop = ARGV[4]
|
||||
local sids = redis.call('ZREVRANGE', prefix..'idx_invisible_'..inv_idx, start, stop)
|
||||
LUA;
|
||||
$data = memory('eval', $script . self::LUA_RETURN_DATA, array($inv_idx, $ss, $ee), "fetch_member_2nd", $this->_pre_cache_key);
|
||||
} elseif ($invisible == 0) {
|
||||
$script = <<<LUA
|
||||
local prefix = ARGV[1]
|
||||
local uid_idx = ARGV[2]
|
||||
local start = ARGV[3]
|
||||
local stop = ARGV[4]
|
||||
local sids = redis.call('ZREVRANGE', prefix..'idx_uid_group_'..uid_idx, start, stop)
|
||||
LUA;
|
||||
$data = memory('eval', $script . self::LUA_RETURN_DATA, array($uid_idx, $ss, $ee), "fetch_member_3rd", $this->_pre_cache_key);
|
||||
} else {
|
||||
global $_G;
|
||||
$temp_uniq = substr(md5(substr(TIMESTAMP, 0, -3).substr($_G['config']['security']['authkey'], 3, -3)), 1, 8);
|
||||
$script = <<<LUA
|
||||
local prefix = ARGV[1]
|
||||
local inv_idx = ARGV[2]
|
||||
local uid_idx = ARGV[3]
|
||||
local out_surfix = ARGV[4]
|
||||
local start = ARGV[5]
|
||||
local stop = ARGV[6]
|
||||
local out_hash = prefix..'invisible_uid_'..out_surfix
|
||||
redis.call('ZINTERSTORE', out_hash, 2, prefix..'idx_invisible_'..inv_idx, prefix..'idx_uid_group_'..uid_idx, 'AGGREGATE', 'MIN')
|
||||
local sids = redis.call('ZREVRANGE', out_hash, start, stop)
|
||||
redis.call('DEL', out_hash)
|
||||
LUA;
|
||||
$data = memory('eval', $script . self::LUA_RETURN_DATA, array($inv_idx, $uid_idx, $temp_uniq, $ss, $ee), "fetch_member_4th", $this->_pre_cache_key);
|
||||
}
|
||||
return $this->array_from_memory_result($data);
|
||||
}
|
||||
|
||||
public function count_invisible($type = 1) {
|
||||
return memory('zcard', 'idx_invisible_' . $type, $this->_pre_cache_key);
|
||||
}
|
||||
|
||||
public function count($type = 0) {
|
||||
switch ($type) {
|
||||
case 1:
|
||||
return memory('zcard', 'idx_uid_group_1', $this->_pre_cache_key);
|
||||
case 2:
|
||||
return memory('zcard', 'idx_uid_group_0', $this->_pre_cache_key);
|
||||
default:
|
||||
return memory('zcard', 'idx_lastactivity', $this->_pre_cache_key);
|
||||
}
|
||||
}
|
||||
|
||||
public function delete_by_session($session, $onlinehold, $guestspan) {
|
||||
if(empty($session) || !is_array($session)) return;
|
||||
$onlinehold = time() - $onlinehold;
|
||||
$guestspan = time() - $guestspan;
|
||||
|
||||
global $_G;
|
||||
$temp_uniq = substr(md5(substr(TIMESTAMP, 0, -3).substr($_G['config']['security']['authkey'], 3, -3)), 1, 8);
|
||||
$script = <<<LUA
|
||||
local rs = {}
|
||||
local prefix = ARGV[1]
|
||||
local sid = ARGV[2]
|
||||
local onlinehold = ARGV[3]
|
||||
local guestspan = ARGV[4]
|
||||
local userip = ARGV[5]
|
||||
local uid = ARGV[6]
|
||||
local out_surfix = ARGV[7]
|
||||
|
||||
local function getdata(key)
|
||||
local data = redis.call("HMGET", key, 'sid', 'ip', 'uid', 'invisible', 'fid')
|
||||
if (data[1]) then
|
||||
return data
|
||||
else
|
||||
return {}
|
||||
end
|
||||
end
|
||||
|
||||
local bysid = getdata(prefix..sid);
|
||||
if (#bysid > 0) then
|
||||
redis.call("del", prefix..sid)
|
||||
rs[#rs + 1] = bysid
|
||||
end
|
||||
|
||||
-- lastactivity < onlinehold
|
||||
local byonlinehold = redis.call("ZRANGEBYSCORE", prefix.."idx_lastactivity", 0, onlinehold + 1);
|
||||
for _, sid in ipairs(byonlinehold) do
|
||||
local data = getdata(prefix..sid);
|
||||
if (#data > 0) then
|
||||
redis.call("del", prefix..sid)
|
||||
rs[#rs + 1] = data
|
||||
end
|
||||
end
|
||||
|
||||
-- uid = 0 and ip = userip
|
||||
local out_hash = prefix..'uid0_ip_'..out_surfix
|
||||
redis.call("ZINTERSTORE", out_hash, 2, prefix.."idx_uid_group_0", prefix.."idx_ip_"..userip, 'AGGREGATE', 'MIN')
|
||||
-- and lastactivity > guestspan
|
||||
local byguestspan = redis.call("ZRANGEBYSCORE", out_hash, guestspan + 1, '+inf')
|
||||
for _, sid in ipairs(byguestspan) do
|
||||
local data = getdata(prefix..sid);
|
||||
if ((#data > 0)) then
|
||||
redis.call("del", prefix..sid)
|
||||
rs[#rs + 1] = data
|
||||
end
|
||||
end
|
||||
redis.call("DEL", out_hash)
|
||||
|
||||
local byuid = redis.call("SMEMBERS", prefix.."idx_uid_"..uid);
|
||||
for _, sid in ipairs(byuid) do
|
||||
local data = getdata(prefix..sid);
|
||||
if (#data > 0) then
|
||||
redis.call("del", prefix..sid)
|
||||
rs[#rs + 1] = data
|
||||
end
|
||||
end
|
||||
|
||||
for _, row in ipairs(rs) do
|
||||
redis.call("ZREM", prefix.."idx_ip_"..row[2], row[1])
|
||||
redis.call("ZREM", prefix.."idx_invisible_"..row[4], row[1])
|
||||
redis.call("ZREM", prefix.."idx_fid_"..row[5], row[1])
|
||||
if (row[3] == '0') then
|
||||
redis.call("ZREM", prefix.."idx_uid_group_0", row[1])
|
||||
else
|
||||
redis.call("ZREM", prefix.."idx_uid_group_1", row[1])
|
||||
end
|
||||
redis.call("ZREM", prefix.."idx_lastactivity", row[1])
|
||||
redis.call("SREM", prefix.."idx_uid_"..row[3], row[1])
|
||||
end
|
||||
|
||||
return #rs
|
||||
LUA;
|
||||
memory('eval', $script, array($session['sid'], $onlinehold, $guestspan, $session['ip'], $session['uid'] ? $session['uid'] : -1, $temp_uniq), "delete_by_session", $this->_pre_cache_key);
|
||||
}
|
||||
|
||||
public function fetch_by_uid($uid) {
|
||||
if(empty($uid)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$sids = memory('smembers', 'idx_uid_' . $uid, $this->_pre_cache_key);
|
||||
foreach ($sids as $sid) {
|
||||
return $this->get_data_by_pk($sid);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function fetch_all_by_uid($uids, $start = 0, $limit = 0) {
|
||||
if(empty($uids)) {
|
||||
return array();
|
||||
}
|
||||
|
||||
if (!is_array($uids)) {
|
||||
$uids = array($uids);
|
||||
}
|
||||
|
||||
$script = <<<LUA
|
||||
local prefix = ARGV[1]
|
||||
local start = ARGV[2]
|
||||
local limit = ARGV[3] - start + 1
|
||||
local argv_index = 4
|
||||
local sid_index = 0
|
||||
local rs = {}
|
||||
while (ARGV[argv_index]) do
|
||||
local sids = redis.call('SMEMBERS', prefix..'idx_uid_'..ARGV[argv_index])
|
||||
for _, key in ipairs(sids) do
|
||||
if (sid_index >= start) then
|
||||
local row = redis.call("hmget", prefix..key, "sid", "ip", "uid", "username", "groupid", "invisible", "action", "lastactivity", "lastolupdate", "fid", "tid")
|
||||
rs[#rs + 1] = row
|
||||
if (#rs >= limit) then
|
||||
return rs
|
||||
end
|
||||
end
|
||||
sid_index = sid_index + 1
|
||||
end
|
||||
argv_index = argv_index + 1
|
||||
end
|
||||
return rs
|
||||
LUA;
|
||||
list($ss, $ee) = $this->get_start_and_end($start, $limit);
|
||||
$data = memory('eval', $script, array_merge(array($ss, $ee), $uids), "fetch_all_by_uid", $this->_pre_cache_key);
|
||||
return $this->array_from_memory_result($data);
|
||||
}
|
||||
|
||||
public function update_by_uid($uid, $data) {
|
||||
if(!($uid = dintval($uid)) || empty($data) || !is_array($data)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$script = <<<LUA
|
||||
local prefix = ARGV[1]
|
||||
local uid = ARGV[2]
|
||||
local sids = redis.call('SMEMBERS', prefix..'idx_uid_'..uid)
|
||||
LUA;
|
||||
$r = memory('eval', $script . self::LUA_RETURN_DATA, array($uid), "update_by_uid_query", $this->_pre_cache_key);
|
||||
$items = $this->array_from_memory_result($r);
|
||||
|
||||
memory('pipeline');
|
||||
foreach ($items as $olditem) {
|
||||
$sid = $olditem['sid'];
|
||||
$data['sid'] = $sid; // $data中可能没有sid
|
||||
memory('hmset', $sid, $data, 0, $this->_pre_cache_key);
|
||||
$this->update_memory_index($sid, $data, $olditem);
|
||||
}
|
||||
memory('commit');
|
||||
}
|
||||
|
||||
public function update_max_rows($max_rows) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
public function clear() {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
public function count_by_fid($fid) {
|
||||
$fid = dintval($fid);
|
||||
if (!$fid) return 0;
|
||||
global $_G;
|
||||
$temp_uniq = substr(md5(substr(TIMESTAMP, 0, -3).substr($_G['config']['security']['authkey'], 3, -3)), 1, 8);
|
||||
$script = <<<LUA
|
||||
local prefix = ARGV[1]
|
||||
local fid = ARGV[2]
|
||||
local out_surfix = ARGV[3]
|
||||
local out_hash = prefix..'uid_fid_inv_'..out_surfix
|
||||
-- uid > 0 and fid=fid and invisible = 0
|
||||
redis.call("ZINTERSTORE", out_hash, 3, prefix.."idx_uid_group_1", prefix.."idx_fid_"..fid, prefix.."idx_invisible_0", 'AGGREGATE', 'MIN')
|
||||
local rs = redis.call("ZCARD", out_hash)
|
||||
redis.call("DEL", out_hash)
|
||||
return rs
|
||||
LUA;
|
||||
$data = memory('eval', $script, array($fid, $temp_uniq), "count_by_fid", $this->_pre_cache_key);
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function fetch_all_by_fid($fid, $limit = 12) {
|
||||
$fid = dintval($fid);
|
||||
if (!$fid) return array();
|
||||
|
||||
global $_G;
|
||||
$temp_uniq = substr(md5(substr(TIMESTAMP, 0, -3).substr($_G['config']['security']['authkey'], 3, -3)), 1, 8);
|
||||
$script = <<<LUA
|
||||
local prefix = ARGV[1]
|
||||
local fid = ARGV[2]
|
||||
local limit = ARGV[3]
|
||||
local out_surfix = ARGV[4]
|
||||
local out_hash = prefix..'uid_fid_inv_'..out_surfix
|
||||
|
||||
-- uid > 0 and fid=fid and invisible = 0
|
||||
redis.call("ZINTERSTORE", out_hash, 3, prefix.."idx_uid_group_1", prefix.."idx_fid_"..fid, prefix.."idx_invisible_0", 'AGGREGATE', 'MIN')
|
||||
|
||||
local keys = redis.call("ZREVRANGE", out_hash, 0, limit - 1)
|
||||
redis.call("DEL", out_hash)
|
||||
local rs = {}
|
||||
for _, key in ipairs(keys) do
|
||||
local row = redis.call("hmget", prefix..key, "uid", "groupid", "username", "invisible", "lastactivity")
|
||||
rs[#rs + 1] = row
|
||||
end
|
||||
return rs
|
||||
LUA;
|
||||
$data = memory('eval', $script, array($fid, $limit, $temp_uniq), "fetch_all_by_fid", $this->_pre_cache_key);
|
||||
$result = array();
|
||||
foreach ($data as $row) {
|
||||
$item = array();
|
||||
$item['uid'] = $row[0];
|
||||
$item['groupid'] = $row[1];
|
||||
$item['username'] = $row[2];
|
||||
$item['invisible'] = $row[3];
|
||||
$item['lastactivity'] = $row[4];
|
||||
$result[] = $item;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function count_by_ip($ip) {
|
||||
if (empty($ip)) return 0;
|
||||
return memory('zcard', 'idx_ip_' . $ip, $this->_pre_cache_key);
|
||||
}
|
||||
|
||||
public function fetch_all_by_ip($ip, $start = 0, $limit = 0) {
|
||||
if (empty($ip)) return array();
|
||||
|
||||
list($ss, $ee) = $this->get_start_and_end($start, $limit);
|
||||
$script = <<<LUA
|
||||
local prefix = ARGV[1]
|
||||
local ip = ARGV[2]
|
||||
local start = ARGV[3]
|
||||
local stop = ARGV[4]
|
||||
local sids = redis.call('ZREVRANGE', prefix..'idx_ip_'..ip, start, stop)
|
||||
LUA;
|
||||
$data = memory('eval', $script . self::LUA_RETURN_DATA, array($ip, $ss, $ee), "fetch_all_by_ip", $this->_pre_cache_key);
|
||||
return $this->array_from_memory_result($data);
|
||||
}
|
||||
|
||||
public function insert($data, $return_insert_id = false, $replace = false, $silent = false) {
|
||||
$id = $data['sid'];
|
||||
memory('pipeline');
|
||||
memory('hmset', $id, $data, 0, $this->_pre_cache_key);
|
||||
$this->update_memory_index($id, $data);
|
||||
memory('commit');
|
||||
}
|
||||
|
||||
public function update($val, $data, $unbuffered = false, $low_priority = false) {
|
||||
$olddata = $this->get_data_by_pk($val);
|
||||
memory('pipeline');
|
||||
memory('hmset', $val, $data, 0, $this->_pre_cache_key);
|
||||
$this->update_memory_index($val, $data, $olddata);
|
||||
memory('commit');
|
||||
}
|
||||
|
||||
private function update_memory_index($sid, $newdata, $olddata = array()) {
|
||||
if (!empty($olddata) && !isset($olddata['lastactivity'])) {
|
||||
return;
|
||||
}
|
||||
if (!empty($olddata) && !isset($newdata['lastactivity'])) {
|
||||
$newdata['lastactivity'] = $olddata['lastactivity'];
|
||||
}
|
||||
foreach ($newdata as $col => $value) {
|
||||
if (!in_array($col, array("ip", "uid", "fid", "lastactivity", "invisible"))) continue;
|
||||
if (isset($olddata[$col])) {
|
||||
if ($olddata[$col] === $value && $olddata['lastactivity'] === $newdata['lastactivity']) {
|
||||
continue;
|
||||
}
|
||||
switch ($col) {
|
||||
case 'ip':
|
||||
memory('zrem', "idx_ip_" . $olddata[$col], $sid, 0, $this->_pre_cache_key);
|
||||
break;
|
||||
case 'lastactivity':
|
||||
memory('zrem', 'idx_lastactivity', $sid, 0, $this->_pre_cache_key);
|
||||
break;
|
||||
case 'fid':
|
||||
memory('zrem', "idx_fid_" . $olddata[$col], $sid, 0, $this->_pre_cache_key);
|
||||
break;
|
||||
case 'invisible':
|
||||
memory('zrem', "idx_invisible_" . $olddata[$col], $sid, 0, $this->_pre_cache_key);
|
||||
break;
|
||||
case 'uid':
|
||||
memory('zrem', "idx_uid_group_" . ($olddata[$col] == 0 ? '0' : '1'), $sid, 0, $this->_pre_cache_key);
|
||||
memory('srem', "idx_uid_" . $olddata[$col], $sid, 0, $this->_pre_cache_key);
|
||||
break;
|
||||
default:
|
||||
continue 2;
|
||||
}
|
||||
}
|
||||
switch ($col) {
|
||||
case 'ip':
|
||||
case 'fid':
|
||||
case 'invisible':
|
||||
memory('zadd', 'idx_' . $col . "_" . $value, $sid, $newdata['lastactivity'], $this->_pre_cache_key);
|
||||
break;
|
||||
case 'lastactivity':
|
||||
memory('zadd', 'idx_lastactivity', $sid, $newdata['lastactivity'], $this->_pre_cache_key);
|
||||
break;
|
||||
case 'uid':
|
||||
memory('zadd', "idx_uid_group_" . ($value == 0 ? '0' : '1'), $sid, $newdata['lastactivity'], $this->_pre_cache_key);
|
||||
memory('sadd', 'idx_uid_' . $value, $sid, 0, $this->_pre_cache_key);
|
||||
break;
|
||||
default:
|
||||
continue 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function array_from_memory_result($data) {
|
||||
$result = array();
|
||||
foreach ($data as $row) {
|
||||
$item = array();
|
||||
$item['sid'] = $row[0];
|
||||
$item['ip'] = $row[1];
|
||||
$item['uid'] = $row[2];
|
||||
$item['username'] = $row[3];
|
||||
$item['groupid'] = $row[4];
|
||||
$item['invisible'] = $row[5];
|
||||
$item['action'] = $row[6];
|
||||
$item['lastactivity'] = $row[7];
|
||||
$item['lastolupdate'] = $row[8];
|
||||
$item['fid'] = $row[9];
|
||||
$item['tid'] = $row[10];
|
||||
$result[] = $item;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function get_data_by_pk($sid) {
|
||||
$data = memory('hgetall', $sid, $this->_pre_cache_key);
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function get_start_and_end($start, $limit) {
|
||||
$limit = intval($limit > 0 ? $limit : 0);
|
||||
$start = intval($start > 0 ? $start : 0);
|
||||
if ($start > 0 && $limit > 0) {
|
||||
return array($start, $start + $limit - 1);
|
||||
} elseif ($limit > 0) {
|
||||
return array(0, $limit - 1);
|
||||
} elseif ($start > 0) {
|
||||
return array(0, $start - 1);
|
||||
} else {
|
||||
return array(0, -1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
50
source/class/memory/memory_driver_apc.php
Normal file
50
source/class/memory/memory_driver_apc.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* [Discuz!] (C)2001-2099 Comsenz Inc.
|
||||
* This is NOT a freeware, use is subject to license terms
|
||||
*
|
||||
* $Id: memory_driver_apc.php 27635 2012-02-08 06:38:31Z zhangguosheng $
|
||||
*/
|
||||
if (!defined('IN_DISCUZ')) {
|
||||
exit('Access Denied');
|
||||
}
|
||||
|
||||
class memory_driver_apc {
|
||||
|
||||
public $cacheName = 'APC';
|
||||
public $enable;
|
||||
|
||||
public function env() {
|
||||
return function_exists('apc_cache_info') && @apc_cache_info();
|
||||
}
|
||||
|
||||
public function init($config) {
|
||||
$this->enable = $this->env();
|
||||
}
|
||||
|
||||
public function get($key) {
|
||||
return apc_fetch($key);
|
||||
}
|
||||
|
||||
public function set($key, $value, $ttl = 0) {
|
||||
return apc_store($key, $value, $ttl);
|
||||
}
|
||||
|
||||
public function rm($key) {
|
||||
return apc_delete($key);
|
||||
}
|
||||
|
||||
public function clear() {
|
||||
return apc_clear_cache('user');
|
||||
}
|
||||
|
||||
public function inc($key, $step = 1) {
|
||||
return apc_inc($key, $step) !== false ? apc_fetch($key) : false;
|
||||
}
|
||||
|
||||
public function dec($key, $step = 1) {
|
||||
return apc_dec($key, $step) !== false ? apc_fetch($key) : false;
|
||||
}
|
||||
|
||||
}
|
50
source/class/memory/memory_driver_apcu.php
Normal file
50
source/class/memory/memory_driver_apcu.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* [Discuz!] (C)2001-2099 Comsenz Inc.
|
||||
* This is NOT a freeware, use is subject to license terms
|
||||
*
|
||||
* $Id: memory_driver_apc.php 27635 2012-02-08 06:38:31Z zhangguosheng $
|
||||
*/
|
||||
if (!defined('IN_DISCUZ')) {
|
||||
exit('Access Denied');
|
||||
}
|
||||
|
||||
class memory_driver_apcu {
|
||||
|
||||
public $cacheName = 'APCu';
|
||||
public $enable;
|
||||
|
||||
public function env() {
|
||||
return function_exists('apcu_cache_info') && @apcu_cache_info();
|
||||
}
|
||||
|
||||
public function init($config) {
|
||||
$this->enable = $this->env();
|
||||
}
|
||||
|
||||
public function get($key) {
|
||||
return apcu_fetch($key);
|
||||
}
|
||||
|
||||
public function set($key, $value, $ttl = 0) {
|
||||
return apcu_store($key, $value, $ttl);
|
||||
}
|
||||
|
||||
public function rm($key) {
|
||||
return apcu_delete($key);
|
||||
}
|
||||
|
||||
public function clear() {
|
||||
return apcu_clear_cache();
|
||||
}
|
||||
|
||||
public function inc($key, $step = 1) {
|
||||
return apcu_inc($key, $step) !== false ? apcu_fetch($key) : false;
|
||||
}
|
||||
|
||||
public function dec($key, $step = 1) {
|
||||
return apcu_dec($key, $step) !== false ? apcu_fetch($key) : false;
|
||||
}
|
||||
|
||||
}
|
44
source/class/memory/memory_driver_eaccelerator.php
Normal file
44
source/class/memory/memory_driver_eaccelerator.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* [Discuz!] (C)2001-2099 Comsenz Inc.
|
||||
* This is NOT a freeware, use is subject to license terms
|
||||
*
|
||||
* $Id: memory_driver_eaccelerator.php 30457 2012-05-30 01:48:49Z zhangguosheng $
|
||||
*/
|
||||
if (!defined('IN_DISCUZ')) {
|
||||
exit('Access Denied');
|
||||
}
|
||||
|
||||
class memory_driver_eaccelerator {
|
||||
|
||||
public $cacheName = 'eAccelerator';
|
||||
public $enable;
|
||||
|
||||
public function env() {
|
||||
return function_exists('eaccelerator_get');
|
||||
}
|
||||
|
||||
public function init($config) {
|
||||
$this->enable = $this->env();
|
||||
}
|
||||
|
||||
public function get($key) {
|
||||
return eaccelerator_get($key);
|
||||
}
|
||||
|
||||
public function set($key, $value, $ttl = 0) {
|
||||
return eaccelerator_put($key, $value, $ttl);
|
||||
}
|
||||
|
||||
public function rm($key) {
|
||||
return eaccelerator_rm($key);
|
||||
}
|
||||
|
||||
public function clear() {
|
||||
return @eaccelerator_clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
112
source/class/memory/memory_driver_file.php
Normal file
112
source/class/memory/memory_driver_file.php
Normal file
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* [Discuz!] (C)2001-2099 Comsenz Inc.
|
||||
* This is NOT a freeware, use is subject to license terms
|
||||
*
|
||||
* $Id: memory_driver_yac.php 27635 2017-02-02 17:02:46Z NaiXiaoxIN $
|
||||
*/
|
||||
if (!defined('IN_DISCUZ')) {
|
||||
exit('Access Denied');
|
||||
}
|
||||
|
||||
class memory_driver_file {
|
||||
|
||||
public $cacheName = 'File';
|
||||
public $enable;
|
||||
public $path;
|
||||
|
||||
public function env() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public function init($config) {
|
||||
$this->path = $config['server'].'/';
|
||||
if($config['server']) {
|
||||
$this->enable = is_dir(DISCUZ_ROOT.$this->path);
|
||||
if(!$this->enable) {
|
||||
dmkdir(DISCUZ_ROOT.$this->path);
|
||||
$this->enable = is_dir(DISCUZ_ROOT.$this->path);
|
||||
}
|
||||
} else {
|
||||
$this->enable = false;
|
||||
}
|
||||
}
|
||||
|
||||
private function cachefile($key) {
|
||||
return str_replace('_', '/', $key).'/'.$key;
|
||||
}
|
||||
|
||||
public function get($key) {
|
||||
$file = DISCUZ_ROOT.$this->path.$this->cachefile($key).'.php';
|
||||
if(!file_exists($file)) {
|
||||
return false;
|
||||
}
|
||||
$data = null;
|
||||
@include $file;
|
||||
if($data !== null) {
|
||||
if($data['exp'] && $data['exp'] < TIMESTAMP) {
|
||||
return false;
|
||||
} else {
|
||||
return $data['data'];
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function set($key, $value, $ttl = 0) {
|
||||
$file = DISCUZ_ROOT.$this->path.$this->cachefile($key).'.php';
|
||||
dmkdir(dirname($file));
|
||||
$data = array(
|
||||
'exp' => $ttl ? TIMESTAMP + $ttl : 0,
|
||||
'data' => $value,
|
||||
);
|
||||
return file_put_contents($file, "<?php\n\$data = ".var_export($data, 1).";\n", LOCK_EX) !== false;
|
||||
}
|
||||
|
||||
public function rm($key) {
|
||||
return @unlink(DISCUZ_ROOT.$this->path.$this->cachefile($key).'.php');
|
||||
}
|
||||
|
||||
private function dir_clear($dir) {
|
||||
if($directory = @dir($dir)) {
|
||||
while($entry = $directory->read()) {
|
||||
$filename = $dir.'/'.$entry;
|
||||
if($entry != '.' && $entry != '..') {
|
||||
if(is_file($filename)) {
|
||||
@unlink($filename);
|
||||
} else {
|
||||
$this->dir_clear($filename);
|
||||
@rmdir($filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
$directory->close();
|
||||
}
|
||||
}
|
||||
|
||||
public function clear() {
|
||||
return $this->dir_clear(DISCUZ_ROOT.$this->path);
|
||||
}
|
||||
|
||||
public function inc($key, $step = 1) {
|
||||
$old = $this->get($key);
|
||||
if (!$old) {
|
||||
return false;
|
||||
}
|
||||
return $this->set($key, $old + $step);
|
||||
}
|
||||
|
||||
public function dec($key, $step = 1) {
|
||||
$old = $this->get($key);
|
||||
if (!$old) {
|
||||
return false;
|
||||
}
|
||||
return $this->set($key, $old - $step);
|
||||
}
|
||||
|
||||
public function exists($key) {
|
||||
return $this->get($key) !== false;
|
||||
}
|
||||
}
|
83
source/class/memory/memory_driver_memcache.php
Normal file
83
source/class/memory/memory_driver_memcache.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* [Discuz!] (C)2001-2099 Comsenz Inc.
|
||||
* This is NOT a freeware, use is subject to license terms
|
||||
*
|
||||
* $Id: memory_driver_memcache.php 27449 2012-02-01 05:32:35Z zhangguosheng $
|
||||
*/
|
||||
if (!defined('IN_DISCUZ')) {
|
||||
exit('Access Denied');
|
||||
}
|
||||
|
||||
class memory_driver_memcache {
|
||||
|
||||
public $cacheName = 'MemCache';
|
||||
public $enable;
|
||||
public $obj;
|
||||
|
||||
public function env() {
|
||||
return extension_loaded('memcache');
|
||||
}
|
||||
|
||||
public function init($config) {
|
||||
if (!$this->env()) {
|
||||
$this->enable = false;
|
||||
return;
|
||||
}
|
||||
if (!empty($config['server'])) {
|
||||
$this->obj = new Memcache;
|
||||
if ($config['pconnect']) {
|
||||
$connect = @$this->obj->pconnect($config['server'], $config['port']);
|
||||
} else {
|
||||
$connect = @$this->obj->connect($config['server'], $config['port']);
|
||||
}
|
||||
$this->enable = $connect ? true : false;
|
||||
}
|
||||
}
|
||||
|
||||
public function get($key) {
|
||||
return $this->obj->get($key);
|
||||
}
|
||||
|
||||
public function getMulti($keys) {
|
||||
return $this->obj->get($keys);
|
||||
}
|
||||
|
||||
public function set($key, $value, $ttl = 0) {
|
||||
return $this->obj->set($key, $value, 0, $ttl);
|
||||
}
|
||||
|
||||
public function add($key, $value, $ttl = 0) {
|
||||
return $this->obj->add($key, $value, 0, $ttl);
|
||||
}
|
||||
|
||||
public function rm($key) {
|
||||
return $this->obj->delete($key);
|
||||
}
|
||||
|
||||
public function clear() {
|
||||
return $this->obj->flush();
|
||||
}
|
||||
|
||||
public function inc($key, $step = 1) {
|
||||
if (!$this->obj->increment($key, $step)) {
|
||||
$this->set($key, $step);
|
||||
}
|
||||
}
|
||||
|
||||
public function incex($key, $step = 1) {
|
||||
return $this->obj->increment($key, $step);
|
||||
}
|
||||
|
||||
public function dec($key, $step = 1) {
|
||||
return $this->obj->decrement($key, $step);
|
||||
}
|
||||
|
||||
public function exists($key) {
|
||||
return $this->obj->get($key) !== FALSE;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
81
source/class/memory/memory_driver_memcached.php
Normal file
81
source/class/memory/memory_driver_memcached.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* [Discuz!] (C)2001-2099 Comsenz Inc.
|
||||
* This is NOT a freeware, use is subject to license terms
|
||||
*
|
||||
* $Id: memory_driver_memcached.php 27449 2017-07-11 05:32:35Z ladyff $
|
||||
*/
|
||||
|
||||
if(!defined('IN_DISCUZ')) {
|
||||
exit('Access Denied');
|
||||
}
|
||||
|
||||
class memory_driver_memcached
|
||||
{
|
||||
public $cacheName = 'MemCached';
|
||||
public $enable;
|
||||
public $obj;
|
||||
|
||||
public function env() {
|
||||
return extension_loaded('memcached');
|
||||
}
|
||||
public function init($config) {
|
||||
if (!$this->env()) {
|
||||
$this->enable = false;
|
||||
return;
|
||||
}
|
||||
if(!empty($config['server'])) {
|
||||
$this->obj = new Memcached;
|
||||
$this->obj->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
|
||||
$this->obj->setOption(Memcached::OPT_TCP_NODELAY, true);
|
||||
$this->obj->addServer($config['server'], $config['port']);
|
||||
$connect=$this->obj->set('connect', '1');
|
||||
$this->enable = $connect ? true : false;
|
||||
}
|
||||
}
|
||||
|
||||
public function get($key) {
|
||||
return $this->obj->get($key);
|
||||
}
|
||||
|
||||
public function getMulti($keys) {
|
||||
return $this->obj->getMulti($keys);
|
||||
}
|
||||
|
||||
public function set($key, $value, $ttl = 0) {
|
||||
return $this->obj->set($key, $value, $ttl);
|
||||
}
|
||||
|
||||
public function add($key, $value, $ttl = 0) {
|
||||
return $this->obj->add($key, $value, $ttl);
|
||||
}
|
||||
|
||||
public function rm($key) {
|
||||
return $this->obj->delete($key);
|
||||
}
|
||||
|
||||
public function clear() {
|
||||
return $this->obj->flush();
|
||||
}
|
||||
|
||||
public function inc($key, $step = 1) {
|
||||
return $this->obj->increment($key, $step, $step);
|
||||
}
|
||||
|
||||
public function incex($key, $step = 1) {
|
||||
return $this->obj->increment($key, $step);
|
||||
}
|
||||
|
||||
public function dec($key, $step = 1) {
|
||||
return $this->obj->decrement($key, $step);
|
||||
}
|
||||
|
||||
public function exists($key) {
|
||||
$this->obj->get($key);
|
||||
return \Memcached::RES_NOTFOUND !== $this->obj->getResultCode();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
293
source/class/memory/memory_driver_redis.php
Normal file
293
source/class/memory/memory_driver_redis.php
Normal file
@@ -0,0 +1,293 @@
|
||||
<?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 $
|
||||
*/
|
||||
if (!defined('IN_DISCUZ')) {
|
||||
exit('Access Denied');
|
||||
}
|
||||
|
||||
class memory_driver_redis {
|
||||
|
||||
public $cacheName = 'Redis';
|
||||
var $enable;
|
||||
var $obj;
|
||||
|
||||
public function env() {
|
||||
return extension_loaded('redis');
|
||||
}
|
||||
|
||||
function init($config) {
|
||||
if(!$this->env()) {
|
||||
$this->enable = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!empty($config['server'])) {
|
||||
try {
|
||||
$this->obj = new Redis();
|
||||
if ($config['pconnect']) {
|
||||
$connect = @$this->obj->pconnect($config['server'], $config['port']);
|
||||
} else {
|
||||
$connect = @$this->obj->connect($config['server'], $config['port']);
|
||||
}
|
||||
} catch (RedisException $e) {
|
||||
|
||||
}
|
||||
$this->enable = $connect ? true : false;
|
||||
if ($this->enable) {
|
||||
if ($config['requirepass']) {
|
||||
$this->obj->auth($config['requirepass']);
|
||||
}
|
||||
@$this->obj->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE);
|
||||
$this->select(isset($config['db']) ? $config['db'] : 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function feature($feature) {
|
||||
switch ($feature) {
|
||||
case 'set':
|
||||
case 'hash':
|
||||
case 'sortedset':
|
||||
case 'pipeline':
|
||||
return true;
|
||||
case 'eval':
|
||||
$ret = $this->obj->eval("return 1");
|
||||
return ($ret === 1);
|
||||
case 'cluster':
|
||||
$ret = $this->obj->info("cluster");
|
||||
return ($ret['cluster_enabled'] === 1);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static function &instance() {
|
||||
static $object;
|
||||
if (empty($object)) {
|
||||
$object = new memory_driver_redis();
|
||||
$object->init(getglobal('config/memory/redis'));
|
||||
}
|
||||
return $object;
|
||||
}
|
||||
|
||||
function get($key) {
|
||||
if (is_array($key)) {
|
||||
return $this->getMulti($key);
|
||||
}
|
||||
return $this->_try_deserialize($this->obj->get($key));
|
||||
}
|
||||
|
||||
function getMulti($keys) {
|
||||
$result = $this->obj->mGet($keys);
|
||||
$newresult = array();
|
||||
$index = 0;
|
||||
foreach ($keys as $key) {
|
||||
if ($result[$index] !== false) {
|
||||
$newresult[$key] = $this->_try_deserialize($result[$index]);
|
||||
}
|
||||
$index++;
|
||||
}
|
||||
unset($result);
|
||||
return $newresult;
|
||||
}
|
||||
|
||||
function select($db = 0) {
|
||||
return $this->obj->select($db);
|
||||
}
|
||||
|
||||
function set($key, $value, $ttl = 0) {
|
||||
if (is_array($value)) {
|
||||
$value = serialize($value);
|
||||
}
|
||||
if ($ttl) {
|
||||
return $this->obj->setex($key, $ttl, $value);
|
||||
} else {
|
||||
return $this->obj->set($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
function add($key, $value, $ttl = 0) {
|
||||
if ($ttl > 0) return $this->obj->set($key, $value, array('nx', 'ex' => $ttl));
|
||||
return $this->obj->setnx($key, $value);
|
||||
}
|
||||
|
||||
function rm($key) {
|
||||
return $this->obj->del($key);
|
||||
}
|
||||
|
||||
function setMulti($arr, $ttl = 0) {
|
||||
if (!is_array($arr)) {
|
||||
return FALSE;
|
||||
}
|
||||
foreach ($arr as $key => $v) {
|
||||
$this->set($key, $v, $ttl);
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
function inc($key, $step = 1) {
|
||||
return $this->obj->incr($key, $step);
|
||||
}
|
||||
|
||||
function incex($key, $step = 1) {
|
||||
$script = "if redis.call('exists', ARGV[1]) == 1 then return redis.call('incrby', ARGV[1], ARGV[2]) end";
|
||||
return $this->evalscript($script, array($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 srem($key, $value) {
|
||||
return $this->obj->sRem($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);
|
||||
}
|
||||
|
||||
function expire($key, $second) {
|
||||
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 hmset($key, $value) {
|
||||
return $this->obj->hMSet($key, $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);
|
||||
}
|
||||
|
||||
function hIncrBy($key, $field, $incr) {
|
||||
return $this->obj->hIncrBy($key, $field, $incr);
|
||||
}
|
||||
|
||||
function hgetall($key) {
|
||||
return $this->obj->hGetAll($key);
|
||||
}
|
||||
|
||||
function hget($key, $field) {
|
||||
return $this->obj->hGet($key, $field);
|
||||
}
|
||||
|
||||
function hexists($key, $field) {
|
||||
return $this->obj->hExists($key, $field);
|
||||
}
|
||||
|
||||
function evalscript($script, $argv) {
|
||||
return $this->obj->eval($script, $argv);
|
||||
}
|
||||
|
||||
function evalsha($sha, $argv) {
|
||||
return $this->obj->evalSha($sha, $argv);
|
||||
}
|
||||
|
||||
function loadscript($script) {
|
||||
return $this->obj->script('load', $script);
|
||||
}
|
||||
|
||||
function scriptexists($sha) {
|
||||
$r = $this->obj->script('exists', $sha);
|
||||
return $r[0];
|
||||
}
|
||||
|
||||
function zadd($key, $member, $score) {
|
||||
return $this->obj->zAdd($key, $score, $member);
|
||||
}
|
||||
|
||||
function zrem($key, $member) {
|
||||
return $this->obj->zRem($key, $member);
|
||||
}
|
||||
|
||||
function zscore($key, $member) {
|
||||
return $this->obj->zScore($key, $member);
|
||||
}
|
||||
|
||||
function zcard($key) {
|
||||
return $this->obj->zCard($key);
|
||||
}
|
||||
|
||||
function zrevrange($key, $start, $end, $withscore = false) {
|
||||
return $this->obj->zRevRange($key, $start, $end, $withscore);
|
||||
}
|
||||
|
||||
function zincrby($key, $member, $value) {
|
||||
return $this->obj->zIncrBy($key, $value, $member);
|
||||
}
|
||||
|
||||
function sort($key, $opt) {
|
||||
return $this->obj->sort($key, $opt);
|
||||
}
|
||||
|
||||
function exists($key) {
|
||||
return $this->obj->exists($key);
|
||||
}
|
||||
|
||||
function clear() {
|
||||
return $this->obj->flushDb();
|
||||
}
|
||||
|
||||
function pipeline() {
|
||||
return $this->obj->multi(Redis::PIPELINE);
|
||||
}
|
||||
|
||||
function commit() {
|
||||
return $this->obj->exec();
|
||||
}
|
||||
|
||||
function discard() {
|
||||
return $this->obj->discard();
|
||||
}
|
||||
|
||||
private function _try_deserialize($data) {
|
||||
try {
|
||||
$ret = dunserialize($data);
|
||||
if ($ret === FALSE) {
|
||||
return $data;
|
||||
}
|
||||
return $ret;
|
||||
} catch (Exception $e) {
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
54
source/class/memory/memory_driver_wincache.php
Normal file
54
source/class/memory/memory_driver_wincache.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* [Discuz!] (C)2001-2099 Comsenz Inc.
|
||||
* This is NOT a freeware, use is subject to license terms
|
||||
*
|
||||
* $Id: memory_driver_wincache.php 31432 2012-08-28 03:04:18Z zhangguosheng $
|
||||
*/
|
||||
if (!defined('IN_DISCUZ')) {
|
||||
exit('Access Denied');
|
||||
}
|
||||
|
||||
class memory_driver_wincache {
|
||||
|
||||
public $cacheName = 'WinCache';
|
||||
public $enable;
|
||||
|
||||
public function env() {
|
||||
return function_exists('wincache_ucache_meminfo') && wincache_ucache_meminfo();
|
||||
}
|
||||
|
||||
public function init($config) {
|
||||
$this->enable = $this->env();
|
||||
}
|
||||
|
||||
public function get($key) {
|
||||
return wincache_ucache_get($key);
|
||||
}
|
||||
|
||||
public function getMulti($keys) {
|
||||
return wincache_ucache_get($keys);
|
||||
}
|
||||
|
||||
public function set($key, $value, $ttl = 0) {
|
||||
return wincache_ucache_set($key, $value, $ttl);
|
||||
}
|
||||
|
||||
public function rm($key) {
|
||||
return wincache_ucache_delete($key);
|
||||
}
|
||||
|
||||
public function clear() {
|
||||
return wincache_ucache_clear();
|
||||
}
|
||||
|
||||
public function inc($key, $step = 1) {
|
||||
return wincache_ucache_inc($key, $step);
|
||||
}
|
||||
|
||||
public function dec($key, $step = 1) {
|
||||
return wincache_ucache_dec($key, $step);
|
||||
}
|
||||
|
||||
}
|
50
source/class/memory/memory_driver_xcache.php
Normal file
50
source/class/memory/memory_driver_xcache.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* [Discuz!] (C)2001-2099 Comsenz Inc.
|
||||
* This is NOT a freeware, use is subject to license terms
|
||||
*
|
||||
* $Id: memory_driver_xcache.php 27449 2012-02-01 05:32:35Z zhangguosheng $
|
||||
*/
|
||||
if (!defined('IN_DISCUZ')) {
|
||||
exit('Access Denied');
|
||||
}
|
||||
|
||||
class memory_driver_xcache {
|
||||
|
||||
public $cacheName = 'XCache';
|
||||
public $enable;
|
||||
|
||||
public function env() {
|
||||
return function_exists('xcache_get');
|
||||
}
|
||||
|
||||
public function init($config) {
|
||||
$this->enable = $this->env();
|
||||
}
|
||||
|
||||
public function get($key) {
|
||||
return xcache_get($key);
|
||||
}
|
||||
|
||||
public function set($key, $value, $ttl = 0) {
|
||||
return xcache_set($key, $value, $ttl);
|
||||
}
|
||||
|
||||
public function rm($key) {
|
||||
return xcache_unset($key);
|
||||
}
|
||||
|
||||
public function clear() {
|
||||
return xcache_clear_cache(XC_TYPE_VAR, 0);
|
||||
}
|
||||
|
||||
public function inc($key, $step = 1) {
|
||||
return xcache_inc($key, $step);
|
||||
}
|
||||
|
||||
public function dec($key, $step = 1) {
|
||||
return xcache_dec($key, $step);
|
||||
}
|
||||
|
||||
}
|
72
source/class/memory/memory_driver_yac.php
Normal file
72
source/class/memory/memory_driver_yac.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* [Discuz!] (C)2001-2099 Comsenz Inc.
|
||||
* This is NOT a freeware, use is subject to license terms
|
||||
*
|
||||
* $Id: memory_driver_yac.php 27635 2017-02-02 17:02:46Z NaiXiaoxIN $
|
||||
*/
|
||||
if (!defined('IN_DISCUZ')) {
|
||||
exit('Access Denied');
|
||||
}
|
||||
|
||||
class memory_driver_yac {
|
||||
|
||||
public $cacheName = 'Yac';
|
||||
private $object = null;
|
||||
public $enable;
|
||||
|
||||
public function env() {
|
||||
return extension_loaded('Yac');
|
||||
}
|
||||
|
||||
public function init($config) {
|
||||
$this->enable = $this->env();
|
||||
if ($this->enable) {
|
||||
$this->object = new yac();
|
||||
}
|
||||
}
|
||||
|
||||
public function get($key) {
|
||||
return $this->object->get($key);
|
||||
}
|
||||
|
||||
public function getMulti($keys) {
|
||||
$result = $this->object->get($keys);
|
||||
foreach ($result as $key => $value) {
|
||||
if ($value === false) {
|
||||
unset($result[$key]);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function set($key, $value, $ttl = 0) {
|
||||
return $this->object->set($key, $value, $ttl);
|
||||
}
|
||||
|
||||
public function rm($key) {
|
||||
return $this->object->delete($key);
|
||||
}
|
||||
|
||||
public function clear() {
|
||||
return $this->object->flush();
|
||||
}
|
||||
|
||||
public function inc($key, $step = 1) {
|
||||
$old = $this->get($key);
|
||||
if (!$old) {
|
||||
return false;
|
||||
}
|
||||
return $this->set($key, $old + $step);
|
||||
}
|
||||
|
||||
public function dec($key, $step = 1) {
|
||||
$old = $this->get($key);
|
||||
if (!$old) {
|
||||
return false;
|
||||
}
|
||||
return $this->set($key, $old - $step);
|
||||
}
|
||||
|
||||
}
|
139
source/class/memory/memory_setting_array.php
Normal file
139
source/class/memory/memory_setting_array.php
Normal file
@@ -0,0 +1,139 @@
|
||||
<?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 $
|
||||
*/
|
||||
if (!defined('IN_DISCUZ')) {
|
||||
exit('Access Denied');
|
||||
}
|
||||
|
||||
class memory_setting_array implements ArrayAccess {
|
||||
private $can_lazy = false;
|
||||
public $array = Array();
|
||||
|
||||
const SETTING_KEY = 'setting';
|
||||
const FIELDS_GROUPS = array(
|
||||
'common_fields' => array(
|
||||
'memory', 'ipaccess', 'mobile', 'domain', 'timeoffset', 'dateformat', 'timeformat', 'bbclosed',
|
||||
'topicperpage', 'postperpage', 'nocacheheaders', 'seokeywords', 'seodescription', 'forumpicstyle',
|
||||
'rssstatus', 'plugins', 'hookscript', 'pluginhooks', 'bbname', 'seotitle', 'indexhot', 'collectionstatus',
|
||||
'grid', 'dateconvert', 'moddisplay', 'whosonlinestatus', 'oltimespan', 'maxonlinelist', 'seohead',
|
||||
'version', 'showusercard', 'disallowfloat', 'creditnotice', 'creditnames', 'jspath', 'csspath',
|
||||
'portalstatus', 'navs', 'groupstatus', 'feedstatus', 'archiver', 'switchwidthauto', 'shortcut', 'topnavs',
|
||||
'mynavs', 'showfjump', 'advtype', 'navmns', 'navdms', 'navmn', 'navlogos', 'avatarmethod', 'ucenterurl',
|
||||
'connect', 'taskstatus', 'menunavs', 'subnavs', 'search', 'blogstatus', 'albumstatus', 'srchhotkeywords',
|
||||
'forumallowside', 'focus', 'site_qq', 'footernavs', 'siteurl', 'sitename', 'icp', 'statcode', 'debug',
|
||||
'boardlicensed', 'followstatus', 'disableipnotice', 'rewritestatus', 'ftp', 'visitbanperiods', 'dynavt',
|
||||
'cacheindexlife', 'whosonline_contract', 'regname', 'reglinkname', 'autoidselect', 'avatarurl', 'avatarpath',
|
||||
'uidlogin', 'secmobilelogin', 'forumstatus', 'friendstatus', 'guidestatus', 'favoritestatus', 'mps', 'mpsid'
|
||||
),
|
||||
'forumdisplay_fields' => array(
|
||||
'group_admingroupids', 'followforumid', 'targetblank', 'allowmoderatingthread', 'threadmaxpages',
|
||||
'globalstick', 'recommendthread', 'heatthread', 'verify', 'visitedforums', 'fastpost', 'seccodedata',
|
||||
'secqaa', 'creditstransextra', 'extcredits', 'threadplugins', 'leftsidewidth', 'forumseparator',
|
||||
'forumdisplaythreadpreview', 'closeforumorderby', 'minpostsize', 'maxpostsize', 'fastsmilies', 'smcols',
|
||||
'allowreplybg', 'newbiespan', 'minpostsize_mobile', 'minsubjectsize', 'maxsubjectsize',
|
||||
),
|
||||
'viewthread_fields' => array(
|
||||
'optimizeviews', 'antitheft', 'cachethreadlife', 'close_leftinfo', 'close_leftinfo_userctrl',
|
||||
'creditspolicy', 'modratelimit', 'showsettings', 'preventrefresh', 'infosidestatus',
|
||||
'threadhotreplies', 'nofilteredpost', 'threadblacklist', 'threadguestlite', 'threadfilternum',
|
||||
'ratelogrecord', 'commentnumber', 'sigviewcond', 'lazyload', 'allowattachurl', 'relatedlinkstatus',
|
||||
'numbercard', 'repliesrank', 'vtonlinestatus', 'alloweditpost', 'zoomstatus', 'imagemaxwidth',
|
||||
'bannedmessages', 'authoronleft', 'profilenode', 'magicstatus', 'starthreshold', 'allowfastreply',
|
||||
'sharestatus', 'globalsightml', 'need_avatar', 'need_secmobile', 'need_email', 'need_friendnum'
|
||||
)
|
||||
);
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->can_lazy = C::memory()->goteval && C::memory()->gothash;
|
||||
if (!$this->can_lazy) {
|
||||
$this->array = memory('get', self::SETTING_KEY);
|
||||
foreach ($this->array as $key => $value) {
|
||||
if ($value !== null && $value !== '') {
|
||||
$unserializedValue = dunserialize($value);
|
||||
if ($unserializedValue !== false) {
|
||||
$this->array[$key] = $unserializedValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function offsetExists($index)
|
||||
{
|
||||
if (!array_key_exists($index, $this->array)) {
|
||||
return memory('hexists', self::SETTING_KEY, $index);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function &offsetGet($index)
|
||||
{
|
||||
$val = $this->array[$index];
|
||||
if ($val === null && $this->can_lazy) {
|
||||
foreach (self::FIELDS_GROUPS as $group => $fields) {
|
||||
if (in_array($index, $fields)) {
|
||||
$this->_load_fields($fields, 'setting_' . $group);
|
||||
$val = $this->array[$index];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($val === null) {
|
||||
$data = memory('hget', self::SETTING_KEY, $index);
|
||||
$val = dunserialize($data);
|
||||
$this->offsetSet($index, $val);
|
||||
}
|
||||
}
|
||||
$ret = & $this->array[$index];
|
||||
return $ret;
|
||||
}
|
||||
|
||||
public function offsetSet($index, $newval)
|
||||
{
|
||||
if ($newval === null || $newval === false) $newval = Array();
|
||||
$this->array[$index] = $newval;
|
||||
}
|
||||
|
||||
public function offsetUnset($index)
|
||||
{
|
||||
unset($this->array[$index]);
|
||||
}
|
||||
|
||||
public static function save($data)
|
||||
{
|
||||
$can_lazy = C::memory()->goteval && C::memory()->gothash;
|
||||
if ($can_lazy) {
|
||||
$newdata = array();
|
||||
foreach ($data as $key => $value) {
|
||||
$newdata[$key] = serialize($value);
|
||||
}
|
||||
memory('hmset', self::SETTING_KEY, $newdata);
|
||||
} else {
|
||||
memory('set', self::SETTING_KEY, $data);//memcached不支持key为array
|
||||
}
|
||||
}
|
||||
|
||||
private function _load_fields($fields, $shakey)
|
||||
{
|
||||
$data = memory('eval', false, array(), $shakey);
|
||||
if (!$data) {
|
||||
$array_def = "local fields = {" . array_reduce($fields, function($carry, $item) {
|
||||
return $carry . '"' . $item . '",';
|
||||
}) . "} ";
|
||||
$script = <<<LUA
|
||||
local prefix = ARGV[1]
|
||||
return redis.call('hmget', prefix..'setting', unpack(fields))
|
||||
LUA;
|
||||
$data = memory('eval', $array_def . $script, array(), $shakey);
|
||||
}
|
||||
foreach ($fields as $index => $field) {
|
||||
$this->offsetSet($field, dunserialize($data[$index]));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user