Cache

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

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

安装

php 7.x

  1. composer require psr/container ^1.1.1 illuminate/redis ^8.2.0 symfony/cache ^5.2

php 8.x

  1. composer require psr/container ^1.1.1 illuminate/redis symfony/cache

Redis配置

redis配置文件在config/redis.php

  1. return [
  2. 'default' => [
  3. 'host' => '127.0.0.1',
  4. 'password' => null,
  5. 'port' => 6379,
  6. 'database' => 0,
  7. ]
  8. ];

示例

  1. <?php
  2. namespace app\controller;
  3. use support\Request;
  4. use support\Cache;
  5. class User
  6. {
  7. public function db(Request $request)
  8. {
  9. $key = 'test_key';
  10. Cache::set($key, rand());
  11. return response(Cache::get($key));
  12. }
  13. }

注意 key尽量加一个前缀,避免与其它使用redis的业务冲突

使用其它Cache组件

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