1. 程式人生 > >redis刪除指定前綴的緩存

redis刪除指定前綴的緩存

index del 自己 情況 exp sta color 幫助 類庫

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);
    }

redis刪除指定前綴的緩存