Source for file Memcache.php
Documentation is available at Memcache.php
// vim: foldmethod=marker tabstop=4 shiftwidth=4 autoindent
* - キャッシュキーには250文字までしか使用できないので注意して下さい
* @todo ネームスペース/キャッシュキー長のエラーハンドリング
* @author Masaki Fujimoto <fujimoto@php.net>
* @version $Id: 163b3ffd272f571d005b78745eadd0049f890cc0 $
* キャッシュマネージャクラス(memcache版)
* @author Masaki Fujimoto <fujimoto@php.net>
/**#@+ @access private */
/** @var object Memcache Memcacheオブジェクト */
private $memcache =
null;
/** @var array plugin configure */
protected function _load()
$this->memcache_pool =
array();
* memcacheキャッシュオブジェクトを生成、取得する
protected function _getMemcache($cache_key, $namespace =
null)
$retry =
$this->config['retry'];
$timeout =
$this->config['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['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"];
* @return array array(host, port)
* @todo $cache_keyから$indexを決める方法を変更できるようにする
$memcache_info =
$this->config['info'];
$default_memcache_host =
$this->config['host'];
$default_memcache_port =
$this->config['port'];
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]['host']) ?
$memcache_info[$namespace][$index]['host'] :
isset
($memcache_info[$namespace][$index]['port']) ?
$memcache_info[$namespace][$index]['port'] :
return array($default_memcache_host, $default_memcache_port);
* キャッシュに値が設定されている場合はキャッシュ値
* が戻り値となる。キャッシュに値が無い場合やlifetime
* を過ぎている場合、エラーが発生した場合はEthna_Error
* @param string $key キャッシュキー
* @param int $lifetime キャッシュ有効期間
* @param string $namespace キャッシュネームスペース
public function get($key, $lifetime =
null, $namespace =
null)
if ($this->memcache ==
null) {
$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) {
$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 キャッシュネームスペース
public 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 キャッシュネームスペース
public function set($key, $value, $timestamp =
null, $namespace =
null)
if ($this->memcache ==
null) {
$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 キャッシュネームスペース
public function clear($key, $namespace =
null)
if ($this->memcache ==
null) {
$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:失敗
public function lock($key, $timeout =
5, $namespace =
null)
if ($this->memcache ==
null) {
return Ethna::raiseError('memcache server not available', E_CACHE_LOCK_ERROR);
$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:失敗
public function unlock($key, $namespace =
null)
if ($this->memcache ==
null) {
return Ethna::raiseError('memcache server not available', E_CACHE_LOCK_ERROR);
$cache_key =
"lock::" .
$this->_getCacheKey($namespace, $key);
$this->memcache->delete($cache_key, -
1);
private function _getCacheKey($namespace, $key)
$cache_key =
$namespace .
"::" .
$key;
if (strlen($cache_key) >
250) {
Documentation generated on Fri, 11 Nov 2011 03:58:40 +0900 by phpDocumentor 1.4.3