redis作为缓存服务器为MySQL数据库提供较高的防御性,对于一些数据的查询可以直接从缓存中可以进行查询。

但是,某些情况下,我们需要清除缓存。

以下场景:

公司经常做活动,每个活动都存在大量的数据。在新活动进行测试的时候,也会产生一些缓存,但是删除这些缓存如果不能批量删除就有点烦了。

在写活动的时候,为了保证活动的缓存不冲突,用自己姓名的前缀及活动的英文名作为前缀。缓存在很大程度上能够帮助我们降低服务器的访问压力,但是也要防止缓存失效的情况,缓存并不能作为我们的最终依靠。

首先在缓存中查询,如果缓存中不存在再去mysql数据库中查询,当数据库中真的不存在的时候,才能确定该查询的数据不存在;因此在数据库中查到数据的时候,再将该数据写入缓存。

------------------------------------------------------------------------------------------------------

如何删除指定前缀的redis。。。

我们一开始给redis封装了一个类库

<?php
class RedisClass
{
static $_instance; //存储对象
public $handler ;
private function __construct($dbindex = )
{
global $_G ;
$data = $_G['config']['redis']['redis']['params'];
if ( !extension_loaded('redis') ) {
throw new Exception("REDIS NOT SUPPORT", );
}
$this->handler = new Redis();
//从配置读取
$this->handler->connect($data['hostname'],$data['port']);
$this->handler->auth($data['auth']);
$this->handler->select($dbindex);
}
public static function getInstance($dbindex = ){
if(!isset(self::$_instance[$dbindex]) or FALSE == (self::$_instance[$dbindex] instanceof self)){
self::$_instance[$dbindex] = new self($dbindex);
}
return self::$_instance[$dbindex];
} /**key value get**/
public function GET($key)
{
return $this->handler->get($key);
}
/**key value set 过期时间为 $exp**/
public function SET($key ,$value ,$exp)
{
$this->handler->setex($key ,$exp ,$value );
} /*移除数据$key*/
public function REMOVE($key)
{
$this->handler->delete($key); }
/*设置数据的过期时间$key*/
public function EXPIRE($key ,$exp)
{
$this->handler->expire($key ,$exp);
}
/**Hash 相关**/ public function HGET($domain , $key)
{
return $this->handler->hGet($domain , $key);
}
public function HSET ($domain ,$key ,$value )
{
$this->handler->hSet($domain , $key);
} public function HREMOVE($domain ,$key)
{
$this->handler->hDel($domain , $key);
}
public function HGETALL($key = '' ){
return $this->handler->hGetAll($key);
}
public function HMset($key = '' , $value = array()){
return $this->handler->hMset($key , $value );
}
/*插入列表*/
public function PushList($channel,$data)
{
$this->handler->lPush($channel,$data);
} /*从列表中获取*/
public function POPList($channel)
{
return $this->handler->lPop($channel);
} public function SADD($hash ,$value){ return $this->handler->SADD($hash ,$value);
} public function SMEMBERS($hash){
return $this->handler->SMEMBERS($hash );
} /**
* pj
* 用于批量获取指定
* @param [type] $key [description]
* 例如:
* $key = "pj_group_*";//获取以pj_group_
$cache = RedisClass::getInstance(12);
$data = $cache->KEYS($key);
$cache->DELKEYS($data);
*/
public function KEYS($key){//获取指定的key 或者指定前缀的key
return $this->handler->keys($key );
}
public function DELKEYS($data = array()){
return $this->handler->delete($data);
}
}
?>

批量删除redis缓存的思路:

先获取要删除的redis前缀,比如“pj_group_*”为前缀的

然后直接delete掉这些key就可以了

//删除指定开始的前缀缓存
public function indexAction(){
$key = "pj_group_*";//当前openid
$cache = RedisClass::getInstance();
$data = $cache->KEYS($key);
$cache->DELKEYS($data);
}

