generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
239 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace EriBloo\CacheObjects\Concerns; | ||
|
||
use EriBloo\CacheObjects\Contracts\CacheObjectDriver; | ||
|
||
/** | ||
* @template TValue | ||
*/ | ||
trait CacheObjectActions | ||
{ | ||
/** | ||
* @param TValue $value | ||
*/ | ||
public function store(mixed $value): bool | ||
{ | ||
/** @var CacheObjectDriver $driver */ | ||
$driver = app() | ||
->make(CacheObjectDriver::class); | ||
|
||
return $driver->set($value, $this); | ||
} | ||
|
||
/** | ||
* @return TValue|null | ||
*/ | ||
public function retrieve(): mixed | ||
{ | ||
/** @var CacheObjectDriver $driver */ | ||
$driver = app() | ||
->make(CacheObjectDriver::class); | ||
|
||
return $driver->get($this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace EriBloo\CacheObjects\Contracts; | ||
|
||
use Carbon\CarbonInterval; | ||
|
||
/** | ||
* @template TValue | ||
*/ | ||
interface CacheObject | ||
{ | ||
public function key(): string; | ||
|
||
public function ttl(): CarbonInterval; | ||
|
||
/** | ||
* @param TValue $value | ||
*/ | ||
public function store(mixed $value): bool; | ||
|
||
/** | ||
* @return TValue | ||
*/ | ||
public function retrieve(): mixed; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace EriBloo\CacheObjects\Contracts; | ||
|
||
interface CacheObjectDriver | ||
{ | ||
/** | ||
* @template TValue | ||
* | ||
* @param TValue $value | ||
* @param CacheObject<TValue> $cacheObject | ||
*/ | ||
public function set(mixed $value, CacheObject $cacheObject): bool; | ||
|
||
/** | ||
* @template TValue | ||
* | ||
* @param CacheObject<TValue> $cacheObject | ||
* @return TValue|null | ||
*/ | ||
public function get(CacheObject $cacheObject): mixed; | ||
|
||
/** | ||
* @template TValue | ||
* | ||
* @param CacheObject<TValue> $cacheObject | ||
*/ | ||
public function delete(CacheObject $cacheObject): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace EriBloo\CacheObjects\Drivers; | ||
|
||
use EriBloo\CacheObjects\Contracts\CacheObject; | ||
use EriBloo\CacheObjects\Contracts\CacheObjectDriver; | ||
use Illuminate\Contracts\Cache\Store; | ||
|
||
final class LaravelDriver implements CacheObjectDriver | ||
{ | ||
public function __construct( | ||
private readonly Store $repository, | ||
) {} | ||
|
||
public function set(mixed $value, CacheObject $cacheObject): bool | ||
{ | ||
$key = $this->prepareKey($cacheObject); | ||
$value = $this->prepareValue($value); | ||
$ttl = $this->prepareTtl($cacheObject); | ||
|
||
if ($ttl <= 0) { | ||
return $this->repository->forever($key, $value); | ||
} | ||
|
||
return $this->repository->put($key, $value, $ttl); | ||
} | ||
|
||
public function get(CacheObject $cacheObject): mixed | ||
{ | ||
$key = $this->prepareKey($cacheObject); | ||
$value = $this->repository->get($key); | ||
|
||
if ($value === null) { | ||
return null; | ||
} | ||
|
||
return $this->prepareReturn($value); | ||
} | ||
|
||
public function delete(CacheObject $cacheObject): bool | ||
{ | ||
return $this->repository->forget($this->prepareKey($cacheObject)); | ||
} | ||
|
||
/** | ||
* @template TValue | ||
* | ||
* @param CacheObject<TValue> $cacheObject | ||
*/ | ||
private function prepareKey(CacheObject $cacheObject): string | ||
{ | ||
return $cacheObject->key(); | ||
} | ||
|
||
private function prepareValue(mixed $value): string | ||
{ | ||
return serialize($value); | ||
} | ||
|
||
private function prepareReturn(string $value): mixed | ||
{ | ||
return unserialize($value); | ||
} | ||
|
||
/** | ||
* @template TValue | ||
* | ||
* @param CacheObject<TValue> $cacheObject | ||
*/ | ||
private function prepareTtl(CacheObject $cacheObject): int | ||
{ | ||
return (int) $cacheObject->ttl() | ||
->totalSeconds; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use EriBloo\CacheObjects\Tests\Fixtures\BasicCacheObject; | ||
|
||
use function PHPUnit\Framework\assertEquals; | ||
|
||
it('stores basic cache object', function () { | ||
// prepare | ||
$obj = new BasicCacheObject('key'); | ||
|
||
// execute | ||
$obj->store('test'); | ||
|
||
// assert | ||
assertEquals('test', $obj->retrieve()); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace EriBloo\CacheObjects\Tests\Fixtures; | ||
|
||
use Carbon\CarbonInterval; | ||
use EriBloo\CacheObjects\Concerns\CacheObjectActions; | ||
use EriBloo\CacheObjects\Contracts\CacheObject; | ||
|
||
/** | ||
* @implements CacheObject<string> | ||
*/ | ||
final readonly class BasicCacheObject implements CacheObject | ||
{ | ||
/** @use CacheObjectActions<string> */ | ||
use CacheObjectActions; | ||
|
||
public function __construct( | ||
public string $value, | ||
) {} | ||
|
||
public function key(): string | ||
{ | ||
return "basic-cache-object:{$this->value}"; | ||
} | ||
|
||
public function ttl(): CarbonInterval | ||
{ | ||
return CarbonInterval::seconds(0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters