Package Data | |
---|---|
Maintainer Username: | Helldar |
Maintainer Contact: | helldar@dragon-code.pro (Andrey Helldar) |
Package Create Date: | 2018-02-01 |
Package Last Update: | 2024-09-17 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-14 15:03:57 |
Package Statistics | |
---|---|
Total Downloads: | 36,862 |
Monthly Downloads: | 1,397 |
Daily Downloads: | 47 |
Total Stars: | 67 |
Total Watchers: | 4 |
Total Forks: | 3 |
Total Open Issues: | 0 |
To get the latest version of Smart Cache
, simply require the project using Composer:
$ composer require dragon-code/laravel-cache
Or manually update require
block of composer.json
and run composer update
.
{
"require": {
"dragon-code/laravel-cache": "^3.0"
}
}
In addition to passing an explicit value, you can also pass objects and arrays to the keys
and tags
methods.
For example:
use DragonCode\Cache\Services\Cache;
use Tests\Fixtures\Dto\DtoObject;
use Tests\Fixtures\Simple\CustomObject;
$arr1 = ['foo', 'bar'];
$arr2 = new ArrayObject(['foo', 'bar']);
$arr3 = DtoObject::make(['foo' => 'Foo', 'bar'=> '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']);
Unpacking and processing of objects occurs as follows:
use DragonCode\Cache\Services\Cache;
use Tests\Fixtures\Dto\DtoObject;
use Tests\Fixtures\Simple\CustomObject;
['Foo', 'Bar'];
// as key: ['Foo', 'Bar']
// as tag: ['foo', 'bar']
new ArrayObject(['Foo', 'Bar']);
// as key: ['Foo', 'Bar']
// as tag: ['foo', 'bar']
DtoObject::make(['foo' => 'Foo', 'bar'=> 'Bar']);
// as key: ['Foo', 'Bar']
// as tag: ['foo', 'bar']
new CustomObject();
// as key: ['Foo']
// as tag: ['foo']
Since the main problem of working with the cache's key compilation, this package solves it.
By passing values to the keys
method, we get a ready-made key at the output.
For example:
use DragonCode\Cache\Services\Cache;
$cache = Cache::make()->key('foo', 'bar', [null, 'baz', 'baq']);
// Key is `acbd18db4cc2f85cedef654fccc4a4d8:37b51d194a7513e45b56f6524f2d51f2:73feffa4b7f6bb68e44cf984c85f6e88:b47951d522316fdd8811b23fc9c2f583`
This means that when writing to the cache, the tree view will be used.
For example:
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
In some cases, it is necessary to bind the cache to certain users. To do this, we have added the withAuth
helper.
use DragonCode\Cache\Services\Cache;
use Illuminate\Support\Facades\Auth;
// before
return Cache::make()->key(get_class(Auth::user()), Auth::id(), 'foo', 'bar');
// after
return Cache::make()->withAuth()->key('foo', 'bar');
When processing requests with a call to the withAuth method, the binding will be carried out not only by identifier, but also by reference to the model class, since a project can have several models with the possibility of authorization.
For example, App\Models\Employee
, App\Models\User
.
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->remember(static fn() => 'Some value');
// or
$cache->remember('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.
use DragonCode\Cache\Services\Cache;
use App\Models\User;
$user = User::first();
$cache = Cache::make()->key('foo');
$cache->put(static fn() => $user);
// or
$cache->put($user);
// Contains cached `$user`
$cache->remember(static fn() => $user);
// or
$cache->remember($user);
// Contains cached `$user`
$cache->get();
// Returns User model
$cache->has();
// Returns `true`
$cache->doesntHave();
// Returns `false`
$cache->forget();
// Will remove the key from the cache.
By default, the cache will be written for 1 day.
The cache will be written for the specified number of minutes, seconds or the DateTimeInterface
instance.
It does not matter in which direction the time shift will be. During processing, the value is converted to the abs()
.
use Carbon\Carbon;
use DateTime;
use DragonCode\Cache\Services\Cache;
use DragonCode\Cache\Support\Ttl;
$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'));
$cache = Cache::make()->ttl(Ttl::DAY);
$cache = Cache::make()->ttl(Ttl::WEEK);
$cache = Cache::make()->ttl(Ttl::MONTH);
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);
You can also store all TTL values in one place - in the config/cache.php
file.
To do this, add a ttl
block to the file and define
a TTL for the objects.
After that you can use the following construction:
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);
If the value is not found, the default value will be taken, which you can also override in the configuration file.
Starting with version 2.9.0
, we added the ability to dynamically specify TTLs in objects. To do this, you
need to implement the DragonCode\Contracts\Cache\Ttl
contract into your object and add a method that returns one of the following types of variables: DateTimeInterface
, Carbon\Carbon
, string
or integer
.
This method will allow you to dynamically specify the TTL depending on the code being executed.
For example:
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
For repositories that support tagging, the keys will be saved separated by tags.
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.
To retrieve a tagged cache item, pass the same ordered list of tags to the tags method and then call the get method with the key you wish to retrieve:
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`
See the official Laravel documentation.
Passing when = false
will not write to the cache.
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`
This package's licensed under the MIT License.