Source for file Ethna_Plugin_Cachemanager_Memcache.php
Documentation is available at Ethna_Plugin_Cachemanager_Memcache.php
// vim: foldmethod=marker tabstop=4 shiftwidth=4 autoindent
* Ethna_Plugin_Cachemanager_Memcache.php
* - キャッシュキーには250文字までしか使用できないので注意して下さい
* @todo ネームスペース/キャッシュキー長のエラーハンドリング
* @author Masaki Fujimoto <fujimoto@php.net>
* キャッシュマネージャクラス(memcache版)
* @author Masaki Fujimoto <fujimoto@php.net>
/**#@+ @access private */
/** @var object MemCache MemCacheオブジェクト */
* Ethna_Plugin_Cachemanager_Memcacheクラスのコンストラクタ
$this->memcache_pool =
array();
* memcacheキャッシュオブジェクトを生成、取得する
$retry =
$this->config->get('memcache_retry');
$timeout =
$this->config->get('memcache_timeout');
if (isset
($this->memcache_pool["$host:$port"])) {
$this->memcache =
$this->memcache_pool["$host:$port"];
$this->memcache_pool["$host:$port"] =
& new MemCache();
if ($this->config->get('memcache_use_pconnect')) {
$r =
$this->memcache_pool["$host:$port"]->pconnect($host, $port, $timeout);
$r =
$this->memcache_pool["$host:$port"]->connect($host, $port, $timeout);
$this->memcache_pool["$host:$port"] =
null;
$this->memcache =
$this->memcache_pool["$host:$port"];
* @todo $cache_keyから$indexを決める方法を変更できるようにする
$namespace =
is_null($namespace) ?
$this->namespace :
$namespace;
$memcache_info =
$this->config->get('memcache');
$default_memcache_host =
$this->config->get('memcache_host');
if ($default_memcache_host ==
"") {
$default_memcache_host =
"localhost";
$default_memcache_port =
$this->config->get('memcache_port');
if ($default_memcache_port ==
"") {
$default_memcache_port =
11211;
if ($memcache_info ==
null || isset
($memcache_info[$namespace]) ==
false) {
return array($default_memcache_host, $default_memcache_port);
// namespace/cache_keyで接続先を決定
$n =
count($memcache_info[$namespace]);
$index =
$cache_key %
$n;
isset
($memcache_info[$namespace][$index]['memcache_host']) ?
$memcache_info[$namespace][$index]['memcache_host'] :
isset
($memcache_info[$namespace][$index]['memcache_port']) ?
$memcache_info[$namespace][$index]['memcache_port'] :
return array($default_memcache_host, $default_memcache_port);
* キャッシュに値が設定されている場合はキャッシュ値
* が戻り値となる。キャッシュに値が無い場合やlifetime
* を過ぎている場合、エラーが発生した場合はEthna_Error
* @param string $key キャッシュキー
* @param int $lifetime キャッシュ有効期間
* @param string $namespace キャッシュネームスペース
function get($key, $lifetime =
null, $namespace =
null)
if ($this->memcache ==
null) {
$namespace =
is_null($namespace) ?
$this->namespace :
$namespace;
$cache_key =
$this->_getCacheKey($namespace, $key);
if ($cache_key ==
null) {
$value =
$this->memcache->get($cache_key);
if (($time+
$lifetime) <
time()) {
* @param string $key キャッシュキー
* @param string $namespace キャッシュネームスペース
* @return int 最終更新日時(unixtime)
if ($this->memcache ==
null) {
$namespace =
is_null($namespace) ?
$this->namespace :
$namespace;
$cache_key =
$this->_getCacheKey($namespace, $key);
if ($cache_key ==
null) {
$value =
$this->memcache->get($cache_key);
* @param string $key キャッシュキー
* @param int $lifetime キャッシュ有効期間
* @param string $namespace キャッシュネームスペース
function isCached($key, $lifetime =
null, $namespace =
null)
$r =
$this->get($key, $lifetime, $namespace);
* @param string $key キャッシュキー
* @param mixed $value キャッシュ値
* @param int $timestamp キャッシュ最終更新時刻(unixtime)
* @param string $namespace キャッシュネームスペース
function set($key, $value, $timestamp =
null, $namespace =
null)
if ($this->memcache ==
null) {
$namespace =
is_null($namespace) ?
$this->namespace :
$namespace;
$cache_key =
$this->_getCacheKey($namespace, $key);
if ($cache_key ==
null) {
$time =
$timestamp ?
$timestamp :
time();
$this->memcache->set($cache_key, array('time' =>
$time, 'data' =>
$value), $this->compress ?
MEMCACHE_COMPRESSED :
null);
* @param string $key キャッシュキー
* @param string $namespace キャッシュネームスペース
function clear($key, $namespace =
null)
if ($this->memcache ==
null) {
$namespace =
is_null($namespace) ?
$this->namespace :
$namespace;
$cache_key =
$this->_getCacheKey($namespace, $key);
if ($cache_key ==
null) {
$this->memcache->delete($cache_key, -
1);
* @param string $key キャッシュキー
* @param int $timeout ロックタイムアウト
* @param string $namespace キャッシュネームスペース
* @return bool true:成功 false:失敗
function lock($key, $timeout =
5, $namespace =
null)
if ($this->memcache ==
null) {
return Ethna::raiseError('memcache server not available', E_CACHE_LOCK_ERROR);
$namespace =
is_null($namespace) ?
$this->namespace :
$namespace;
$cache_key =
"lock::" .
$this->_getCacheKey($namespace, $key);
$r =
$this->memcache->add($cache_key, true, false, $lock_lifetime);
* @param string $key キャッシュキー
* @param string $namespace キャッシュネームスペース
* @return bool true:成功 false:失敗
function unlock($key, $namespace =
null)
if ($this->memcache ==
null) {
return Ethna::raiseError('memcache server not available', E_CACHE_LOCK_ERROR);
$namespace =
is_null($namespace) ?
$this->namespace :
$namespace;
$cache_key =
"lock::" .
$this->_getCacheKey($namespace, $key);
$this->memcache->delete($cache_key, -
1);
function _getCacheKey($namespace, $key)
$cache_key =
$namespace .
"::" .
$key;
if (strlen($cache_key) >
250) {
Documentation generated on Fri, 11 Nov 2011 04:00:08 +0900 by phpDocumentor 1.4.3