vagento / cacheable
0.0.1
2021-12-11 16:34 UTC
Requires
- php: ^8.0
- laravel/framework: ^v8.65
README
此包为方法调用添加缓存功能。它需要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 许可协议。