redis實現分頁功能,主要是將數據緩存起來,無需頻繁查詢數據庫,減少數據庫的壓力。
適用場景:單用戶操作列表界面分頁,如博客列表。
缺點:不可模糊查詢,缺少靈活性。
封裝類:
class XgRedis { protected $_redis; public function __construct($hash_prefix=''){ $this->_redis = connectRedis::getinstance();; //$this->_redis = Redis::connection(); } /* * 添加記錄 * @param $hash_prefix 前綴 * @param $id 記錄id * @param $data 數據 * @return bool 返回值 */ public function set_redis_page_info($hash_prefix,$id,$data){ if(!is_numeric($id) || !is_array($data)) return false; $hashName = $hash_prefix.'_'.$id; //同時設置 hash 的多個 field,已存在會自動更新 $this->_redis->hmset($hashName, $data); //添加元素到有序集合,元素在集合中存在則更新對應的score(權)(key,權,值) $this->_redis->zadd($hash_prefix.'_sort',$id,$id); return true; } /* * 獲取分頁數據 * @param $hash_prefix 前綴 * @param $page 當前頁數 * @param $pageSize 每頁多少條 * @param $hashName Hash 記錄名稱 * @param $SortName Redis SortSet 記錄名稱 * @param $redis Redis 對象 * @param $key 字段數組 不傳為取出全部字段 * @return array */ public function get_redis_page_info($hash_prefix,$page,$pageSize,$key=array()){ if(!is_numeric($page) || !is_numeric($pageSize)) return false; $limit_s = ($page-1) * $pageSize; $limit_e = ($limit_s + $pageSize) - 1; //類似lrange操作從集合中去指定區間的元素,返回的是帶有 score 值(可選)的有序結果集: $range = $this->_redis->zrange($hash_prefix.'_sort',$limit_s,$limit_e); //統計ScoreSet集合元素總數 $count = $this->_redis->zcard($hash_prefix.'_sort'); $pageCount = ceil($count/$pageSize); //總共多少頁 $pageList = array(); foreach($range as $qid){ if(count($key) > 0){ $pageList[] = $this->_redis->hmget($hash_prefix.'_'.$qid,$key); //獲取hash表中的field所有的數據 }else{ $pageList[] = $this->_redis->hgetall($hash_prefix.'_'.$qid); //獲取hash表中所有的數據 } } $data = array( 'data'=>$pageList, //需求數據 'page'=>array( 'page'=>$page, //當前頁數 'pageSize'=>$pageSize, //每頁多少條 'count'=>$count, //記錄總數 'pageCount'=>$pageCount //總頁數 ) ); return $data; } /* * 獲取單條記錄 * @param $id id * @return array */ public function show_redis_page_info($hash_prefix,$id){ $info = $this->_redis->hgetall($hash_prefix.'_'.$id); return $info; } /* * 刪除記錄 單條或多條 * @param $ids ids 數組形式 [1,2,3] * @param $hashName Hash 記錄名稱 * @param $SortName Redis SortSet 記錄名稱 * @param $redis Redis 對象 * @return bool */ public function del_redis_page_info($hash_prefix,$ids){ if(!is_array($ids)) return false; foreach($ids as $value){ $hashName = $hash_prefix.'_'.$value; $this->_redis->del($hashName); $this->_redis->zrem($hash_prefix.'_sort',$value); } return true; } /* * 清空數據 * @param string $type db:清空當前數據庫 all:清空所有數據庫 * @return bool */ public function clear($type='db'){ if($type == 'db'){ $this->_redis->flushdb(); }elseif($type == 'all'){ $this->_redis->flushall(); }else{ return false; } return true; } /* * 入棧單條數據,針對某個用戶可以,多用戶並發就不好了 * @param $hash_prefix 前綴 * @param $data 數據 * @return bool 返回值 */ public function in_data($hash_prefix,$data){ if(count($data) != count($data,1)) echo '須為一維數組';return false; if(!is_array($data)) return false; //統計ScoreSet集合元素總數 $id = $this->_redis->zcard($hash_prefix.'_sort'); $hashName = $hash_prefix.'_'.$id; //同時設置 hash 的多個 field,已存在會自動更新 $this->_redis->hmset($hashName, $data); //添加元素到有序集合,元素在集合中存在則更新對應的score(權)(key,權,值) $this->_redis->zadd($hash_prefix.'_sort',$id,$id); return true; } }
連接redis:
class connectRedis { static $redis_port = '6379'; static $redis_host = '127.0.0.1'; private function __construct() { } static public $instance; static public function getinstance() { if (!self::$instance) self::$instance = new self(); try { $redis = new Redis(); $redis->connect(static::$redis_host, static::$redis_port); } catch (Exception $e) { die('RedisException : Can\'t connect to ' . static::$redis_host . ':' . static::$redis_port); } return $redis; } }
連接數據庫:
class connectVboxDb{ static $db = 'demo'; static $host = '192.168.1.20'; static $pdo; private function __construct(){ $pdo = new PDO('mysql:host='.static::$host.';dbname='.static::$db,'root','Aaa2019.cn'); $pdo->query('set names utf8');//設置字符集 static::$pdo = $pdo; } static public $instance; static public function getinstance(){ if(!self::$instance) self::$instance = new self(); return self::$instance; } static public function fetchAll($sql){ $qry = static::$pdo->query($sql); $rst = $qry->fetchAll(PDO::FETCH_ASSOC); return $rst; } }
index:
require_once('../connectRedis.php'); require_once('xgredis.php'); require_once('../../connectVboxDb.php'); $XgRedis = new XgRedis(); connectVboxDb::getinstance(); $post = connectVboxDb::fetchAll('select * from PaydayCardOrder'); $key_name = 'PaydayCardOrderList'; //var_dump($post);die; foreach ($post as $k => $v) { //將每條數據信息存儲起來,這里key是數組key,信息后期有添加可以入棧添加,不影響其他信息, $XgRedis->set_redis_page_info($key_name, $k, $v); } //$XgRedis->in_data($key_name,$post[0]); //var_dump($XgRedis->del_redis_page_info($key_name,[52,53])); var_dump($XgRedis->get_redis_page_info($key_name, 1, 100)); //var_dump($XgRedis->show_redis_page_info($key_name,0));
總結:redis做為緩存工具使用,
① 定義的key要簡潔明了,不可過長。
② 無用數據要及時清除掉,防止服務器內存過大。清除的時候,建議設置過期時間,防止當前有其他用戶使用。
③ 更新緩存數據,應當先更新數據庫,再更新redis緩存,防止並發下出現臟數據。
當然網上很多也很全,自己整理的先就這些。