cache redis git-svn-id: https://torrentpier2.googlecode.com/svn/trunk@102 a8ac35ab-4ca4-ca47-4c2d-a49a94f06293
This commit is contained in:
nanosimbiot 2011-07-13 18:27:55 +00:00
commit b3891ccc9f
5 changed files with 178 additions and 17 deletions

View file

@ -179,6 +179,19 @@ class CACHES
$this->ref[$cache_name] =& $this->obj[$cache_name];
break;
case 'redis':
if (!isset($this->obj[$cache_name]))
{
$cache_cfg = array(
'host' => '127.0.0.1',
'port' => 6379,
'con_required' => true,
);
$this->obj[$cache_name] = new cache_redis($cache_cfg);
}
$this->ref[$cache_name] =& $this->obj[$cache_name];
break;
case 'filecache':
if (!isset($this->obj[$cache_name]))
{
@ -618,25 +631,73 @@ class cache_dbg_common
}
}
function sql_dbg_enabled ()
class cache_redis extends cache_common
{
return (SQL_DEBUG && DBG_USER && !empty($_COOKIE['sql_log']));
}
var $used = true;
function short_query ($sql, $esc_html = false)
{
$max_len = 2500;
$sql = str_compact($sql);
var $cfg = null;
var $redis = null;
var $connected = false;
if (empty($_COOKIE['sql_log_full']))
function cache_redis ($cfg)
{
if (strlen($sql) > $max_len)
global $bb_cfg;
if (!$this->is_installed())
{
$sql = substr($sql, 0, $max_len-500) .' [...cut...] '. substr($sql, -480);
die('Error: Redis extension not installed');
}
$this->cfg = $cfg;
$this->redis = new Redis();
}
function connect ()
{
if (@$this->redis->connect($this->cfg['host'], $this->cfg['port']))
{
$this->connected = true;
}
if (!$this->connected && $this->cfg['con_required'])
{
die('Could not connect to redis server');
}
}
return ($esc_html) ? htmlCHR($sql, true) : $sql;
function get ($name)
{
if (!$this->connected) $this->connect();
return ($this->connected) ? unserialize($this->redis->get($name)) : false;
}
function set ($name, $value, $ttl = 0)
{
if (!$this->connected) $this->connect();
if($this->redis->set($name, serialize($value)))
{
if ($ttl > 0)
{
$this->redis->expire($name, $ttl);
}
return true;
}
else
{
return false;
}
}
function rm ($name)
{
if (!$this->connected) $this->connect();
return ($this->connected) ? $this->redis->del($name) : false;
}
function is_installed ()
{
return class_exists('Redis');
}
}
class cache_file extends cache_common
@ -724,6 +785,26 @@ class cache_file extends cache_common
}
}
function sql_dbg_enabled ()
{
return (SQL_DEBUG && DBG_USER && !empty($_COOKIE['sql_log']));
}
function short_query ($sql, $esc_html = false)
{
$max_len = 2500;
$sql = str_compact($sql);
if (empty($_COOKIE['sql_log_full']))
{
if (strlen($sql) > $max_len)
{
$sql = substr($sql, 0, $max_len-500) .' [...cut...] '. substr($sql, -480);
}
}
return ($esc_html) ? htmlCHR($sql, true) : $sql;
}
// Functions
function utime ()
{
@ -867,7 +948,7 @@ function verify_ip ($ip)
function str_compact ($str)
{
return preg_replace('#\s+#', ' ', trim($str));
return preg_replace('#\s+#u', ' ', trim($str));
}
function make_rand_str ($len = 10)

View file

@ -57,8 +57,8 @@ $bb_cfg['css_ver'] = 1;
// Increase number of revision after update
$bb_cfg['tp_version'] = '2.0.2';
$bb_cfg['tp_release_state'] = 'TP II r101';
$bb_cfg['tp_release_date'] = '13-07-2011';
$bb_cfg['tp_release_state'] = 'TP II r102';
$bb_cfg['tp_release_date'] = '14-07-2011';
$bb_cfg['board_disabled_msg'] = 'форум временно отключен'; // 'forums temporarily disabled'; // show this msg if board has been disabled via ON/OFF trigger
$bb_cfg['srv_overloaded_msg'] = "Извините, в данный момент сервер перегружен\nПопробуйте повторить запрос через несколько минут";
@ -80,7 +80,7 @@ $bb_cfg['db_alias'] = array(
$bb_cfg['cache']['pconnect'] = false;
$bb_cfg['cache']['db_dir'] = realpath(BB_ROOT) .'/cache/filecache/';
// Available cache types: sqlite, db_sqlite, memcache, filecache
// Available cache types: sqlite, db_sqlite, memcache, redis, filecache
# name => array( (string) type, (array) cfg )
$bb_cfg['cache']['engines'] = array(
'bb_cache' => array('filecache', array()),
@ -104,6 +104,11 @@ $bb_cfg['datastore']['memcache'] = array(
'pconnect' => true, // use persistent connection
'con_required' => true, // exit script if can't connect
);
$bb_cfg['datastore']['redis'] = array(
'host' => '127.0.0.1',
'port' => 6379,
'con_required' => true,
);
// Tracker
$bb_cfg['announce_type'] = 'php'; // Тип анонсера, xbt или php

View file

@ -53,8 +53,10 @@ $del_ids = DB()->fetch_rowset("SELECT cap_id FROM ". BB_CAPTCHA ." WHERE cap_id
foreach ($del_ids as $del_id)
{
$cap_img_path = CAPTCHA()->get_img_path(abs($del_id));
if(@fopen($cap_img_path, 'r'))
{
unlink($cap_img_path);
}
DB()->query("DELETE FROM ". BB_CAPTCHA ." WHERE cap_id = $del_id LIMIT 1");
}

View file

@ -389,6 +389,75 @@ class datastore_sqlite extends datastore_common
}
}
class datastore_redis extends datastore_common
{
var $cfg = null;
var $redis = null;
var $connected = false;
function datastore_redis ($cfg)
{
global $bb_cfg;
if (!$this->is_installed())
{
die('Error: Redis extension not installed');
}
$this->cfg = $cfg;
$this->redis = new Redis();;
}
function connect ()
{
if (@$this->redis->connect($this->cfg['host'],$this->cfg['port']))
{
$this->connected = true;
}
if (!$this->connected && $this->cfg['con_required'])
{
die('Could not connect to redis server');
}
}
function store ($title, $var)
{
if (!$this->connected) $this->connect();
$this->data[$title] = $var;
return (bool) $this->redis->set($title, serialize($var));
}
function clean ()
{
if (!$this->connected) $this->connect();
foreach ($this->known_items as $title => $script_name)
{
$this->redis->del($title);
}
}
function _fetch_from_store ()
{
if (!$items = $this->queued_items)
{
$src = $this->_debug_find_caller('enqueue');
trigger_error("Datastore: item '$item' already enqueued [$src]", E_USER_ERROR);
}
if (!$this->connected) $this->connect();
foreach ($items as $item)
{
$this->data[$item] = unserialize($this->redis->get($item));
}
}
function is_installed ()
{
return class_exists('Redis');
}
}
class datastore_file extends datastore_common
{
var $dir = null;

View file

@ -557,6 +557,10 @@ switch ($bb_cfg['datastore_type'])
$datastore = new datastore_memcache($bb_cfg['datastore']['memcache']);
break;
case 'redis':
$datastore = new datastore_redis($bb_cfg['datastore']['redis']);
break;
case 'filecache':
$datastore = new datastore_file($bb_cfg['cache']['db_dir'] . 'datastore/');
break;