Cache

在webman默认使用 symfony/cache作为cache组件。

使用symfony/cache之前必须先给php-cli安装redis扩展。

安装

php 7.x

composer require -W illuminate/redis ^8.2.0 symfony/cache ^5.2

php 8.x

composer require -W illuminate/redis symfony/cache

安装后需要restart重启(reload无效)

Redis配置

redis配置文件在config/redis.php

return [
    'default' => [
        'host'     => '127.0.0.1',
        'password' => null,
        'port'     => 6379,
        'database' => 0,
    ]
];

示例

<?php
namespace app\controller;

use support\Request;
use support\Cache;

class UserController
{
    public function db(Request $request)
    {
        $key = 'test_key';
        Cache::set($key, rand());
        return response(Cache::get($key));
    }
}

注意
key尽量加一个前缀,避免与其它使用redis的业务冲突。
symfony/cache 的key不允许包含字符"{}()/\@:"

使用其它Cache组件

ThinkCache组件使用参考 其它数据库

编辑于2024-03-26 15:45:08 完善本页 +发起讨论
赞助商
QQ交流群 865805921