The laravel-cache package by Andrey Helldar provides a helper for working with cache in Laravel. Using the key()
method, you can generate a cache instance to get, set, update, and remove cache values:
1use DragonCode\Cache\Services\Cache;2 3// Default is one day; you can pass a custom TTL for cache duration with the ->ttl() method.4$cache = Cache::make()->key('foo', 'bar', ['baz', 'baq']);5 6$cache->put(static fn() => 'Some value');7$cache->get();8$cache->has();9$cache->forget();
This package also supports tagging for repositories that support it, which you can use by calling the tags()
method:
1use DragonCode\Cache\Services\Cache;2 3$cache = Cache::make()4 ->tags('actor', 'author')5 ->key('foo', 'bar', ['baz', 'baq']);6 7$cache->get();8$cache->has();9// etc.
You can retrieve tagged cache items using the same tags provided:
1use DragonCode\Cache\Services\Cache; 2 3$cache = Cache::make()->key('foo', 'bar'); 4 5// Contains cached `Some value` 6$cache->tags('actor', 'author')->put(static fn() => 'Some value'); 7 8// Returns cached `Some value` 9$cache->tags('actor', 'author')->get();10 11$cache->tags('actor')->get(); // returns `null`12$cache->tags('author')->get(); // returns `null`
You can learn more about this package, get full installation instructions, and view the source code on GitHub.
0 comments:
Post a Comment
Thanks