PHP操作redis實(shí)現(xiàn)的分頁(yè)列表,新增,刪除功能封裝類(lèi)與用法示例
本文實(shí)例講述了PHP操作redis實(shí)現(xiàn)的分頁(yè)列表,新增,刪除功能封裝類(lèi)與用法。分享給大家供大家參考,具體如下:
<?php
/*
* redis 分頁(yè)數(shù)據(jù)類(lèi)庫(kù)
*/
class redisPage{
protected $_redis;
protected $_redis_ip = '127.0.0.1'; //ip
protected $_redis_port = 6379; //端口
protected $_redis_db = 0; //數(shù)據(jù)庫(kù)號(hào)
protected $_hash_prefix = 'my_data'; //前綴名稱(chēng)
public function __construct($ip='',$port='',$db='',$hash_prefix=''){
if($ip != '') $this->_redis_ip = $ip;
if($port != '') $this->_redis_port = $port;
if($db != '') $this->_redis_db = $db;
if($hash_prefix != '') $this->_hash_prefix = $hash_prefix;
$this->_redis = new Redis();
$this->_redis->connect($this->_redis_ip, $this->_redis_port);
$this->_redis->select($this->_redis_db);
}
/*
* 添加記錄
* @param $id id
* @param $data hash數(shù)據(jù)
* @param $hashName Hash 記錄名稱(chēng)
* @param $SortName Redis SortSet 記錄名稱(chēng)
* @param $redis Redis 對(duì)象
* @return bool
*/
public function set_redis_page_info($id,$data){
if(!is_numeric($id) || !is_array($data)) return false;
$hashName = $this->_hash_prefix.'_'.$id;
$this->_redis->hMset($hashName, $data);
$this->_redis->zAdd($this->_hash_prefix.'_sort',$id,$id);
return true;
}
/*
* 獲取分頁(yè)數(shù)據(jù)
* @param $page 當(dāng)前頁(yè)數(shù)
* @param $pageSize 每頁(yè)多少條
* @param $hashName Hash 記錄名稱(chēng)
* @param $SortName Redis SortSet 記錄名稱(chēng)
* @param $redis Redis 對(duì)象
* @param $key 字段數(shù)組 不傳為取出全部字段
* @return array
*/
public function get_redis_page_info($page,$pageSize,$key=array()){
if(!is_numeric($page) || !is_numeric($pageSize)) return false;
$limit_s = ($page-1) * $pageSize;
$limit_e = ($limit_s + $pageSize) - 1;
$range = $this->_redis->ZRANGE($this->_hash_prefix.'_sort',$limit_s,$limit_e); //指定區(qū)間內(nèi),帶有 score 值(可選)的有序集成員的列表。
$count = $this->_redis->zCard($this->_hash_prefix.'_sort'); //統(tǒng)計(jì)ScoreSet總數(shù)
$pageCount = ceil($count/$pageSize); //總共多少頁(yè)
$pageList = array();
foreach($range as $qid){
if(count($key) > 0){
$pageList[] = $this->_redis->hMGet($this->_hash_prefix.'_'.$qid,$key); //獲取hash表中所有的數(shù)據(jù)
}else{
$pageList[] = $this->_redis->hGetAll($this->_hash_prefix.'_'.$qid); //獲取hash表中所有的數(shù)據(jù)
}
}
$data = array(
'data'=>$pageList, //需求數(shù)據(jù)
'page'=>array(
'page'=>$page, //當(dāng)前頁(yè)數(shù)
'pageSize'=>$pageSize, //每頁(yè)多少條
'count'=>$count, //記錄總數(shù)
'pageCount'=>$pageCount //總頁(yè)數(shù)
)
);
return $data;
}
/*
* 刪除記錄
* @param $id id
* @param $hashName Hash 記錄名稱(chēng)
* @param $SortName Redis SortSet 記錄名稱(chēng)
* @param $redis Redis 對(duì)象
* @return bool
*/
public function del_redis_page_info($id){
if(!is_array($id)) return false;
foreach($id as $value){
$hashName = $this->_hash_prefix.'_'.$value;
$this->_redis->del($hashName);
$this->_redis->zRem($this->_hash_prefix.'_sort',$value);
}
return true;
}
/*
* 清空數(shù)據(jù)
* @param string $type db:清空當(dāng)前數(shù)據(jù)庫(kù) all:清空所有數(shù)據(jù)庫(kù)
* @return bool
*/
public function clear($type='db'){
if($type == 'db'){
$this->_redis->flushDB();
}elseif($type == 'all'){
$this->_redis->flushAll();
}else{
return false;
}
return true;
}
}
//數(shù)據(jù)庫(kù)
$host='localhost';
$user='root';
$psd='';
$dbname='china';
$link = @mysql_connect($host,$user,$psd);
mysql_select_db($dbname,$link);
mysql_query("set names utf8");
$SQL = "SELECT * FROM js_collection_node order by nodeid asc limit 100 ";
$query = mysql_query($SQL);
$redis = new redisPage('127.0.0.1',6379,0,'collection_node'); //實(shí)例化對(duì)象
$redis->clear(); //測(cè)試清空數(shù)據(jù)
while($info = mysql_fetch_assoc($query)){
$redis->set_redis_page_info($info['nodeid'],$info); //插入數(shù)據(jù)
}
$redis->del_redis_page_info(array(61)); //刪除數(shù)據(jù)
$data = $redis->get_redis_page_info(1,10,array('nodeid','name')); //獲取分頁(yè)數(shù)據(jù)
print_r($data);
?>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《php+redis數(shù)據(jù)庫(kù)程序設(shè)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《PHP基本語(yǔ)法入門(mén)教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
Php 構(gòu)造函數(shù)construct的前下劃線(xiàn)是雙的_
最近寫(xiě)php的class時(shí),總是碰到function non object的錯(cuò)誤,知道是類(lèi)沒(méi)有實(shí)例化,但就是不知道錯(cuò)誤在哪里。2009-12-12
PHP設(shè)計(jì)模式的策略,適配器和觀(guān)察者模式詳解
這篇文章主要為大家詳細(xì)介紹了PHP設(shè)計(jì)模式的策略,適配器和觀(guān)察者模式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03
PHP多維數(shù)組遍歷方法(2種實(shí)現(xiàn)方法)
這篇文章主要介紹了PHP多維數(shù)組遍歷方法,實(shí)例分析了2種多維數(shù)組的遍歷技巧,包括簡(jiǎn)單的foreach遍歷與遞歸操作遍歷實(shí)現(xiàn)方法,需要的朋友可以參考下2015-12-12
編寫(xiě)PHP腳本過(guò)濾用戶(hù)上傳的圖片
這篇文章主要介紹了編寫(xiě)PHP腳本過(guò)濾用戶(hù)上傳的圖片,至于是不是能達(dá)到設(shè)想的準(zhǔn)確過(guò)濾那種圖片的效果,這個(gè)就...需要的朋友可以參考下2015-07-07
封裝一個(gè)PDO數(shù)據(jù)庫(kù)操作類(lèi)代碼
數(shù)據(jù)庫(kù)PDO操作類(lèi),網(wǎng)上好多朋友需要的,可以參考下。2009-09-09

