andrey-helldar / cache
v2.11.1
2022-02-01 08:59 UTC
Requires
- php: ^7.3 || ^8.0
- dragon-code/contracts: ^2.16
- dragon-code/support: ^5.6
- illuminate/support: ^6.0 || ^7.0 || ^8.0 || ^9.0
Requires (Dev)
- dragon-code/simple-data-transfer-object: ^2.1
- orchestra/testbench: ^4.0 || ^5.0 || ^6.0 || ^7.0
- phpunit/phpunit: ^8.5 || ^9.0
Conflicts
README
安装
要获取最新版本的 Laravel Cache
,只需使用 Composer 包含项目。
$ composer require dragon-code/laravel-cache
或者手动更新 composer.json
中的 require
块并运行 composer update
。
{ "require": { "dragon-code/laravel-cache": "^2.0" } }
使用
键和标签
除了传递显式值外,您还可以将对象和数组传递给 keys
和 tags
方法。
例如
use DragonCode\Cache\Services\Cache; use DragonCode\SimpleDataTransferObject\DataTransferObject; use Tests\Fixtures\Simple\CustomObject; $arr1 = ['foo', 'bar']; $arr2 = new ArrayObject(['foo', 'bar']); $arr3 = DataTransferObject::make(['foo', 'bar']); $arr4 = new CustomObject(); Cache::make()->key($arr1)->tags($arr1); Cache::make()->key($arr2)->tags($arr3); Cache::make()->key($arr2)->tags($arr3); Cache::make()->key($arr4)->tags($arr4); Cache::make()->key([$arr1, $arr2, $arr3, $arr4, 'foo', 'bar'])->tags([$arr1, $arr2, $arr3, $arr4, 'foo', 'bar']); Cache::make()->key([$arr1, $arr2, $arr3, $arr4, 'foo', 'bar'])->tags([$arr1, $arr2, $arr3, $arr4, 'foo', 'bar']); Cache::make()->key([$arr1, $arr2, $arr3, $arr4, 'foo', 'bar'])->tags([$arr1, $arr2, $arr3, $arr4, 'foo', 'bar']);
键处理
由于缓存键编译的主要问题,此包解决了它。
通过向 keys
方法传递值,我们在输出中得到一个现成的键。
例如
use DragonCode\Cache\Services\Cache; $cache = Cache::make()->key('foo', 'bar', [null, 'baz', 'baq']); // Key is `acbd18db4cc2f85cedef654fccc4a4d8:37b51d194a7513e45b56f6524f2d51f2:73feffa4b7f6bb68e44cf984c85f6e88:b47951d522316fdd8811b23fc9c2f583`
这意味着在写入缓存时,将使用树视图。
例如
use DragonCode\Cache\Services\Cache; Cache::make()->key('foo', 'foo')->put('foo'); Cache::make()->key('foo', 'bar')->put('bar'); Cache::make()->key('baz')->put('baz'); // acbd18db4cc2f85cedef654fccc4a4d8: // acbd18db4cc2f85cedef654fccc4a4d8: foo // 37b51d194a7513e45b56f6524f2d51f2: bar // 73feffa4b7f6bb68e44cf984c85f6e88: baz
启用时
基本
默认情况下,缓存将保留1天。
use DragonCode\Cache\Services\Cache; $cache = Cache::make()->key('foo', 'bar', ['baz', 'baq']); $cache->put(static fn() => 'Some value'); // or $cache->put('Some value'); // Contains cached `Some value` $cache->get(); // Returns cached `Some value` $cache->has(); // Returns `true` $cache->doesntHave(); // Returns `false` $cache->forget(); // Will remove the key from the cache.
自定义 TTL
缓存将保留指定的分钟数、秒数或 DateTimeInterface
实例。
时间移动的方向无关紧要。在处理过程中,值将转换为 abs()
。
按分钟
use Carbon\Carbon; use DateTime; use DragonCode\Cache\Services\Cache; $cache = Cache::make()->ttl(10); $cache = Cache::make()->ttl('10'); $cache = Cache::make()->ttl(fn () => 10); $cache = Cache::make()->ttl(Carbon::now()->addDay()); $cache = Cache::make()->ttl(new DateTime('tomorrow'));
按秒
use Carbon\Carbon; use DateTime; use DragonCode\Cache\Services\Cache; $cache = Cache::make()->ttl(10, false); $cache = Cache::make()->ttl('10', false); $cache = Cache::make()->ttl(fn () => 10, false); $cache = Cache::make()->ttl(Carbon::now()->addDay(), false); $cache = Cache::make()->ttl(new DateTime('tomorrow'), false);
通过对象和自定义字符串
您还可以将所有 TTL 值存储在一个地方——在 config/cache.php
文件中。
为此,向文件添加一个 ttl
块并为对象定义 TTL。
然后您可以使用以下结构
use DragonCode\Cache\Services\Cache; use Tests\Fixtures\Simple\CustomObject; $cache = Cache::make()->ttl(CustomObject::class); $cache = Cache::make()->ttl(new CustomObject()); $cache = Cache::make()->ttl('custom_key'); // You can also specify that these values are in seconds, not minutes: $cache = Cache::make()->ttl(CustomObject::class, false); $cache = Cache::make()->ttl(new CustomObject(), false); $cache = Cache::make()->ttl('custom_key', false);
如果找不到值,将采用 默认值,您也可以在 配置文件 中覆盖它。
使用合约
从版本 2.9.0
开始,我们添加了在对象中动态指定 TTL 的功能。为此,您需要将 DragonCode\Contracts\Cache\Ttl
合约实现到您的对象中,并添加一个返回以下类型变量之一的方 法:DateTimeInterface
、Carbon\Carbon
、string
或 integer
。
此方法将允许您根据正在执行的代码动态指定TTL(生存时间)。
例如
use DragonCode\Cache\Services\Cache; use DragonCode\Contracts\Cache\Ttl; class Foo implements Ttl { protected $value; public function __construct(string $value) { $this->value = $value; } public function cacheTtl(): int { return $this->value === 'foo' ? 123 : 456; } } $cache = Cache::make()->ttl(new Foo('foo')); // TTL is 7380 seconds $cache = Cache::make()->ttl(new Foo('bar')); // TTL is 27360 seconds $cache = Cache::make()->ttl(new Foo('foo'), false); // TTL is 123 seconds $cache = Cache::make()->ttl(new Foo('bar'), false); // TTL is 456 seconds
标记
对于支持标记的仓库,键将按标记分隔保存。
use DragonCode\Cache\Services\Cache; $cache = Cache::make() ->tags('actor', 'author') ->key('foo', 'bar', ['baz', 'baq']); $cache->put(static fn() => 'Some value'); // or $cache->put('Some value'); // Contains cached `Some value` $cache->get(); // Returns cached `Some value` $cache->has(); // Returns `true` $cache->doesntHave(); // Returns `false` $cache->forget(); // Will remove the key from the cache.
要检索标记的缓存项,请将相同的有序标记列表传递给标记方法,然后使用要检索的键调用get方法。
use DragonCode\Cache\Services\Cache; $cache = Cache::make()->key('foo', 'bar'); $cache->tags('actor', 'author')->put(static fn() => 'Some value'); // or $cache->tags('actor', 'author')->put('Some value'); // Contains cached `Some value` $cache->tags('actor', 'author')->get(); // Returns cached `Some value` $cache->tags('actor')->get(); // Returns `null` $cache->tags('author')->get(); // Returns `null`
请参阅官方Laravel 文档。
当禁用时
传递when = false
将不会写入缓存。
use DragonCode\Cache\Services\Cache; $cache = Cache::make() ->when(false) ->key('foo', 'bar'); $value = $cache->put(static fn() => 'Some value'); // or $value = $cache->put('Some value'); // Returns `Some value` $cache->get(); // Returns `null` $cache->has(); // Returns `false` $cache->doesntHave(); // Returns `true`
许可
本软件包遵循MIT许可协议。