当前位置:网站首页>TP5 uses redis

TP5 uses redis

2022-04-23 06:52:00 Collect and study by yourself

1. open tp5 The official manual , Under cache, find using multiple cache types

Paste the following code into tp frame config The following cache settings

 

//  Switch to redis operation 
Cache::store('redis')->set('name','value');

// obtain redis
Cache::store('redis')->get('rename');

// Realization redis Message queue

stay /thinkphp/library/think/cache/driver/Redis.php Inside the file

// Add data to the queue 
public function lPush($key, $value)
{
    return $this->handler->lPush($key, $value);
}
// Get data from the queue 
public function lPop($key)
{
     return $this->handler->lPop($key);
}

Call... In the controller

// Storage 
Cache::store('redis')->handler()->lPush('k','v');
// obtain 
Cache::store('redis')->handler()->lPop('k');

A little more complicated

/**
     *  stay list The added value to the left of is $value The elements of 
     * @access public
     * @param $key  Indexes 
     * @param $value  value 
     * @return int
     */
    public function lPush($key,$value){
        return $this->handler->lPush($key,$value);
    }

    /**
     *  stay list The value on the right of is $value The elements of 
     * @access public
     * @param $key  Indexes 
     * @return int
     */
    public function lPop($key){
        return $this->handler->lPop($key);
    }

    /**
     *  stay list Remove duplicate values 
     * @access public
     * @param $key  Indexes 
     * @return int
     */
    public function lrem($key1,$key2){
        return $this->handler->lrem ($key1,$key2);
    }


    /**
     *  stay list Judge 
     * @access public
     * @param $key  Indexes 
     * @return int
     */
    public function lLen($key){
        return $this->handler->lLen ($key);
    }

Use in controller

 Here is the code in the method :
$list=array(
    '1'=>'a',
    '2'=>'b',
    '3'=>'c'
);
$redis=new Redis();
$keys='test1';
$total=$redis->lLen($keys);// Return key name test1 length 
if($total == 0){
    foreach ($list as $key => $val){
        $redis->lPush($keys,$val);// Write the value of the array to the key name test1 Of the queue 
    }
}

// Here is the... Taken out , If you take it out elsewhere , Delete this paragraph 
$data=$redis->lPop($keys);// Remove key name test1 The first data in the queue 
if (!$data) {
    return ' Completed ';
}
$redis->lrem($keys,$data);// duplicate removal 

版权声明
本文为[Collect and study by yourself]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230555551672.html