Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the ability to use a TTL contract #28

Merged
merged 2 commits into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,42 @@ $cache = Cache::make()->ttlBy('custom_key', false);

If the value is not found, the [default value](config/cache.php) will be taken, which you can also override in the [configuration file](config/cache.php).

##### With Contract

Starting with version [`2.9.0`](https://github.com/TheDragonCode/laravel-cache/releases/tag/v2.9.0), we added the ability to dynamically specify TTLs in objects. To do this, you
need to implement the Foo 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:

```php
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()->ttlBy(new Foo('foo'));
// TTL is 123

$cache = Cache::make()->ttlBy(new Foo('bar'));
// TTL is 456
```

#### Tagged

For repositories that support tagging, the keys will be saved separated by tags.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"require": {
"php": "^7.3 || ^8.0",
"dragon-code/contracts": "^2.11",
"dragon-code/contracts": "^2.13",
"dragon-code/support": "^5.0",
"illuminate/support": "^6.0 || ^7.0 || ^8.0"
},
Expand Down
19 changes: 18 additions & 1 deletion src/Support/TtlBy.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace DragonCode\Cache\Support;

use DragonCode\Cache\Facades\Support\Ttl as TtlSupport;
use DragonCode\Contracts\Cache\Ttl as TtlContract;
use DragonCode\Support\Facades\Helpers\Instance;
use DragonCode\Support\Facades\Helpers\Is;

class TtlBy
Expand All @@ -13,14 +15,24 @@ class TtlBy

public function get($value, bool $is_minutes = true): int
{
if ($this->isObject($value) && $this->isContract($value)) {
return $this->correct($value->cacheTtl(), $is_minutes);
}

$value = $this->resolve($value);

$ttl = $this->ttl($value) ?: $this->ttlDefault();

return $this->correct($ttl, $is_minutes);
}

protected function correct(int $value, bool $is_minutes): int
/**
* @param \DateTimeInterface|string|int|callable $value
* @param bool $is_minutes
*
* @return int
*/
protected function correct($value, bool $is_minutes): int
{
return $is_minutes
? TtlSupport::fromMinutes($value)
Expand All @@ -46,4 +58,9 @@ protected function isObject($value): bool
{
return Is::object($value);
}

protected function isContract($value): bool
{
return Instance::of($value, TtlContract::class);
}
}
25 changes: 25 additions & 0 deletions tests/Fixtures/Ttl/AsCarbon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Ttl;

use Carbon\Carbon;
use DragonCode\Contracts\Cache\Ttl;

class AsCarbon implements Ttl
{
protected $value;

public function __construct(string $value)
{
$this->value = $value;
}

public function cacheTtl(): Carbon
{
return $this->value === 'foo'
? Carbon::now()->addHour()
: Carbon::now()->addHours(2);
}
}
26 changes: 26 additions & 0 deletions tests/Fixtures/Ttl/AsDateTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Ttl;

use DateTime;
use DateTimeInterface;
use DragonCode\Contracts\Cache\Ttl;

class AsDateTime implements Ttl
{
protected $value;

public function __construct(string $value)
{
$this->value = $value;
}

public function cacheTtl(): DateTimeInterface
{
return $this->value === 'foo'
? new DateTime('+1 hour')
: new DateTime('+2 hour');
}
}
22 changes: 22 additions & 0 deletions tests/Fixtures/Ttl/AsInteger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Ttl;

use DragonCode\Contracts\Cache\Ttl;

class AsInteger implements Ttl
{
protected $value;

public function __construct(string $value)
{
$this->value = $value;
}

public function cacheTtl(): int
{
return $this->value === 'foo' ? 10 : 20;
}
}
22 changes: 22 additions & 0 deletions tests/Fixtures/Ttl/AsString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Ttl;

use DragonCode\Contracts\Cache\Ttl;

class AsString implements Ttl
{
protected $value;

public function __construct(string $value)
{
$this->value = $value;
}

public function cacheTtl(): string
{
return $this->value === 'foo' ? '10' : '20';
}
}
34 changes: 34 additions & 0 deletions tests/Support/TtlByTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
use Tests\Fixtures\Simple\CustomObject;
use Tests\Fixtures\Simple\DragonCodeArrayable;
use Tests\Fixtures\Simple\IlluminateArrayable;
use Tests\Fixtures\Ttl\AsCarbon;
use Tests\Fixtures\Ttl\AsDateTime;
use Tests\Fixtures\Ttl\AsInteger;
use Tests\Fixtures\Ttl\AsString;
use Tests\TestCase;

class TtlByTest extends TestCase
Expand Down Expand Up @@ -47,4 +51,34 @@ public function testObjectAsSeconds()
$this->assertSame(400, TtlBy::get(new DragonCodeArrayable(), false));
$this->assertSame(3600, TtlBy::get(new IlluminateArrayable(), false));
}

public function testContractAsMinutes()
{
$this->assertSame(3600, TtlBy::get(new AsCarbon('foo')));
$this->assertSame(7200, TtlBy::get(new AsCarbon('bar')));

$this->assertSame(3600, TtlBy::get(new AsDateTime('foo')));
$this->assertSame(7200, TtlBy::get(new AsDateTime('bar')));

$this->assertSame(600, TtlBy::get(new AsInteger('foo')));
$this->assertSame(1200, TtlBy::get(new AsInteger('bar')));

$this->assertSame(600, TtlBy::get(new AsString('foo')));
$this->assertSame(1200, TtlBy::get(new AsString('bar')));
}

public function testContractAsSeconds()
{
$this->assertSame(3600, TtlBy::get(new AsCarbon('foo'), false));
$this->assertSame(7200, TtlBy::get(new AsCarbon('bar'), false));

$this->assertSame(3600, TtlBy::get(new AsDateTime('foo'), false));
$this->assertSame(7200, TtlBy::get(new AsDateTime('bar'), false));

$this->assertSame(10, TtlBy::get(new AsInteger('foo'), false));
$this->assertSame(20, TtlBy::get(new AsInteger('bar'), false));

$this->assertSame(10, TtlBy::get(new AsString('foo'), false));
$this->assertSame(20, TtlBy::get(new AsString('bar'), false));
}
}