phpcms 添加memcache支持

1,修改caches configs cache php

 

1,修改caches/configs/cache.php

<?php

return array (
‘file1‘ => array (
‘type‘ => ‘file‘,
‘debug‘ => true,
‘pconnect‘ => 0,
‘autoconnect‘ => 0
),
‘memcache1‘ => array (
‘hostname‘ => ‘127.0.0.1‘,
‘port‘ => 11211,
‘timeout‘ => 0,
‘type‘ => ‘memcache‘,
‘debug‘ => true,
‘pconnect‘ => 0,
‘autoconnect‘ => 0
)
);

?>

 

 

2,修改phpcms/libs/functions/globl.func.php  文件的四个函数

 

function setcache($name, $data, $filepath=‘‘, $type=‘memcache‘, $config=‘memcache1‘, $timeout=0) {
pc_base::load_sys_class(‘cache_factory‘,‘‘,0);
if($config) {
$cacheconfig = pc_base::load_config(‘cache‘);
$cache = cache_factory::get_instance($cacheconfig)->get_cache($config);
} else {
$cache = cache_factory::get_instance()->get_cache($type);
}

return $cache->set($name, $data, $timeout, ‘‘, $filepath);
}


function getcache($name, $filepath=‘‘, $type=‘memcache‘, $config=‘memcache1‘) {
pc_base::load_sys_class(‘cache_factory‘,‘‘,0);
if($config) {
$cacheconfig = pc_base::load_config(‘cache‘);
$cache = cache_factory::get_instance($cacheconfig)->get_cache($config);
} else {
$cache = cache_factory::get_instance()->get_cache($type);
}
return $cache->get($name, ‘‘, ‘‘, $filepath);
}


function delcache($name, $filepath=‘‘, $type=‘memcache‘, $config=‘memcache1‘) {
pc_base::load_sys_class(‘cache_factory‘,‘‘,0);
if($config) {
$cacheconfig = pc_base::load_config(‘cache‘);
$cache = cache_factory::get_instance($cacheconfig)->get_cache($config);
} else {
$cache = cache_factory::get_instance()->get_cache($type);
}
return $cache->delete($name, ‘‘, ‘‘, $filepath);
}


function getcacheinfo($name, $filepath=‘‘, $type=‘memcache‘, $config=‘memcache1‘) {
pc_base::load_sys_class(‘cache_factory‘);
if($config) {
$cacheconfig = pc_base::load_config(‘cache‘);
$cache = cache_factory::get_instance($cacheconfig)->get_cache($config);
} else {
$cache = cache_factory::get_instance()->get_cache($type);
}
return $cache->cacheinfo($name, ‘‘, ‘‘, $filepath);
}

 

3,修改phpcms/libs/classes/cache_memcache.class.php

<?php 
class cache_memcache {

private $memcache = null;

public function __construct() {
$this->memcache = new Memcache;
$this->memcache->connect(MEMCACHE_HOST, MEMCACHE_PORT)or die ("Could not connect");
}

public function memcache() {
$this->__construct();
}

public function get($name, $empty=‘‘, $empty=‘‘,$filepath) {
$value = $this->memcache->get($name.$filepath);
return $value;
}

public function cacheinfo($name, $empty=‘‘, $empty=‘‘, $filepath){
$value = $this->memcache->get($name.$filepath);
return $value;
}

public function set($name, $value, $ttl = 3600, $ext1=‘‘, $ext2=‘‘) {
return $this->memcache->set($name.$ext2, $value, false, $ttl);
}

public function delete($name, $empty=‘‘, $empty=‘‘, $filepath) {
return $this->memcache->delete($name.$filepath);
}

public function flush() {
return $this->memcache->flush();
}
}
?>

 

 

 

4,测试调用文件(保持到网站根目录查看)

<?php
header(‘Content-type: text/html; charset=utf8‘);
echo "<pre>";
$host=‘127.0.0.1‘;
$port=11211;
$mem=new Memcache();
$mem->connect($host,$port);

 

$mem->set(‘var_key‘, ‘some variable‘, 0, 30);//设置

$var = $mem->get(‘var_key‘);//获取

echo $var;

$mem->add(‘xxx‘,‘fffffffff‘,0,80);//添加
$mem->delete(‘xxx‘, 10);//删除
$var = $mem->get(‘xxx‘);
echo $var;

 


$items=$mem->getExtendedStats (‘items‘);
$items=$items["$host:$port"][‘items‘];
foreach($items as $key=>$values){
$number=$key;;
$str=$mem->getExtendedStats ("cachedump",$number,0);
$line=$str["$host:$port"];
if( is_array($line) && count($line)>0){
foreach($line as $key=>$value){
echo $key.‘=>‘;
print_r($mem->get($key));
echo "\r\n"."<br>";
}
}
}
?>

ok!