laravel:访问redis(10.27.0)
一,相关文档:
https://learnku.com/docs/laravel/10.x/redis/14887
二,php代码
1,配置.env
使用默认的设置:
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
2,controller中引用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\App; use App\extend\result\Result; use Illuminate\Support\Facades\Redis; class NewsController extends Controller { //访问redis public function cache(Request $request ) { //设置值 Redis::set( 'name' , '老刘是头牛' ); //指定300秒过期 Redis::expire( 'name' , 300); //读取 $name = Redis::get( 'name' ); //返回 $data = [ 'name' => $name ]; return Result::Success( $data ); } |
3,从redis客户端查看:
liuhongdi@lhdpc:/data/laravel/dignews$ redis-cli
127.0.0.1:6379> keys *
1) "a"
2) "laravel_database_name"
127.0.0.1:6379> get laravel_database_name
"\xe8\x80\x81\xe5\x88\x98\xe6\x98\xaf\xe5\xa4\xb4\xe7\x89\x9b"
127.0.0.1:6379> ttl laravel_database_name
(integer) 294
三,测试效果
说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/10/18/laravel-fang-wen-redis-10-27/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com
四,查看laravel的版本:
liuhongdi@lhdpc:/data/laravel/dignews$ php artisan --version
Laravel Framework 10.27.0