diff --git a/src/Drivers/CacheDriver.php b/src/Drivers/CacheDriver.php index eb359cb..62a5e4c 100644 --- a/src/Drivers/CacheDriver.php +++ b/src/Drivers/CacheDriver.php @@ -6,6 +6,10 @@ use EriBloo\CacheObjects\Contracts\CacheObject; use EriBloo\CacheObjects\Contracts\CacheObjectDriver; +use EriBloo\CacheObjects\Events\CacheObjectDeleted; +use EriBloo\CacheObjects\Events\CacheObjectMissed; +use EriBloo\CacheObjects\Events\CacheObjectRetrieved; +use EriBloo\CacheObjects\Events\CacheObjectStored; use Illuminate\Contracts\Cache\Store; final class CacheDriver implements CacheObjectDriver @@ -17,17 +21,19 @@ public function __construct( public function set(mixed $value, CacheObject $cacheObject): string { $key = (string) $cacheObject->key(); - $value = $cacheObject->transformer() + $transformed = $cacheObject->transformer() ->onSave($value); $ttl = (int) $cacheObject->ttl() ->total('seconds'); if ($ttl <= 0) { - $this->store->forever($key, $value); + $this->store->forever($key, $transformed); } else { - $this->store->put($key, $value, $ttl); + $this->store->put($key, $transformed, $ttl); } + event(new CacheObjectStored($cacheObject, $value, $transformed)); + return $key; } @@ -37,15 +43,27 @@ public function get(CacheObject $cacheObject): mixed $value = $this->store->get($key); if ($value === null) { + event(new CacheObjectMissed($cacheObject)); + return null; } - return $cacheObject->transformer() + $transformed = $cacheObject->transformer() ->onLoad($value); + + event(new CacheObjectRetrieved($cacheObject, $value, $transformed)); + + return $transformed; } public function delete(CacheObject $cacheObject): bool { - return $this->store->forget((string) $cacheObject->key()); + $result = $this->store->forget((string) $cacheObject->key()); + + if ($result === true) { + event(new CacheObjectDeleted($cacheObject)); + } + + return $result; } } diff --git a/src/Events/CacheObjectDeleted.php b/src/Events/CacheObjectDeleted.php new file mode 100644 index 0000000..cb8286a --- /dev/null +++ b/src/Events/CacheObjectDeleted.php @@ -0,0 +1,22 @@ + $cacheObject + */ + public function __construct( + public CacheObject $cacheObject, + ) {} +} diff --git a/src/Events/CacheObjectMissed.php b/src/Events/CacheObjectMissed.php new file mode 100644 index 0000000..edaab05 --- /dev/null +++ b/src/Events/CacheObjectMissed.php @@ -0,0 +1,22 @@ + $cacheObject + */ + public function __construct( + public CacheObject $cacheObject, + ) {} +} diff --git a/src/Events/CacheObjectRetrieved.php b/src/Events/CacheObjectRetrieved.php new file mode 100644 index 0000000..9a375c7 --- /dev/null +++ b/src/Events/CacheObjectRetrieved.php @@ -0,0 +1,25 @@ + $cacheObject + * @param TValue $transformedValue + */ + public function __construct( + public CacheObject $cacheObject, + public string $originalValue, + public mixed $transformedValue, + ) {} +} diff --git a/src/Events/CacheObjectStored.php b/src/Events/CacheObjectStored.php new file mode 100644 index 0000000..61b7a64 --- /dev/null +++ b/src/Events/CacheObjectStored.php @@ -0,0 +1,25 @@ + $cacheObject + * @param TValue $originalValue + */ + public function __construct( + public CacheObject $cacheObject, + public mixed $originalValue, + public string $transformedValue, + ) {} +} diff --git a/tests/EventTest.php b/tests/EventTest.php new file mode 100644 index 0000000..1ebb5e2 --- /dev/null +++ b/tests/EventTest.php @@ -0,0 +1,60 @@ +store('test'); + + // assert + Event::assertDispatched(CacheObjectStored::class); +}); + +it('dispatches restored event', function () { + // prepare + Event::fake(); + $obj = new BasicCacheObject('test'); + $obj->store('test'); + + // execute + $obj->retrieve(); + + // assert + Event::assertDispatched(CacheObjectRetrieved::class); +}); + +it('dispatches missed event', function () { + // prepare + Event::fake(); + $obj = new BasicCacheObject('test'); + + // execute + $obj->retrieve(); + + // assert + Event::assertDispatched(CacheObjectMissed::class); +}); + +it('dispatches deleted event', function () { + // prepare + Event::fake(); + $obj = new BasicCacheObject('test'); + $obj->store('test'); + + // execute + $obj->delete(); + + // assert + Event::assertDispatched(CacheObjectDeleted::class); +});