mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-04-21 19:32:15 +09:00
#18885380 add Cache Handler
git-svn-id: http://xe-core.googlecode.com/svn/sandbox@7450 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
parent
8f5d4c70fe
commit
f07cc09e4f
3 changed files with 255 additions and 0 deletions
91
classes/cache/CacheMemcache.class.php
vendored
Normal file
91
classes/cache/CacheMemcache.class.php
vendored
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
<?php
|
||||
/**
|
||||
* @class CacheMemcache
|
||||
* @author sol (sol@nhn.com)
|
||||
* @brief Memcache Handler
|
||||
* @version 0.1
|
||||
*
|
||||
**/
|
||||
|
||||
class CacheMemcache extends CacheBase {
|
||||
|
||||
var $valid_time = 36000;
|
||||
var $Memcache;
|
||||
|
||||
function getInstance($url){
|
||||
if(!$GLOBALS['__CacheMemcache__']) {
|
||||
$GLOBALS['__CacheMemcache__'] = new CacheMemcache($url);
|
||||
}
|
||||
return $GLOBALS['__CacheMemcache__'];
|
||||
}
|
||||
|
||||
function CacheMemcache($url){
|
||||
//$config['url'] = array('memcache://localhost:11211');
|
||||
$config['url'] = is_array($url)?$url:array($url);
|
||||
$this->Memcache = new Memcache;
|
||||
|
||||
foreach($config['url'] as $url) {
|
||||
$info = parse_url($url);
|
||||
$this->Memcache->addServer($info['host'], $info['port']);
|
||||
}
|
||||
}
|
||||
|
||||
function isSupport(){
|
||||
return $this->Memcache->set('xe', 'xe', MEMCACHE_COMPRESSED, 1);
|
||||
}
|
||||
|
||||
function getKey($key){
|
||||
return md5(_XE_PATH_.$key);
|
||||
}
|
||||
|
||||
function put($key, $buff, $valid_time = 0){
|
||||
if($valid_time == 0) $valid_time = $this->valid_time;
|
||||
|
||||
return $this->Memcache->set($this->getKey($key), array(time(), $buff), MEMCACHE_COMPRESSED, $valid_time);
|
||||
}
|
||||
|
||||
function isValid($key, $modified_time = 0) {
|
||||
$_key = $this->getKey($key);
|
||||
|
||||
$obj = $this->Memcache->get($_key);
|
||||
if(!$obj || !is_array($obj)) return false;
|
||||
unset($obj[1]);
|
||||
|
||||
if($modified_time > 0 && $modified_time > $obj[0]) {
|
||||
$this->_delete($_key);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function get($key, $modified_time = 0) {
|
||||
$_key = $this->getKey($key);
|
||||
$obj = $this->Memcache->get($_key);
|
||||
if(!$obj || !is_array($obj)) return false;
|
||||
|
||||
if($modified_time > 0 && $modified_time > $obj[0]) {
|
||||
$this->_delete($_key);
|
||||
return false;
|
||||
}
|
||||
|
||||
unset($obj[0]);
|
||||
|
||||
return $obj[1];
|
||||
}
|
||||
|
||||
function delete($key) {
|
||||
$_key = $this->getKey($key);
|
||||
$this->_delete($_key);
|
||||
}
|
||||
|
||||
function _delete($_key) {
|
||||
$this->Memcache->delete($_key);
|
||||
}
|
||||
|
||||
function truncate() {
|
||||
// not support memcached
|
||||
return false;
|
||||
}
|
||||
}
|
||||
?>
|
||||
Loading…
Add table
Add a link
Reference in a new issue