PHP operation redis implementation of the paging list add delete function encapsulation class and usage examples
- 2021-10-25 06:14:17
- OfStack
This article describes the PHP operation redis implementation of the paging list, add, delete function encapsulation class and usage. Share it for your reference, as follows:
<?php
/*
* redis Paged data class library
*/
class redisPage{
protected $_redis;
protected $_redis_ip = '127.0.0.1'; //ip
protected $_redis_port = 6379; // Port
protected $_redis_db = 0; // Database number
protected $_hash_prefix = 'my_data'; // Prefix name
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);
}
/*
* Add a record
* @param $id id
* @param $data hash Data
* @param $hashName Hash Record name
* @param $SortName Redis SortSet Record name
* @param $redis Redis Object
* @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;
}
/*
* Get paging data
* @param $page Current number of pages
* @param $pageSize How many articles per page
* @param $hashName Hash Record name
* @param $SortName Redis SortSet Record name
* @param $redis Redis Object
* @param $key Field array Do not pass to take out all fields
* @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); // Within the specified interval, with the score Value ( Optional ) Gets a list of members of the ordered set of.
$count = $this->_redis->zCard($this->_hash_prefix.'_sort'); // Statistics ScoreSet Total
$pageCount = ceil($count/$pageSize); // How many pages are there altogether
$pageList = array();
foreach($range as $qid){
if(count($key) > 0){
$pageList[] = $this->_redis->hMGet($this->_hash_prefix.'_'.$qid,$key); // Get hash All the data in the table
}else{
$pageList[] = $this->_redis->hGetAll($this->_hash_prefix.'_'.$qid); // Get hash All the data in the table
}
}
$data = array(
'data'=>$pageList, // Demand data
'page'=>array(
'page'=>$page, // Current number of pages
'pageSize'=>$pageSize, // How many articles per page
'count'=>$count, // Total number of records
'pageCount'=>$pageCount // Total pages
)
);
return $data;
}
/*
* Delete a record
* @param $id id
* @param $hashName Hash Record name
* @param $SortName Redis SortSet Record name
* @param $redis Redis Object
* @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;
}
/*
* Empty data
* @param string $type db: Empty the current database all: Empty all databases
* @return bool
*/
public function clear($type='db'){
if($type == 'db'){
$this->_redis->flushDB();
}elseif($type == 'all'){
$this->_redis->flushAll();
}else{
return false;
}
return true;
}
}
// Database
$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'); // Instantiate object
$redis->clear(); // Test empty data
while($info = mysql_fetch_assoc($query)){
$redis->set_redis_page_info($info['nodeid'],$info); // Insert data
}
$redis->del_redis_page_info(array(61)); // Delete data
$data = $redis->get_redis_page_info(1,10,array('nodeid','name')); // Get paging data
print_r($data);
?>
More readers interested in PHP can check out the topics on this site: "Summary of php+redis Database Programming Skills", "Introduction to php Object-Oriented Programming", "Introduction to PHP Basic Syntax", "Encyclopedia of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"
I hope this article is helpful to everyone's PHP programming.