在php memcache缓存里,我只找到 add,delete两种方法添加或删除缓存,没有分组功能像JAVA版 memecache有分组功能,如我用一个用户日记分组 当我更新了日记,可以删除此日记分组缓存,我的个人日记列表,我的WAP版日记列表也更新我想有如何功能add("cacheGroup","cacheName1",$value1);
add("cacheGroup","cacheName2",$value2);
add("cacheGroup","cacheName3",$value3);
deleteGroup("cacheGroup");
然后上面三个缓存都清除
php memecache缓存API如下
Memcache::add — 添加一个值,如果已经存在,则返回false
Memcache::addServer — 添加一个可供使用的服务器地址
Memcache::close — 关闭一个Memcache对象
Memcache::connect — 创建一个Memcache对象
memcache_debug — 控制调试功能
Memcache::decrement — 对保存的某个key中的值进行减法操作
Memcache::delete — 删除一个key值
Memcache::flush — 清除所有缓存的数据
Memcache::get — 获取一个key值
Memcache::getExtendedStats — 获取进程池中所有进程的运行系统统计
Memcache::getServerStatus — 获取运行服务器的参数
Memcache::getStats — 返回服务器的一些运行统计信息
Memcache::getVersion — 返回运行的Memcache的版本信息
Memcache::increment — 对保存的某个key中的值进行加法操作
Memcache::pconnect — 创建一个Memcache的持久连接对象
Memcache::replace — R对一个已有的key进行覆写操作
Memcache::set — 添加一个值,如果已经存在,则覆写
Memcache::setCompressThreshold — 对大于某一大小的数据进行压缩
Memcache::setServerParams — 在运行时修改服务器的参数
add("cacheGroup","cacheName2",$value2);
add("cacheGroup","cacheName3",$value3);
deleteGroup("cacheGroup");
然后上面三个缓存都清除
php memecache缓存API如下
Memcache::add — 添加一个值,如果已经存在,则返回false
Memcache::addServer — 添加一个可供使用的服务器地址
Memcache::close — 关闭一个Memcache对象
Memcache::connect — 创建一个Memcache对象
memcache_debug — 控制调试功能
Memcache::decrement — 对保存的某个key中的值进行减法操作
Memcache::delete — 删除一个key值
Memcache::flush — 清除所有缓存的数据
Memcache::get — 获取一个key值
Memcache::getExtendedStats — 获取进程池中所有进程的运行系统统计
Memcache::getServerStatus — 获取运行服务器的参数
Memcache::getStats — 返回服务器的一些运行统计信息
Memcache::getVersion — 返回运行的Memcache的版本信息
Memcache::increment — 对保存的某个key中的值进行加法操作
Memcache::pconnect — 创建一个Memcache的持久连接对象
Memcache::replace — R对一个已有的key进行覆写操作
Memcache::set — 添加一个值,如果已经存在,则覆写
Memcache::setCompressThreshold — 对大于某一大小的数据进行压缩
Memcache::setServerParams — 在运行时修改服务器的参数
解决方案 »
- 高手进来看看 我的Smarty模板安装不上啊
- 收藏 不显示删除回复显示所有回复显示星级回复显示得分回复 想组建大家一起做项目!开源类型。
- 请有集群,负载均衡,双机热备经验的人回答
- NewMagickWand图片压缩
- dedecms做的网站,现在被恶意刷流量,不知道有没有程序能够实现阻止恶意刷流量呢
- 用PHP在xampp+Dreamweaver+PHP_Maile环境下r实现发送邮件功能
- ThinkPHP上使用PHPExcel
- 基于 Laravel 5.2 开发的一个 bbs,参考 V2EX, PHPHub 和 Ruby China
- phpcms自定义模块如何简化url
- php怎么将MySQL里的blob格式的图片显示在html上?
- 织梦cms后台的内容修改页面可以增加自动分页吗?
- php程序中支付宝插件如何安装
add("cacheGroup","cacheName2",$value2);
add("cacheGroup","cacheName3",$value3);
deleteGroup("cacheGroup");你的这种方式,你可以扩充mem
class myMem extends memcache {
private $groupExpireDefault = 14400;
public function setGroup($group, $name, $value) {
parent::set($group.'_'.$name, $value);
}
public function delGroup($group) {
parent::set($group, '-1');
}
public function setGroupExpire($group, $expire=null) {
if(!$expire) $expire = time()+$this->groupExpireDefault;
parent::set($group, $expire);
}
public function getGroup($group, $name) {
// 如果已经过期
if(parent::get($group) < time()) {
return null;
}
return parent::get($group.'_'.$name);
}
}$mymem = new myMem();$mymem->setGroupExpire('group1', time()+14400);
$mymem->setGroup('group1','xxx','xxx');
$mymem->setGroup('group1','yyy','yyy');echo $mymem->getGroup('group1', 'xxx'); //xxx$mymem->delGroup('group1');echo $mymem->getGroup('group1', 'xxx'); // is empty
我测试了你的程序是可行的,但是有几点不解1、
你在 setGroupExpire 使用 parent::set($group, $expire); 这个不就是添加一个缓存么?他怎么成了设置缓存时间?2、
parent::set($group.'_'.$name, $value);parent::set($group, '-1');这两个key值完全不一样,程序又如何实现删除一个$group把$group.'_'.$name所有缓存都删除了我没有看memcache源码。
我在用 echo $mymem->get($group); 得到了$expire时间值
add("cacheGroup","cacheName2",$value2);
add("cacheGroup","cacheName3",$value3);
deleteGroup("cacheGroup");这样也就是一个key 控制一数组class mem
{
public $memcache=null;
public $array=array();
public function __construct()
{
$this->memcache = new Memcache;
$this->memcache->connect('localhost', 11211) or die ("Could not connect");
}
public function add($parent="",$key="",$value="")
{
$this->array[$key]=$value;
$this->memcache->set($parent,$this->array,false,500);
}
public function deleteGroup($key=null)
{
$this->memcache->delete($key);
}
public function get($key=null)
{
return $this->memcache->get($key);
}
}
$mem=new mem();
$mem->add("cacheGroup","cacheName1","1");
$mem->add("cacheGroup","cacheName2","2");
$mem->add("cacheGroup","cacheName3","3");
//$mem->deleteGroup("cacheGroup");
var_Dump($mem->get('cacheGroup'));
public function getGroup($group, $name) {
// 如果已经过期
if(parent::get($group) < time()) {
return null;
}
return parent::get($group.'_'.$name);
}上面的代码来判断是否过期,这样所有这个group里的数据,都有了统一的时间验证标准,只要parent::get($group) < time()成立,即便MC中有数据或者说数据没有过期,我们也认为过期了
其实这个数据并没有删除,还在MC中,但是在你重新存数据之前,已经是垃圾数据了!在group的缓存周期到期时,如果你不在存新的内容到相应的key(例如:$group.'_'.$name),那么在真正的内容过期(指定有默认过期时间)或者内存不够用(没有指定默认过期时间),MC就会通过GC(垃圾回收机制)自动回收了,因为这个数据经常不会用,也就是没有缓存命中,所以这方面你就不用担心了!
{
private $mmc = null;
private $group = null;
private $version = 1;
function __construct($group){
if(!class_exists('memcache')){
$this->mmc = false;
return;
}
$this->mmc = new memcache();
$ip = //ip
$port = //port
$this->mmc->addServer($ip, $port); //可添加多个服务器
$this->group = $group;
$this->version = $this->mmc->get('version_'.$group);
if(!$this->version) $this->version = 1;
}
function set($key, $var, $expire=3600){
if(!$this->mmc)return;
return $this->mmc->set($this->group.'_'.$this->version.'_'.$key, $var, $expire);
}
function get($key){
if(!$this->mmc)return;
return $this->mmc->get($this->group.'_'.$this->version.'_'.$key);
}
function incr($key, $value=1){
if(!$this->mmc)return;
return $this->mmc->increment($this->group.'_'.$this->version.'_'.$key, $value);
}
function decr($key, $value=1){
if(!$this->mmc)return;
return $this->mmc->decrement($this->group.'_'.$this->version.'_'.$key, $value);
}
function delete($key){
if(!$this->mmc)return;
return $this->mmc->delete($this->group.'_'.$this->version.'_'.$key);
}
function flush(){
if(!$this->mmc)return;
++$this->version;
$this->mmc->set('version_'.$this->group, $this->version);
}
}
{
private $mmc = null;
private $group = null;
private $version = 1;
function __construct($group){
if(!class_exists('memcache')){
$this->mmc = false;
return;
}
$this->mmc = new memcache();
$ip = K::$config->item('cache', 'memcache');
$port = K::$config->item('cache', 'memport');
$this->mmc->addServer($ip, $port); //可添加多个服务器
$this->group = $group;
$this->version = $this->mmc->get('version_'.$group);
if(!$this->version) $this->version = 1;
}
//function test(){
// $this->mmc->connect() or die ("Could not connect memcache");
//}
function set($key, $var, $expire=3600){
if(!$this->mmc)return;
return $this->mmc->set($this->group.'_'.$this->version.'_'.$key, $var, $expire);
}
function get($key){
if(!$this->mmc)return;
return $this->mmc->get($this->group.'_'.$this->version.'_'.$key);
}
function incr($key, $value=1){
if(!$this->mmc)return;
return $this->mmc->increment($this->group.'_'.$this->version.'_'.$key, $value);
}
function decr($key, $value=1){
if(!$this->mmc)return;
return $this->mmc->decrement($this->group.'_'.$this->version.'_'.$key, $value);
}
function delete($key){
if(!$this->mmc)return;
return $this->mmc->delete($this->group.'_'.$this->version.'_'.$key);
}
function flush(){
if(!$this->mmc)return;
++$this->version;
$this->mmc->set('version_'.$this->group, $this->version);
}
}