redis分頁

模仿的https://www.cnblogs.com/dee0912/p/4612183.htmlphp

第一步鏈接redis後進行添加數據html

require_once '../redis/redis.php';redis

$redis = new RedisDriver();
/*$result = $redis->getStr('userid');
var_dump($result) ;*/json

//uid 自加
//當有 userid 這個鍵時,執行 incr時該鍵的值加1;不存在該鍵時,建立一個 userid,值爲0,執行incr,值爲1數組

/* for ($i=0; $i < 50; $i++) { 函數

$uid = $redis->incrs('userid');
//向 hash 表中批量添加數據:hMset
$age = mt_rand(10,20);
$redis->setHash('user:'.$i, array('uid'=>$i, 'name'=>'name:'.$i, 'age'=>$age));
//把用戶 id 存入鏈表
$redis->rpushs('uid', $i);
}
die;*/ui

 

第二部,從redis中查詢數據進行分頁this

/*分頁*/
//用戶總數
$count = $redis->getListSize('uid');
//頁大小 
$pageSize = 5;
//當前頁碼
$page = !empty($_GET['page'])?$_GET['page']:1;
//頁總數
$pageCount = ceil($count / $pageSize);
//echo $count.'='.$page.'='.$pageCount;die;
/*
第 1 頁:lrange 0 4
第 2 頁:lrange 5 9
第 3 頁:lrange 10 14
第 n 頁:lrange ($page - 1) * $pageSize    ($page - 1) * $pageSize + ($pageSize - 1)
*/
$ids = $redis->lranges('uid', (($page - 1) * $pageSize), (($page - 1) * $pageSize + ($pageSize - 1)));

/*echo "<pre>";
print_r($ids); die;*/
/*
var_dump($ids); 
$page = 1
array(5) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1) "4" [4]=> string(1) "5" }
*/

foreach($ids as $k => $v){
    $data[] = $redis->hgetalls('user:'.$v);
}

//$newArray1 = array_column($data,NULL,'uid');
///array_column() 返回input數組中鍵值爲column_key的列, 若是指定了可選參數index_key,那麼input數組中的這一列的值將做爲返回數組中對應值的鍵。

?>
<table border="1" cellspacing="0" cellpadding="5">
    <tr>
        <th>uid</th>
        <th>name</th>
        <th>age</th>
        <th>操做</th>
    </tr>
    <?php foreach($data as $v){ ?>
    <tr>
        <td><?php echo $v['uid']?></td>
        <td><?php echo $v['name']?></td>
        <td><?php echo $v['age']?></td>
        <td><a >刪除</a> | uid:<?php echo $v['uid'];?></td>
    </tr>
    <?php } ?>
    <tr><td colspan="4">
            <a href="?page=<?php echo ($page-1)<=1?1:($page-1);?>">上一頁</a>
            <a href="?page=<?php echo ($page+1)>$pageCount?$pageCount:($page+1);?>">下一頁 </a>
            <a href="?page=1">首頁</a>
            <a href="?page=<?php echo $pageCount;?>">尾頁</a>
            當前<?php echo $page;?>頁 
            總共<?php echo $pageCount;?>頁 
            共<?php echo $count;?>用戶
        </td>
    </tr>
</table>
</body>
</html>

require_once '../redis/redis.php';上面引入的redis類spa

<?php
 
/**
 * ------------------------------------------
 * 統一redis的配置與數據存儲規範,便於擴展與修改
 * # redis一般用於熱數據與消息列隊等場景
 * # list內存儲array是採用json格式
 *
 */
 
class RedisDriver
{
 
    public $redis   = null;    

    public function __construct() { 
        if (is_null($this->redis)) {
            $this->redis = new \Redis();    
            $this->redis->connect('127.0.0.1', '6379');
        }
    }
   
   /**給key加一
    * [incrs description]
    * @param  [type] $key [description]
    * @return [type]      [description]
    */
    public function incrs($key) {
        $this->redis->incr($key);
    }

    // 設置一條String
    public function setStr($key, $text, $expire = null)
    {
        $key = 'string:' . $key;
        $this->redis->set($key, $text);
        if (!is_null($expire)) {
            $this->redis->setTimeout($key, $expire);
        }
    }
 
    // 獲取一條String
    public function getStr($key)
    {
        $key = 'string:' . $key;
        $text = $this->redis->get($key);
        return empty($text) ? null : $text;
    }
 
    // 刪除一條String
    public function delStr($key)
    {
        $key = 'string:' . $key;
        $this->redis->del($key);
    }
 
    // 設置一條Hash
    public function setHash($key, $arr, $expire = null)
    {
        $key = $key;
        $this->redis->hMset($key, $arr);
        if (!is_null($expire)) {
            $this->redis->setTimeout($key, $expire);
        }
    }

