≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > PHP笔记

php利用redis实现分页列表,新增,删除功能

人气:976 时间:2018-10-25

这篇文章主要为大家详细介绍了php利用redis实现分页列表,新增,删除功能,具有一定的参考价值,可以用来参考一下。

感兴趣php利用redis实现分页列表,新增,删除功能的小伙伴,下面一起跟随四海网的小编罗X来看看吧。
<?php
/*
 * redis 分页数据类库
 * @param 
 * @arrange (512.笔记) www.q1010.com
 */
class redisPage{
    protected $_redis;
    protected $_redis_ip = '127.0.0.1'; //ip
    protected $_redis_port = 6379; //端口
    protected $_redis_db = 0; //数据库号
    protected $_hash_prefix = 'my_data'; //前缀名称
  
    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数据
     * @param $hashName Hash 记录名称
     * @param $SortName Redis SortSet 记录名称
     * @param $redis Redis 对象
     * @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;
    }
  
    /*
     * 获取分页数据
     * @param $page 当前页数
     * @param $pageSize 每页多少条
     * @param $hashName Hash 记录名称
     * @param $SortName Redis SortSet 记录名称
     * @param $redis Redis 对象
     * @param $key 字段数组 不传为取出全部字段
     * @return array
     * @param 
     * @arrange (512.笔记) www.q1010.com
     */
    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); //指定区间内,带有 score 值(可选)的有序集成员的列表。
        $count = $this->_redis->zCard($this->_hash_prefix.'_sort'); //统计ScoreSet总数
        $pageCount = ceil($count/$pageSize); //总共多少页
        $pageList = array();
        foreach($range as $qid){
            if(count($key) > 0){
                $pageList[] = $this->_redis->hMGet($this->_hash_prefix.'_'.$qid,$key); //获取hash表中所有的数据
            }else{
                $pageList[] = $this->_redis->hGetAll($this->_hash_prefix.'_'.$qid); //获取hash表中所有的数据
            }
        }
        $data = array(
            'data'=>$pageList, //需求数据
            'page'=>array(
                'page'=>$page, //当前页数
                'pageSize'=>$pageSize, //每页多少条
                'count'=>$count, //记录总数
                'pageCount'=>$pageCount //总页数
            )
        );
        return $data;
    }
  
    /*
     * 删除记录
     * @param $id id
     * @param $hashName Hash 记录名称
     * @param $SortName Redis SortSet 记录名称
     * @param $redis Redis 对象
     * @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;
    }
  
    /*
     * 清空数据
     * @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;
    }
  
}
  
  
  
//数据库
$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'); //实例化对象
$redis->clear(); //测试清空数据
  
while($info = mysql_fetch_assoc($query)){
    $redis->set_redis_page_info($info['nodeid'],$info); //插入数据
}
  
$redis->del_redis_page_info(array(61)); //删除数据
$data = $redis->get_redis_page_info(1,10,array('nodeid','name')); //获取分页数据
print_r($data);
  
?>
 

本文来自:http://www.q1010.com/173/2592-0.html

注:关于php利用redis实现分页列表,新增,删除功能的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:redis

您可能感兴趣的文章

  • Python Redis连接配置的简单示例
  • FastDFS加Redis实现自定义文件名存储海量文件的简单示例
  • memcached 和 redis 使用场景及优缺点解析
  • python redis使用菜鸟教程
上一篇:Memcached::cas 详解
下一篇:php比较好用的字符串截取函数
热门文章
  • PHP 写入WRITE编码为UTF8的文件示例
  • PHP 中文字符串截取函数示例:支持gb2312,gbk,big
  • PHP 简单留言板的制作示例
  • 解决Fatal error: Call to undefined function mb_convert_encoding() in错误问题
  • PHP语言基础(标记、注释、变量、数组、常量、函数)示例
  • php 生成迅雷链接的简单示例
  • php 获取短网址的实现方法
  • PHP 通用分页类的简单示例
  • PHP 使用文件方式导入导出整个MYSQL数据库的实现方法
  • php 获取MYSQL错误的简单示例
  • 最新文章
    • 解决PHP使用redis实现统计缓存MySQL压力的问题
    • php 简单的上传进度条的简单示例
    • php 给html中引用的js和css路径打上版本号的实现方法
    • php 实现计算年龄精准到年月日的实例
    • php+ajax无刷新分页的简单示例
    • 解决php+ajax无刷新上传图片的问题
    • 解决PHP生成HTML静态页面的问题
    • 解决PHP使用uniqid函数生成唯一ID的问题
    • 解决PHP防刷票的一些问题
    • 微信access_token的获取开发的实现方法

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。