PHP redis 切换数据库

redis 数据库db0-db15
默认情况下,redis会生成0-15号共16个db,以供不同情境使用的需要
不同的数据库下,相同的key各自独立

<?php
/**
 * Redis 操作类
 * 可以使用redis的所有数据类型,而tp5仅可使用string类型+
 * 字符串(String)  哈希(Hash)  列表(List)  集合(Set) 有序集合(sorted set)
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/10/21
 * Time: 10:45
 */

namespace app\common;


class Redis  extends \Redis
{
    
    

    public  function __construct()
    {
    
    
        $host = config('redis.host');
        $password = config('redis.password');
        $this->connect($host);
        if($password){
    
    
            $this->auth($password);
        }

    }


    public function auth($password)
    {
    
    
        parent::auth($password); // TODO: Change the autogenerated stub
    }


}

调用:

foreach ($iss as $s) {
    
    
	$redis = new Redis();
    $redis->select(1);#选择1号仓
    $redis->rPush('Tracking_num', $s);#以队列方式存储
}


$redis = new Redis();
$redis->select(1);#选择1号仓,如果不选择则无法取出值
$track_num = $redis->lPop('Tracking_num');#以队列的方式取出

猜你喜欢

转载自blog.csdn.net/weixin_42433970/article/details/115208943