webman:用illuminate/redis访问redis(v1.5.7)

发布时间 2023-09-22 11:24:44作者: 刘宏缔的架构森林

一,官方文档:

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

二,安装库:

1,从命令行安装

liuhongdi@lhdpc:/data/webman/imageadmin$ composer require -W illuminate/redis illuminate/events

2,查看所安装的版本

liuhongdi@lhdpc:/data/webman/imageadmin$ composer show illuminate/redis
name     : illuminate/redis
descrip. : The Illuminate Redis package.
keywords :
versions : * v10.23.1
...

3,配置redis:

config/redis.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
27
28
<?php
/**
 * This file is part of webman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author    walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link      http://www.workerman.net/
 * @license   http://www.opensource.org/licenses/mit-license.php MIT License
 */
 
return [
    'default' => [
        'host' => '127.0.0.1',
        'password' => null,
        'port' => 6379,
        'database' => 0,
    ],
    'cache1' => [
        'host'     => '127.0.0.1',
        'password' => null,
        'port'     => 6379,
        'database' => 0,
    ],
];

三,php代码:

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
27
28
29
30
<?php
namespace app\controller;
 
use support\Request;
use app\lib\result\Result;
use support\Log;
use think\facade\Db;
use support\Redis;
 
use app\model\Comment as CommentModel;
 
class ImageController
{
 
    /**
     * 测试redis
     */
    public function captcha(Request $request){
        
        $url = 'https://wx3.sinaimg.cn/mw690/7f8d08b2gy1hh0qy7n57qj213y0u0wj2.jpg';
        //指定连接
        $redis1 = Redis::connection('cache1');
        //保存到缓存
        $redis1->set('url', $url,'ex',600);
        //从缓存中读取
        $content = $redis1->get('url');
        //返回
        $data = ["url"=>$content];
        return Result::Success($data);
    }

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

四,测试效果

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

五,查看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
...