1. 程式人生 > 其它 >tp5.0 實現redis 訊息佇列

tp5.0 實現redis 訊息佇列

1.在專案config.php配置檔案類 配置chahe

程式碼:

'cache'                  => [
        // 驅動方式
        'type'   => 'redis',
        // 快取儲存目錄
        'path'   => CACHE_PATH,
        // 快取字首
        'prefix' => '',
        // 快取有效期 0表示永久快取
        'expire' => 0,
        // redis快取
        'redis'   =>  [
            
// 驅動方式 'type' => 'redis', // 伺服器地址 'host' => '127.0.0.1', ], ],

在 /thinkphp/library/think/cache/driver/Redis.php 檔案裡面封裝

//向佇列新增資料
public function lPush($key, $value)
{
    return $this->handler->lPush($key, $value);
}
//向佇列裡面取資料
public function
lPop($key) { return $this->handler->lPop($key); }

在控制器裡面呼叫

//儲存
Cache::store('redis')->handler()->lPush('k','v');
//獲取
Cache::store('redis')->handler()->lPop('k');