vagento/cacheable

安装: 3

依赖者: 0

建议者: 0

安全: 0

星标: 1

关注者: 1

分支: 0

开放问题: 0

类型:vagento-module

0.0.1 2021-12-11 16:34 UTC

This package is auto-updated.

Last update: 2024-09-12 03:46:10 UTC


README

Version License: MIT Twitter: WWoshid

此包为方法调用添加缓存功能。它需要Laravel框架。

安装

composer require vagento/cacheable

用法

特质:Cacheable - 方法缓存性

\Vagento\Cacheable\Traits\Cacheable 特质添加到任何需要将方法调用结果缓存的类。

use Vagento\Cacheable\Traits\Cacheable;

class MyClass
{
    use Cacheable; // Add this trait
    
    public function myMethod()
    {
        // Big computational task
        return $bigData;
    }
}

现在您可以像这样调用您的带有 'Cached' 后缀的方法

$myClass = new MyClass();

$result = $myClass->myMethodCached();

可以在方法调用之前传递可选参数(可以链式调用)

$myClass = new MyClass();

// Optional: Cache usage, will toggle caching on or off
// Default: true
$myClass->setCacheUsage(false);

// Optional: Laravel cache tags @see https://laravel.net.cn/docs/8.x/cache#cache-tags
// Default: null
$myClass->setCacheTags('my-method-tag');

// Optional: Time to live
// Default: null
$myClass->setTtl(60);
$myClass->setTtl(Carbon::now()->addMinutes(30)->toDateTime());

// Chaining
$result = $myClass->setCacheUsage(true)->setCacheTags(['method', 'something-else'])->setTtl(60)->myMethodCached();

为了支持IDE,您可以在类中添加如下的 @method 注解

/**
 * @method array myMethodCached()
 */
class MyClass
{
    use Cacheable;

    public function myMethod(): array
    {
        // Big computational task
        return $bigData;
    }
}

展示您的支持

如果这个项目对您有帮助,请给一个 ⭐️!

📝 许可证

版权所有 © 2021 Valentin Wotschel
本项目遵循 MIT 许可协议。