redis删除指定前缀的缓存

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

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

以下场景:

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

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

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

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

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

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

<?php
  class RedisClass
  {
    static $_instance; //存储对象
    public $handler ;
      private function __construct($dbindex = 0)
      {      
        global $_G ;
        $data = $_G['config']['redis']['redis']['params']; 
        if ( !extension_loaded('redis') ) {
            throw new Exception("REDIS NOT  SUPPORT", 1);
        }      
        $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 = 0){
        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(12);
        $data = $cache->KEYS($key);
        $cache->DELKEYS($data);
    }
原文地址:https://www.cnblogs.com/xs-yqz/p/7366498.html