thinkphp5 redis使用


參數參考位置:thinkphp\library\think\cache\driver

class Redis extends Driver
{
    protected $options = [
        'host'       => '127.0.0.1',
        'port'       => 6379,
        'password'   => '',
        'select'     => 0,
        'timeout'    => 0,
        'expire'     => 0,
        'persistent' => false,
        'prefix'     => '',
    ];

方式一:控制器

public function index(){
        $config = [
            'host' => '192.168.70.161',
            'port' => 6379,
            'password' => 'admin999',
            'select' => 0,
            'timeout' => 0,
            'expire' => 0,
            'persistent' => false,
            'prefix' => '',
        ];
 
        $Redis=new Redis($config);
        $Redis->set("test","test");
        echo $Redis->get("test");
    }

方式二:符合類型緩存(配置文件)

config.php

  'cache'                  => [
        // 使用復合緩存類型
        'type'  =>  'complex',
        // 默認使用的緩存
        'default'   =>  [
            // 驅動方式
            'type'   => 'redis',
            // 緩存保存目錄
            'path'   => CACHE_PATH,
        // 文件緩存
            ],
        'file'   =>  [
            // 驅動方式
            'type'   => 'file',
            // 設置不同的緩存保存目錄
            'path'   => CACHE_PATH,
        ],
//        // 驅動方式

        // redis緩存
        'redis'   =>  [
            // 驅動方式
            'type'   => 'redis',
            // 服務器地址
            'host'  => '192.168.70.161',
            'expire' => 0,
            'port' => 6379,
            'password'=>'hello',
            'select'=>14 //選擇幾號庫
            

        ],
Cache::store('redis')->set('name','999');
Cache::store('file')->set('name1','999');

鏈接方式三:配置文件

//redis連接
            $redis = new \Redis();
            $redis->connect('192.168.70.161','6379');
            $redis->auth('hello');
            $redis->select(14);
       $redis->Lrange('runoobkey','0','10') 

// thinkphp 默認不能使用redis的push、lLen等操作,需要連接自帶的redis.php

方式一和方式二連接都無法使用redis自帶的push,lLen,Lrange方法,只能使用thinkphp自帶的redis幾個方法


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM