一,配置多個redis庫的地址:
.env
APP_DEBUG = true #APP_DEBUG = false [APP] DEFAULT_TIMEZONE = Asia/Shanghai [REDIS0] TYPE = redis HOST = 127.0.0.1 PORT = 6379 PASSWORD = [REDIS1] TYPE = redis HOST = 127.0.0.1 PORT = 6380 PASSWORD = [LANG] default_lang = zh-cn
說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest
對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/
說明:作者:劉宏締 郵箱: 371125307@qq.com
二,配置php到redis的連接
config/cache.php
<?php // +---------------------------------------------------------------------- // | 緩存設置 // +---------------------------------------------------------------------- return [ // 默認緩存驅動 'default' => env('cache.driver', 'file'), // 緩存連接方式配置 'stores' => [ 'file' => [ // 驅動方式 'type' => 'File', // 緩存保存目錄 'path' => '', // 緩存前綴 'prefix' => '', // 緩存有效期 0表示永久緩存 'expire' => 0, // 緩存標簽前綴 'tag_prefix' => 'tag:', // 序列化機制 例如 ['serialize', 'unserialize'] 'serialize' => [], ], // 更多的緩存連接 'redis0' => [ 'type' => env('redis0.type', 'redis'), 'host' => env('redis0.host', '127.0.0.1'), 'port' => env('redis0.port', '6379'), 'password' => env('redis0.password', ''), 'select' => '0', // 全局緩存有效期(0為永久有效) 'expire' => 0, // 緩存前綴 'prefix' => '', 'timeout' => 0, ], // 更多的緩存連接 'redis1' => [ 'type' => env('redis1.type', 'redis'), 'host' => env('redis1.host', '127.0.0.1'), 'port' => env('redis1.port', '6380'), 'password' => env('redis1.password', ''), 'select' => '0', // 全局緩存有效期(0為永久有效) 'expire' => 0, // 緩存前綴 'prefix' => '', 'timeout' => 0, ], ], ];
三,如何訪問多個redis數據源,看例子:
api/controller/UserController.php
<?php namespace app\api\controller; use think\facade\Cache; class UserController { public function index() { //訪問redis0 Cache::store('redis0')->set('name0','value1234',3600); $value0 = Cache::store('redis0')->get("name0"); //訪問redis1 Cache::store('redis1')->set('name1','value5678',3600); $value1 = Cache::store('redis1')->get("name1"); //返回 return '您好![userindex],<br/>redis0 value0:'.$value0."<br/>redis1 value1:".$value1; } public function test() { return "this is usercontroller test"; } }
四,測試效果
1,訪問:
http://127.0.0.1:81/api/userindex
返回:
2,從命令行查看redis中保存的數據:
root@ku:/data# /usr/local/soft/redis6/bin/redis-cli -p 6380 127.0.0.1:6380> get name1 "s:9:\"value5678\";" 127.0.0.1:6380> exit root@ku:/data# /usr/local/soft/redis6/bin/redis-cli -p 6379 127.0.0.1:6379> get name0 "s:9:\"value1234\";"
可以看到數據已保存到redis緩存中
五,查看thinkphp的版本
liuhongdi@ku:/data/php/mytp$ php think version v6.0.5
六,查看php的版本:
liuhongdi@ku:/data/logs/phplogs/tlog/202012$ php --version PHP 7.4.9 (cli) (built: Oct 26 2020 15:17:14) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.9, Copyright (c), by Zend Technologies