mirror of
https://github.com/torrentpier/torrentpier
synced 2025-08-22 06:13:58 -07:00
r96
memcache git-svn-id: https://torrentpier2.googlecode.com/svn/trunk@96 a8ac35ab-4ca4-ca47-4c2d-a49a94f06293
This commit is contained in:
parent
455fef9f1a
commit
62a5fd246f
4 changed files with 105 additions and 269 deletions
|
@ -141,33 +141,21 @@ class CACHES
|
||||||
|
|
||||||
switch ($cache_type)
|
switch ($cache_type)
|
||||||
{
|
{
|
||||||
case 'cache_memcache':
|
case 'memcache':
|
||||||
if (!$mc_name =& $cache_cfg[0])
|
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])
|
$this->ref[$cache_name] =& $this->obj[$cache_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];
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'cache_sqlite':
|
case 'sqlite':
|
||||||
if (!isset($this->obj[$cache_name]))
|
if (!isset($this->obj[$cache_name]))
|
||||||
{
|
{
|
||||||
$cache_cfg['pconnect'] = $this->cfg['pconnect'];
|
$cache_cfg['pconnect'] = $this->cfg['pconnect'];
|
||||||
|
@ -263,244 +251,63 @@ class cache_common
|
||||||
|
|
||||||
class cache_memcache extends cache_common
|
class cache_memcache extends cache_common
|
||||||
{
|
{
|
||||||
var $cfg = array(
|
var $used = true;
|
||||||
'mc_class' => 'class_name', // $bb_cfg['cache']['mc_class']
|
|
||||||
);
|
var $cfg = null;
|
||||||
var $used = true;
|
var $memcache = null;
|
||||||
var $db = null;
|
var $connected = false;
|
||||||
|
|
||||||
function cache_memcache ($cfg)
|
function cache_memcache ($cfg)
|
||||||
{
|
{
|
||||||
$this->cfg = array_merge($this->cfg, $cfg);
|
global $bb_cfg;
|
||||||
$this->db = new $this->cfg['mc_class']($this->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))
|
$connect_type = ($this->cfg['pconnect']) ? 'pconnect' : 'connect';
|
||||||
{
|
|
||||||
return is_array($key) ? array() : false;
|
|
||||||
}
|
|
||||||
$cached_items = array();
|
|
||||||
$prefix_len = strlen($prefix);
|
|
||||||
$key_ary = (array) $key;
|
|
||||||
$key_get = array();
|
|
||||||
|
|
||||||
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
|
if (DBG_LOG) dbg_log(' ', 'CACHE-connect'. ($this->connected ? '' : '-FAIL'));
|
||||||
foreach ($this->db->get($key_get) as $k => $v)
|
|
||||||
{
|
|
||||||
$cached_items[substr($k, $prefix_len)] = $v;
|
|
||||||
}
|
|
||||||
|
|
||||||
// get miss items
|
if (!$this->connected && $this->cfg['con_required'])
|
||||||
if ($get_miss_key_callback AND $miss_key = array_diff($key_ary, array_keys($cached_items)))
|
|
||||||
{
|
{
|
||||||
foreach ($get_miss_key_callback($miss_key) as $k => $v)
|
die('Could not connect to memcached server');
|
||||||
{
|
|
||||||
$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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
if (!$this->connected) $this->connect();
|
||||||
}
|
return ($this->connected) ? $this->memcache->set($name, $value, false, $ttl) : false;
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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'] .")" : '';
|
if (!$this->connected) $this->connect();
|
||||||
$this->debug('start');
|
return ($this->connected) ? $this->memcache->delete($name) : false;
|
||||||
|
|
||||||
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');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function get ($key)
|
function is_installed ()
|
||||||
{
|
{
|
||||||
$this->cur_query = ($this->dbg_enabled) ? "get(". join(", ", (array)$key) .")" : '';
|
return class_exists('Memcache');
|
||||||
$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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -57,7 +57,7 @@ $bb_cfg['css_ver'] = 1;
|
||||||
|
|
||||||
// Increase number of revision after update
|
// Increase number of revision after update
|
||||||
$bb_cfg['tp_version'] = '2.0.2';
|
$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['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
|
$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']['pconnect'] = false;
|
||||||
$bb_cfg['cache']['db_dir'] = realpath(BB_ROOT) .'/cache/filecache/';
|
$bb_cfg['cache']['db_dir'] = realpath(BB_ROOT) .'/cache/filecache/';
|
||||||
|
|
||||||
$bb_cfg['cache']['memcache'] = array(
|
// Available cache types: sqlite, db_sqlite, memcache, filecache
|
||||||
// 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
|
|
||||||
|
|
||||||
# name => array( (string) type, (array) cfg )
|
# name => array( (string) type, (array) cfg )
|
||||||
|
|
||||||
$bb_cfg['cache']['engines'] = array(
|
$bb_cfg['cache']['engines'] = array(
|
||||||
'bb_cache' => array('filecache', array()),
|
'bb_cache' => array('filecache', array()),
|
||||||
'tr_cache' => array('filecache', array()),
|
'tr_cache' => array('filecache', array()),
|
||||||
'session_cache' => array('filecache', array()),
|
'session_cache' => array('filecache', array()),
|
||||||
|
|
||||||
'bb_cap_sid' => array('cache_sqlite', array()),
|
'bb_cap_sid' => array('filecache', array()),
|
||||||
'bb_login_err' => array('cache_sqlite', array()),
|
'bb_login_err' => array('filecache', array()),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Datastore
|
// Datastore
|
||||||
|
@ -112,15 +103,12 @@ $bb_cfg['datastore']['sqlite'] = array(
|
||||||
'db_file_path' => $bb_cfg['cache']['db_dir'] . '/bb_datastore.sqlite.db',
|
'db_file_path' => $bb_cfg['cache']['db_dir'] . '/bb_datastore.sqlite.db',
|
||||||
'pconnect' => false,
|
'pconnect' => false,
|
||||||
);
|
);
|
||||||
$bb_cfg['datastore']['mc']['srv_all'] = array(
|
$bb_cfg['datastore']['memcache'] = array(
|
||||||
'ds_bb_core',
|
'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
|
// Tracker
|
||||||
$bb_cfg['announce_type'] = 'php'; // Тип анонсера, xbt или php
|
$bb_cfg['announce_type'] = 'php'; // Тип анонсера, xbt или php
|
||||||
|
|
|
@ -275,33 +275,74 @@ class datastore_mysql extends datastore_common
|
||||||
|
|
||||||
class datastore_memcache extends datastore_common
|
class datastore_memcache extends datastore_common
|
||||||
{
|
{
|
||||||
var $engine = 'memcache';
|
var $cfg = null;
|
||||||
var $cfg = array();
|
var $memcache = null;
|
||||||
|
var $connected = false;
|
||||||
|
|
||||||
function datastore_memcache ($cfg)
|
function datastore_memcache ($cfg)
|
||||||
{
|
{
|
||||||
|
global $bb_cfg;
|
||||||
|
|
||||||
|
if (!$this->is_installed())
|
||||||
|
{
|
||||||
|
die('Error: Memcached extension not installed');
|
||||||
|
}
|
||||||
|
|
||||||
$this->cfg = $cfg;
|
$this->cfg = $cfg;
|
||||||
|
$this->memcache = new Memcache;
|
||||||
}
|
}
|
||||||
|
|
||||||
function store ($item_name, $item_data)
|
function connect ()
|
||||||
{
|
{
|
||||||
$this->data[$item_name] = $item_data;
|
$connect_type = ($this->cfg['pconnect']) ? 'pconnect' : 'connect';
|
||||||
# bb_log(join("\t", array(date('H:i:s'), $item_name, @basename($_SERVER['REQUEST_URI'])))."\n", 'ds_store');
|
|
||||||
|
|
||||||
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 ()
|
function _fetch_from_store ()
|
||||||
{
|
{
|
||||||
if (!$items = $this->queued_items) return;
|
if (!$items = $this->queued_items)
|
||||||
|
|
||||||
foreach (CACHE($this->cfg['srv_loc'])->get($items, '', 'ds_') as $item_name => $item_val)
|
|
||||||
{
|
{
|
||||||
$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');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -558,7 +558,7 @@ switch ($bb_cfg['datastore_type'])
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'memcache':
|
case 'memcache':
|
||||||
$datastore = new datastore_memcache($bb_cfg['datastore']['mc']);
|
$datastore = new datastore_memcache($bb_cfg['datastore']['memcache']);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'filecache':
|
case 'filecache':
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue