webman:用thinkcache访问redis(v1.5.7)

发布时间 2023-08-23 09:53:03作者: 刘宏缔的架构森林

一,官方文档地址:

https://www.workerman.net/doc/webman/db/thinkcache.html

二,安装组件

liuhongdi@lhdpc:/data/webman/imageadmin$ composer require -W webman/think-cache

三,配置redis

config/thinkcache.php,按自己的实际情况配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
return [
    'default' => 'file',
    'stores' => [
        'file' => [
            'type' => 'File',
            // 缓存保存目录
            'path' => runtime_path() . '/cache/',
            // 缓存前缀
            'prefix' => '',
            // 缓存有效期 0表示永久缓存
            'expire' => 0,
        ],
        'redis' => [
            'type' => 'redis',
            'host' => '127.0.0.1',
            'port' => 6379,
            'prefix' => '',
            'expire' => 0,
        ],
    ],
];

四,代码:

app/controller/ImageController.php

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
26
<?php
namespace app\controller;
 
use support\Request;
use app\result\Result;
use support\Log;
use think\facade\Db;
use think\facade\Cache;
 
use app\model\Comment as CommentModel;
 
class ImageController
{
     
    //图片的详情
    public function detail(Request $request)
    {
         $url = 'https://wx3.sinaimg.cn/mw690/7f8d08b2gy1hh0qy7n57qj213y0u0wj2.jpg';
        //set
        Cache::store('redis')->set('url',$url,3600);
        //get
        $content = Cache::store('redis')->get("url");
        //返回
        $data = ["url"=>$content];
        return Result::Success($data);
    }

说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/08/19/webman-yong-thinkcache-fang-wen-redis/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com

五,测试效果

1,查看输出

2,从客户端查看效果:

liuhongdi@lhdpc:/data/webman/imageadmin$ redis-cli
127.0.0.1:6379> get url
"s:65:\"https://wx3.sinaimg.cn/mw690/7f8d08b2gy1hh0qy7n57qj213y0u0wj2.jpg\";"
127.0.0.1:6379> ttl url
(integer) 3504

六,查看webman的版本:

liuhongdi@lhdpc:/data/webman/imageadmin$ composer show workerman/webman-framework
name     : workerman/webman-framework
descrip. : High performance HTTP Service Framework.
keywords : High Performance, http service
versions : * v1.5.7
...