    public function hgetalls($key) {
        $arr =  $this->redis->hgetall($key);
        return empty($arr) ? 'null' : $arr;
    }
 
    // 獲取一條Hash,$fields可爲字符串或數組
    public function getHash($key, $fields = null)
    {
        $key = 'hash:' . $key;
        if (is_null($fields)) {
            $arr = $this->redis->hGetAll($key);
        } else {
            if (is_array($fields)) {
 
                $arr = $this->redis->hmGet($key, $fields);
                foreach ($arr as $key => $value) {
                    if ($value === false) {
                        unset($arr[$key]);
                    }
                }
            } else {
                $arr = $this->redis->hGet($key, $fields);
            }
        }
        return empty($arr) ? null : (is_array($arr) ? $arr : array($fields => $arr));
    }
 
    // 刪除一條Hash,$field爲字符串
    public function delHash($key, $field = null)
    {
        $key = 'hash:' . $key;
        if (is_null($field)) {
            $this->redis->del($key);
        } else {
            $this->redis->hDel($key, $field);
        }
    }
 
    // 在Hash的field內增長一個值 (值之間使用「,」分隔)
    public function fieldAddVal($key, $field, $val)
    {
        $arr = $this->getHash($key, $field);
        if (!is_null($arr)) {
            $str = reset($arr);
            $arr = explode(',', $str);
            foreach ($arr as $v) {
                if ($v == $val) {
                    return;
                }
            }
            $str .= ",{$val}";
            $this->setHash($key, array($field => $str));
        } else {
            $this->setHash($key, array($field => $val));
        }
    }
 
    // 在Hash的field內刪除一個值
    public function fieldDelVal($key, $field, $val)
    {
        $arr = $this->getHash($key, $field);
        if (!is_null($arr)) {
            $arr = explode(',', reset($arr));
            $tmpStr = '';
            foreach ($arr as $v) {
                if ($v != $val) {
                    $tmpStr .= ",{$v}";
                }
            }
            if ($tmpStr == '') {
                $this->delHash($key, $field);
            } else {
                $this->setHash($key, array($field => substr($tmpStr, 1)));
            }
        }
    }
 
    // 設置表格的一行數據
    public function setTableRow($table, $id, $arr, $expire = null)
    {
        $key = '' . $table . ':' . $id;
        $this->redis->hMset($key, $arr);
        if (!is_null($expire)) {
            $this->redis->setTimeout($key, $expire);
        }
    }
 
    // 獲取表格的一行數據,$fields可爲字符串或數組
    public function getTableRow($table, $id, $fields = null)
    {
        $key = '' . $table . ':' . $id;
        if (is_null($fields)) {
            $arr = $this->redis->hGetAll($key);
        } else {
            if (is_array($fields)) {
                $arr = $this->redis->hmGet($key, $fields);
                foreach ($arr as $key => $value) {
                    if ($value === false) {
                        unset($arr[$key]);
                    }
                }
            } else {
                $arr = $this->redis->hGet($key, $fields);
            }
        }
        return empty($arr) ? null : (is_array($arr) ? $arr : array($fields => $arr));
    }
 
    // 刪除表格的一行數據
    public function delTableRow($table, $id)
    {
        $key = '' . $table . ':' . $id;
        $this->redis->del($key);
    }
 
    public function rpushs($key, $value) {
        $this->redis->rpush($key,$value);
    }

    public function lranges($key,$start,$end) {
        return $this->redis->lrange($key,$start,$end);
    }

    // 推送一條數據至列表,頭部
    public function pushList($key, $arr)
    {
        $key = 'list:' . $key;
        $this->redis->lPush($key, json_encode($arr));
    }
 
    // 從列表拉取一條數據,尾部
    public function pullList($key, $timeout = 0)
    {
        $key = 'list:' . $key;
        if ($timeout > 0) {
            $val = $this->redis->brPop($key, $timeout); // 該函數返回的是一個數組, 0=key 1=value
        } else {
            $val = $this->redis->rPop($key);
        }
        $val = is_array($val) && isset($val[1]) ? $val[1] : $val;
        return empty($val) ? null : $this->objectToArray(json_decode($val));
    }
 
    // 取得列表的數據總條數
    public function getListSize($key)
    {
        $key = $key;
        return $this->redis->lSize($key);
    }
 
    // 刪除列表
    public function delList($key)
    {
        $key = 'list:' . $key;
        $this->redis->del($key);
    }
 
    // 使用遞歸,將stdClass轉爲array
    protected function objectToArray($obj)
    {
        if (is_object($obj)) {
            $arr = (array) $obj;
        }
        if (is_array($obj)) {
            foreach ($obj as $key => $value) {
                $arr[$key] = $this->objectToArray($value);
            }
        }
        return !isset($arr) ? $obj : $arr;
    }
 
}
相關文章
相關標籤/搜索