当前位置:Gxlcms > PHP教程 > php打包memcache类

php打包memcache类

时间:2021-07-01 10:21:17 帮助过:19人阅读

php 封装memcache类
/*
* memcache类
*/
class Memcacheds{
//声明静态成员变量
private static $m = null;
private static $cache = null;

private function __construct() {
self::$m = new Memcache();
self::$m->connect('www.cat.com','11211'); //写入缓存地址,端口
}

//为当前类创建对象
private static function Men(){
self::$cache = new Memcacheds();
return self::$m;
}

/*
* 添加缓存数据
* @param string $key 获取数据唯一key
* @param String||Array $value 缓存数据
* @param $time memcache生存周期(秒)
*/
public static function setMen($key,$value,$time){
self::Men()->set($key,$value,0,$time);
}
/*
* 获取缓存数据
* @param string $key
* @return
*/
public static function getMen($key){
return self::Men()->get($key);
}
/*
* 删除对应缓存数据
* @param string $key
* @return
*/
public static function delMen($key){
self::Men()->delete($key);
}
/*
* 删除所有缓存数据
*/
public static function delAllMen(){
self::Men()->flush();
}

/*
* 删除所有缓存数据
*/
public static function menStatus(){
return self::Men()->getStats();
}

}


<----------------------------------------------------------->

调用类

include_once 'memcache.php'; 引入类文件

$key = 'myKey';
$value = 'insert into 12334234';
$time = 60;
Memcacheds::setMen($key,$value,$time); //写入缓存

$get = Memcacheds::getMen($key); //读取

echo '

';
print_r($get);

?>

结果

输出:

insert into 12334234


版权声明:本文为博主原创文章,未经博主允许不得转载。

人气教程排行