memcache git-svn-id: https://torrentpier2.googlecode.com/svn/trunk@96 a8ac35ab-4ca4-ca47-4c2d-a49a94f06293
This commit is contained in:
nanosimbiot 2011-07-11 17:55:35 +00:00
commit 62a5fd246f
4 changed files with 105 additions and 269 deletions

View file

@ -141,33 +141,21 @@ class CACHES
switch ($cache_type)
{
case 'cache_memcache':
if (!$mc_name =& $cache_cfg[0])
case 'memcache':
if (!isset($this->obj[$cache_name]))
{
trigger_error("empty mc_name for $cache_name cache", E_USER_ERROR);
$cache_cfg = array(
'host' => '127.0.0.1',
'port' => 11211,
'pconnect' => true, // use persistent connection
'con_required' => true, // exit script if can't connect
);
$this->obj[$cache_name] = new cache_memcache($cache_cfg);
}
if (!$mc_cfg =& $this->cfg['memcache'][$mc_name])
{
trigger_error("mc_cfg for $cache_name not found", E_USER_ERROR);
}
if (!isset($this->obj[$mc_name]))
{
$servers = (array) $mc_cfg[0];
$pconnect = $mc_cfg[1];
$con_required = $mc_cfg[2];
$this->obj[$mc_name] = new cache_memcache(array(
'mc_name' => $mc_name,
'mc_class' => $this->cfg['mc_class'], // $bb_cfg['cache']['mc_class']
'servers' => $servers,
'pconnect' => $pconnect, // $bb_cfg['cache']['pconnect']
'con_required' => $con_required,
));
}
$this->ref[$cache_name] =& $this->obj[$mc_name];
$this->ref[$cache_name] =& $this->obj[$cache_name];
break;
case 'cache_sqlite':
case 'sqlite':
if (!isset($this->obj[$cache_name]))
{
$cache_cfg['pconnect'] = $this->cfg['pconnect'];
@ -263,244 +251,63 @@ class cache_common
class cache_memcache extends cache_common
{
var $cfg = array(
'mc_class' => 'class_name', // $bb_cfg['cache']['mc_class']
);
var $used = true;
var $db = null;
var $used = true;
var $cfg = null;
var $memcache = null;
var $connected = false;
function cache_memcache ($cfg)
{
$this->cfg = array_merge($this->cfg, $cfg);
$this->db = new $this->cfg['mc_class']($this->cfg);
global $bb_cfg;
if (!$this->is_installed())
{
die('Error: Memcached extension not installed');
}
$this->cfg = $cfg;
$this->memcache = new Memcache;
}
function get ($key, $get_miss_key_callback = '', $prefix = '', $ttl = 604800)
function connect ()
{
if (empty($key))
{
return is_array($key) ? array() : false;
}
$cached_items = array();
$prefix_len = strlen($prefix);
$key_ary = (array) $key;
$key_get = array();
$connect_type = ($this->cfg['pconnect']) ? 'pconnect' : 'connect';
foreach ($key_ary as $k)
if (@$this->memcache->$connect_type($this->cfg['host'], $this->cfg['port']))
{
$key_get[] = $prefix . $k;
$this->connected = true;
}
// get available items
foreach ($this->db->get($key_get) as $k => $v)
{
$cached_items[substr($k, $prefix_len)] = $v;
}
if (DBG_LOG) dbg_log(' ', 'CACHE-connect'. ($this->connected ? '' : '-FAIL'));
// get miss items
if ($get_miss_key_callback AND $miss_key = array_diff($key_ary, array_keys($cached_items)))
if (!$this->connected && $this->cfg['con_required'])
{
foreach ($get_miss_key_callback($miss_key) as $k => $v)
{
$this->set($prefix.$k, $v, $ttl);
$cached_items[$k] = $v;
}
}
// return
if (is_array($key))
{
return $cached_items;
}
else
{
return isset($cached_items[$key]) ? $cached_items[$key] : false;
die('Could not connect to memcached server');
}
}
function set ($key, $value, $ttl = 604800, $prefix = '')
function get ($name)
{
return $this->db->set($prefix.$key, $value, $ttl);
if (!$this->connected) $this->connect();
return ($this->connected) ? $this->memcache->get($name) : false;
}
function rm ($key, $prefix = '')
function set ($name, $value, $ttl = 0)
{
return $this->db->rm($prefix.$key);
}
}
class memcache_common extends cache_dbg_common
{
var $cfg = array(
'mc_name' => null, // $bb_cfg['cache']['memcache'][ key ]
'servers' => array('host:port'), // $bb_cfg['cache']['memcache'][ val ]
'pconnect' => false, // $bb_cfg['cache']['pconnect']
'con_required' => false,
'log_name' => 'memcache',
);
var $engine = 'Memcache';
var $mc = null;
function memcache_common ($cfg = array())
{
$this->mc = new Memcache();
$this->cfg = array_merge($this->cfg, $cfg);
$this->dbg_enabled = sql_dbg_enabled();
$this->init();
if (!$this->connected) $this->connect();
return ($this->connected) ? $this->memcache->set($name, $value, false, $ttl) : false;
}
function init ()
function rm ($name)
{
$this->cur_query = ($this->dbg_enabled) ? "addServer(". join(", ", (array)$this->cfg['servers']) .", ". (int) $this->cfg['pconnect'] .", ". (int) $this->cfg['con_required'] .")" : '';
$this->debug('start');
foreach ($this->cfg['servers'] as $srv)
{
list($host, $port) = explode(':', $srv);
$this->mc->addServer($host, $port, $this->cfg['pconnect']);
}
$this->mc->setCompressThreshold(5000);
$this->debug('stop');
if (!$this->connected) $this->connect();
return ($this->connected) ? $this->memcache->delete($name) : false;
}
function get ($key)
function is_installed ()
{
$this->cur_query = ($this->dbg_enabled) ? "get(". join(", ", (array)$key) .")" : '';
$this->debug('start');
$result = $this->mc->get($key);
if (!is_array($result))
{
// îáðàáîòêè îøèáîê íåò
$result = array();
}
$this->debug('stop');
return $result;
}
function set ($key, $value, $ttl = 604800, $flag = 0)
{
$this->cur_query = ($this->dbg_enabled) ? "set($key, ". str_compact(print_r($value, true)) .")" : '';
$this->debug('start');
$result = $this->mc->set($key, $value, $flag, $ttl);
$this->debug('stop');
return $result;
}
function rm ($key)
{
$this->cur_query = ($this->dbg_enabled) ? "rm('$key')" : '';
$this->debug('start');
$result = $this->mc->delete($key, 0);
$this->debug('stop');
return $result;
}
}
class memcached_common extends cache_dbg_common
{
var $cfg = array(
'mc_name' => null, // $bb_cfg['cache']['memcache'][ key ]
'servers' => array('host:port'), // $bb_cfg['cache']['memcache'][ val ]
'pconnect' => false, // $bb_cfg['cache']['pconnect']
'con_required' => false,
'log_name' => 'memcached',
);
var $engine = 'Memcached';
var $mc = null;
function memcached_common ($cfg = array())
{
$this->cfg = array_merge($this->cfg, $cfg);
$this->dbg_enabled = sql_dbg_enabled();
$persistent_id = ($this->cfg['pconnect']) ? $this->cfg['mc_name'] : '';
if ($this->dbg_enabled)
{
$pconnect = ($this->cfg['pconnect']) ? 'pcon' : 'not_pcon';
$con_req = ($this->cfg['con_required']) ? 'req' : 'not_req';
$this->engine .= "<b class=normal>($persistent_id), $pconnect, $con_req</b>";
}
$this->mc = new Memcached($persistent_id);
$this->init();
}
function init ()
{
if (!count($this->mc->getServerList()))
{
$this->cur_query = ($this->dbg_enabled) ? "addServer(". join(", ", (array)$this->cfg['servers']) .")" : '';
$this->debug('start');
foreach ($this->cfg['servers'] as $srv)
{
list($host, $port) = explode(':', $srv);
$this->mc->addServer($host, $port);
}
$this->debug('stop');
}
}
function get ($key)
{
$this->cur_query = ($this->dbg_enabled) ? "get(". join(", ", (array)$key) .")" : '';
$this->debug('start');
$result = $this->mc->getMulti((array)$key);
if (!is_array($result))
{
$res_code = $this->mc->getResultCode();
$res_msg = $this->mc->getResultMessage();
$err_txt = "Memcached({$this->cfg['mc_name']})::get failed [$res_code, $res_msg]";
if ($this->cfg['con_required'])
{
trigger_error($err_txt, E_USER_ERROR);
}
else
{
bb_log(join(" ", array(date('d-m H:i:s'), $err_txt))."\n", 'mc_err');
}
$result = array();
}
$this->debug('stop');
return $result;
}
function set ($key, $value, $ttl = 604800, $flag = 0)
{
$this->cur_query = ($this->dbg_enabled) ? "set($key, ". str_compact(print_r($value, true)) .")" : '';
$this->debug('start');
$result = $this->mc->set($key, $value, $ttl);
$this->debug('stop');
return $result;
}
function rm ($key)
{
$this->cur_query = ($this->dbg_enabled) ? "rm('$key')" : '';
$this->debug('start');
$result = $this->mc->delete($key, 0);
$this->debug('stop');
return $result;
return class_exists('Memcache');
}
}

View file

@ -57,7 +57,7 @@ $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 r95';
$bb_cfg['tp_release_state'] = 'TP II r96';
$bb_cfg['tp_release_date'] = '11-07-2011';
$bb_cfg['board_disabled_msg'] = 'форум временно отключен'; // 'forums temporarily disabled'; // show this msg if board has been disabled via ON/OFF trigger
@ -85,24 +85,15 @@ define('DBMS', 'mysql');
$bb_cfg['cache']['pconnect'] = false;
$bb_cfg['cache']['db_dir'] = realpath(BB_ROOT) .'/cache/filecache/';
$bb_cfg['cache']['memcache'] = array(
// cache
'mc_bb_core' => array('localhost:11211', false, false),
// datastore
'ds_bb_core' => array('localhost:11211', false, false),
);
$bb_cfg['cache']['mc_class'] = 'memcache_common'; // memcache_common, memcached_common
// Available cache types: sqlite, db_sqlite, memcache, filecache
# name => array( (string) type, (array) cfg )
$bb_cfg['cache']['engines'] = array(
'bb_cache' => array('filecache', array()),
'tr_cache' => array('filecache', array()),
'session_cache' => array('filecache', array()),
'bb_cap_sid' => array('cache_sqlite', array()),
'bb_login_err' => array('cache_sqlite', array()),
'bb_cap_sid' => array('filecache', array()),
'bb_login_err' => array('filecache', array()),
);
// Datastore
@ -112,15 +103,12 @@ $bb_cfg['datastore']['sqlite'] = array(
'db_file_path' => $bb_cfg['cache']['db_dir'] . '/bb_datastore.sqlite.db',
'pconnect' => false,
);
$bb_cfg['datastore']['mc']['srv_all'] = array(
'ds_bb_core',
$bb_cfg['datastore']['memcache'] = array(
'host' => '127.0.0.1',
'port' => 11211,
'pconnect' => true, // use persistent connection
'con_required' => true, // exit script if can't connect
);
$bb_cfg['datastore']['mc']['srv_loc'] = 'undefined';
// создание конфига кешей
foreach ($bb_cfg['datastore']['mc']['srv_all'] as $ds_srv)
{
$bb_cfg['cache']['engines'][$ds_srv] = array('cache_memcache', array($ds_srv));
}
// Tracker
$bb_cfg['announce_type'] = 'php'; // Тип анонсера, xbt или php

View file

@ -275,33 +275,74 @@ class datastore_mysql extends datastore_common
class datastore_memcache extends datastore_common
{
var $engine = 'memcache';
var $cfg = array();
var $cfg = null;
var $memcache = null;
var $connected = false;
function datastore_memcache ($cfg)
{
global $bb_cfg;
if (!$this->is_installed())
{
die('Error: Memcached extension not installed');
}
$this->cfg = $cfg;
$this->memcache = new Memcache;
}
function store ($item_name, $item_data)
function connect ()
{
$this->data[$item_name] = $item_data;
# bb_log(join("\t", array(date('H:i:s'), $item_name, @basename($_SERVER['REQUEST_URI'])))."\n", 'ds_store');
$connect_type = ($this->cfg['pconnect']) ? 'pconnect' : 'connect';
foreach ($this->cfg['srv_all'] as $ds_srv)
if (@$this->memcache->$connect_type($this->cfg['host'], $this->cfg['port']))
{
CACHE($ds_srv)->set($item_name, $item_data, 604800, 'ds_');
$this->connected = true;
}
if (DBG_LOG) dbg_log(' ', 'CACHE-connect'. ($this->connected ? '' : '-FAIL'));
if (!$this->connected && $this->cfg['con_required'])
{
die('Could not connect to memcached server');
}
}
function store ($title, $var)
{
if (!$this->connected) $this->connect();
$this->data[$title] = $var;
return (bool) $this->memcache->set($title, $var);
}
function clean ()
{
if (!$this->connected) $this->connect();
foreach ($this->known_items as $title => $script_name)
{
$this->memcache->delete($title);
}
}
function _fetch_from_store ()
{
if (!$items = $this->queued_items) return;
foreach (CACHE($this->cfg['srv_loc'])->get($items, '', 'ds_') as $item_name => $item_val)
if (!$items = $this->queued_items)
{
$this->data[$item_name] = $item_val;
$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] = $this->memcache->get($item);
}
}
function is_installed ()
{
return class_exists('Memcache');
}
}

View file

@ -558,7 +558,7 @@ switch ($bb_cfg['datastore_type'])
break;
case 'memcache':
$datastore = new datastore_memcache($bb_cfg['datastore']['mc']);
$datastore = new datastore_memcache($bb_cfg['datastore']['memcache']);
break;
case 'filecache':