redis删除指定前缀的缓存的更多相关文章

  1. laravel redis 删除指定前缀的 key

    // 前缀 $prefix = 'abc'; // 需要在前面连接上应用的缓存前缀 $keys = app('redis')->keys(config('cache.prefix') . $pr ...

  2. Redis删除相同前缀的key

          如何优雅地删除Redis set集合中前缀相同的key?       Redis中有删除单条数据的命令DEL,却没有批量删除特定前缀key的指令,但我们经常遇到需要根据前缀来删除的业务场景 ...

  3. php redis 获取指定前缀的所有key

    php redis 获取指定前缀的所有key 以laravel框架为例: $key = $this->redis->keys('db:shipping:shippingId:' . &qu ...

  4. Redis删除特定前缀key的优雅实现

    还在用keys命令模糊匹配删除数据吗?这就是一颗随时爆炸的炸弹! Redis中没有批量删除特定前缀key的指令,但我们往往需要根据前缀来删除,那么究竟该怎么做呢?可能你一通搜索后会得到下边的答案 re ...

  5. MySQL批量删除指定前缀表

    Select CONCAT( 'drop table ', table_name, ';' ) FROM information_schema.tables Where table_name LIKE ...

  6. MySQL中批量删除指定前缀表的sql语句

    有时候我们在安装一些cms的时候,这些cms都是带表前缀的方便区分数据,但有时候我们测试完需要删除的时候又有别的前缀表就可以参考下面的方法 代码如下:Select CONCAT( 'drop tabl ...

  7. mysql批量删除指定前缀或后缀表

    今天突然发现我们数据库中多出很多表,后缀名为"copy",预计是navicat直接拷贝导致的,然后要对这些有同样后缀名的表进行删除,假设一个一个选择会非常麻烦,表计较多,在网上找了 ...

  8. Redis【知识点】批量删除指定Key

    Redis中有删除单条数据的命令DEL但是他没有批量删除多条数据的方法,那我们怎么去批量删除多条数据呢! 第一种方式 /work/app/redis/bin/redis-cli -a youpassw ...

  9. redis删除list中指定index的值

    Redis的List删除命令: lrem : lrem mylist 0 "value"    //从mylist中删除全部等值value的元素   0为全部,负值为从尾部开始. ...

随机推荐

  1. __str__&__repr__

    [__str__&__repr__] object.__str__(self): Called by the str() built-in function and by the print  ...

  2. Kafka存储机制(转)

    转自:https://www.cnblogs.com/jun1019/p/6256514.html Kafka存储机制 同一个topic下有多个不同的partition,每个partition为一个目 ...

  3. Cocoapods 版本升级

    和往常一样使用 Cocoapods ,执行命令: $ pod install #输出信息 /System/Library/Frameworks/Ruby.framework/Versions/2.0/ ...

  4. code2800 送外卖

    首先,对图进行一次Floyd(g[][]是图) 1.dfs:(u是当前在的节点,d是已经走的路程) void dfs(int u,int d){ if(d>=ans)return; bool f ...

  5. $_SERVER['HTTP_REFERER']

    $_SERVER['HTTP_REFERER']//获取前一个页面的url地址

  6. duilib界面库

    xml编写界面库 notify控制程序 win32程序 winmain主函数 复杂控件自绘

  7. 44个javascript 变态题解析

    原题来自: javascript-puzzlers 读者可以先去做一下感受感受. 当初笔者的成绩是 21/44… 当初笔者做这套题的时候不仅怀疑智商, 连人生都开始怀疑了…. 不过, 对于基础知识的理 ...

  8. OBD Problem Vehicles

    This page contains a list of vehicles that are known to be non-compliant with OBD-II in one way or a ...

  9. VS2012用正则表达式统计行数

    使用正则表达式: b*[^:b#/]+.*$

  10. 支持stl容器的gdb自定义命令

    # 本文可以从https://sourceware.org/ml/gdb/2008-02/msg00064/stl-views.gdb直接下载 # 有关gdb的高级使用,请浏览:http://blog ...