memcache緩存類
<?php
class memcached{
private $local_cache = array();
private $m;
private $client_type;
protected $errors = array();
public function __construct()
{
if (OPEN_CACHE == false){
return false;
}
$this->client_type = class_exists('Memcache') ? "Memcache" : (class_exists('Memcached') ? "Memcached" : FALSE);
if($this->client_type)
{
// 判斷引入類型
switch($this->client_type)
{
case 'Memcached':
$this->m = new Memcached;
break;
case 'Memcache':
$this->m = new Memcache();
// if (auto_compress_tresh){
// $this->setcompressthreshold(auto_compress_tresh, auto_compress_savings);
// }
break;
}
$this->auto_connect();
}
else
{
//echo 'ERROR: Failed to load Memcached or Memcache Class (∩_∩)';
//exit;
return false;
}
}
/**
* @Name: auto_connect
* @param:none
* @todu 連接memcache server
* @return : none
* add by cheng.yafei
**/
private function auto_connect()
{
$configServer = array(
'host' => MEMCACHE_HOST,
'port' => MEMCACHE_PORT,
'weight' => 1,
);
if(!$this->add_server($configServer)){
echo 'ERROR: Could not connect to the server named '.MEMCACHE_HOST;
}else{
//echo 'SUCCESS:Successfully connect to the server named '.MEMCACHE_HOST;
}
}
/**
* @Name: add_server
* @param:none
* @todu 連接memcache server
* @return : TRUE or FALSE
* add by cheng.yafei
**/
public function add_server($server){
extract($server);
return $this->m->addServer($host, $port, $weight);
}
/**
* @Name: add_server
* @todu 添加
* @param:$key key
* @param:$value 值
* @param:$expiration 過期時間
* @return : TRUE or FALSE
* add by cheng.yafei
**/
public function add($key = NULL, $value = NULL, $expiration = 0)
{
if (OPEN_CACHE == false){
return null;
}
if(is_null($expiration)){
$expiration = MEMCACHE_EXPIRATION;
}
if(is_array($key))
{
foreach($key as $multi){
if(!isset($multi['expiration']) || $multi['expiration'] == ''){
$multi['expiration'] = MEMCACHE_EXPIRATION;
}
$this->add($this->key_name($multi['key']), $multi['value'], $multi['expiration']);
}
}else{
$this->local_cache[$this->key_name($key)] = $value;
switch($this->client_type){
case 'Memcache':
$add_status = $this->m->add($this->key_name($key), $value, MEMCACHE_COMPRESSION, $expiration);
break;
default:
case 'Memcached':
$add_status = $this->m->add($this->key_name($key), $value, $expiration);
break;
}
return $add_status;
}
}
/**
* @Name 與add類似,但服務器有此鍵值時仍可寫入替換
* @param $key key
* @param $value 值
* @param $expiration 過期時間
* @return TRUE or FALSE
* add by cheng.yafei
**/
public function set($key = NULL, $value = NULL, $expiration = NULL)
{
if(is_null($expiration)){
$expiration = MEMCACHE_EXPIRATION;
}
if(is_array($key))
{
foreach($key as $multi){
if(!isset($multi['expiration']) || $multi['expiration'] == ''){
$multi['expiration'] = $this->config['config']['expiration'];
}
$this->set($this->key_name($multi['key']), $multi['value'], $multi['expiration']);
}
}else{
$this->local_cache[$this->key_name($key)] = $value;
switch($this->client_type){
case 'Memcache':
$add_status = $this->m->set($this->key_name($key), $value, MEMCACHE_COMPRESSION, $expiration);
break;
case 'Memcached':
$add_status = $this->m->set($this->key_name($key), $value, $expiration);
break;
}
return $add_status;
}
}
/**
* @Name get 根據鍵名獲取值
* @param $key key
* @return array OR json object OR string...
* add by cheng.yafei
**/
public function get($key = NULL)
{
if($this->m)
{
if(isset($this->local_cache[$this->key_name($key)]))
{
return $this->local_cache[$this->key_name($key)];
}
if(is_null($key)){
$this->errors[] = 'The key value cannot be NULL';
return FALSE;
}
if(is_array($key)){
foreach($key as $n=>$k){
$key[$n] = $this->key_name($k);
}
return $this->m->getMulti($key);
}else{
return $this->m->get($this->key_name($key));
}
}else{
return FALSE;
}
}
/**
* @Name rm
* @param $key key
* @param $expiration 服務端等待刪除該元素的總時間
* @return true OR false
* add by cheng.yafei
**/
public function rm($key, $expiration = NULL)
{
if(is_null($key))
{
$this->errors[] = 'The key value cannot be NULL';
return FALSE;
}
if(is_null($expiration))
{
$expiration = MEMCACHE_EXPIRATION;
}
if(is_array($key))
{
foreach($key as $multi)
{
$this->delete($multi, $expiration);
}
}
else
{
unset($this->local_cache[$this->key_name($key)]);
return $this->m->delete($this->key_name($key), $expiration);
}
}
/**
* @Name replace
* @param $key 要替換的key
* @param $value 要替換的value
* @param $expiration 到期時間
* @return none
* add by cheng.yafei
**/
public function replace($key = NULL, $value = NULL, $expiration = NULL)
{
if(is_null($expiration)){
$expiration = MEMCACHE_EXPIRATION;
}
if(is_array($key)){
foreach($key as $multi) {
if(!isset($multi['expiration']) || $multi['expiration'] == ''){
$multi['expiration'] = $this->config['config']['expiration'];
}
$this->replace($multi['key'], $multi['value'], $multi['expiration']);
}
}else{
$this->local_cache[$this->key_name($key)] = $value;
switch($this->client_type){
case 'Memcache':
$replace_status = $this->m->replace($this->key_name($key), $value, MEMCACHE_COMPRESSION, $expiration);
break;
case 'Memcached':
$replace_status = $this->m->replace($this->key_name($key), $value, $expiration);
break;
}
return $replace_status;
}
}
/**
* @Name replace 清空所有緩存
* @return none
* add by cheng.yafei
**/
public function flush()
{
return $this->m->flush();
}
/**
* @Name 獲取服務器池中所有服務器的版本信息
**/
public function getversion()
{
return $this->m->getVersion();
}
/**
* @Name 獲取服務器池的統計信息
**/
public function getstats($type="items")
{
switch($this->client_type)
{
case 'Memcache':
$stats = $this->m->getStats($type);
break;
default:
case 'Memcached':
$stats = $this->m->getStats();
break;
}
return $stats;
}
/**
* @Name: 開啟大值自動壓縮
* @param:$tresh 控制多大值進行自動壓縮的閾值。
* @param:$savings 指定經過壓縮實際存儲的值的壓縮率,值必須在0和1之間。默認值0.2表示20%壓縮率。
* @return : true OR false
* add by cheng.yafei
**/
public function setcompressthreshold($tresh, $savings=0.2)
{
switch($this->client_type)
{
case 'Memcache':
$setcompressthreshold_status = $this->m->setCompressThreshold($tresh, $savings=0.2);
break;
default:
$setcompressthreshold_status = TRUE;
break;
}
return $setcompressthreshold_status;
}
/**
* @Name: 生成md5加密后的唯一鍵值
* @param:$key key
* @return : md5 string
* add by cheng.yafei
**/
private function key_name($key)
{
return md5(strtolower(MEMCACHE_PREFIX.$key));
}
/**
* @Name: 向已存在元素后追加數據
* @param:$key key
* @param:$value value
* @return : true OR false
* add by cheng.yafei
**/
public function append($key = NULL, $value = NULL)
{
// if(is_array($key))
// {
// foreach($key as $multi)
// {
//
// $this->append($multi['key'], $multi['value']);
// }
// }
// else
// {
$this->local_cache[$this->key_name($key)] = $value;
switch($this->client_type)
{
case 'Memcache':
$append_status = $this->m->append($this->key_name($key), $value);
break;
default:
case 'Memcached':
$append_status = $this->m->append($this->key_name($key), $value);
break;
}
return $append_status;
// }
}//END append
}// END class
?>
- Memcache 操作類
- 在config文件中添加相應配置(可擴展為多memcache server)
$cacheObj = new memcached();
$cacheObj -> set('keyName','this is value');
$cacheObj -> get('keyName');
exit;
請關注我們微信公眾號:mw748219