railken/cacheable

v0.2.0 2024-03-27 18:50 UTC

This package is auto-updated.

Last update: 2024-09-27 19:50:01 UTC


README

Actions Status

这个库为您提供了调用任何以 Cached 后缀的方法的能力,以检索方法的缓存结果。当您有一个耗时很长的方法,并且给定相同的参数,结果总是相同的时候,这非常有用。

要求

  • PHP 8.1 及以上
  • Laravel

安装

您可以通过输入以下命令通过 Composer 安装它:

composer require railken/cacheable

用法

CacheableTraitCacheableContract 添加到您想要缓存的类中

use Railken\Cacheable\CacheableTrait;
use Railken\Cacheable\CacheableContract;

class Foo implements CacheableContract
{
  use CacheableTrait;

  public function sum(int $x, int $y): int
  {
      return $x + $y;
  }

  public function random(): string
  {
      return str_random(10);
  }
}

现在您可以玩转这个方法了

$foo = new Foo();
$foo->sumCached(2, 8); // 10

$foo->randomCached(); // Return always the same string

为了清理缓存,只需运行 php artisan cache:clean