<?php
namespace app\index\controller;
use Redis;
class Index
{
    public function index()
    {
        $redis = new Redis();
        $redis->connect('127.0.0.1', 6379);
        $redis->flushAll();

        $redis->rPush('key1', 'A');
        $redis->rPush('key1', 'B');
        $redis->rPush('key1', 'C');
        $lp = $redis->lRange('key1',0,-1);
        var_dump($lp);//array(3) { [0]=> string(1) "A" [1]=> string(1) "B" [2]=> string(1) "C" }
        echo "<hr>";
        // lPop() 移除并返回列表的第一个元素
        echo $redis->lPop('key1');// A
        echo $redis->lPop('key1');// B
        echo $redis->lPop('key1');// C
        $lp = $redis->lRange('key1',0,-1);
        var_dump($lp);//array(0) { }
        echo "<hr/>";

        $redis->rPush('k2', 'a');
        $redis->rPush('k2', 'b');
        $redis->rPush('k2', 'c');
        $rp = $redis->lRange('k2',0,-1);
        var_dump($rp);//array(3) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" }
        echo "<hr/>";

        // rPop() 移除并返回列表的最后一个元素
        echo $redis->rPop('k2');//c
        echo $redis->rPop('k2');//b
        echo $redis->rPop('k2');//a
        $rp = $redis->lRange('k2',0,-1);
        var_dump($rp);//array(0) { }


    }



}

 

更多推荐