Skip to content

Commit

Permalink
feat(Cache): pre-operation events #1156
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk authored Dec 28, 2024
2 parents 99da899 + 6d3d6af commit 8b0beb3
Show file tree
Hide file tree
Showing 12 changed files with 270 additions and 43 deletions.
49 changes: 33 additions & 16 deletions src/Cache/src/CacheRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
use Psr\SimpleCache\CacheInterface;
use Spiral\Cache\Event\CacheHit;
use Spiral\Cache\Event\CacheMissed;
use Spiral\Cache\Event\CacheRetrieving;
use Spiral\Cache\Event\KeyDeleted;
use Spiral\Cache\Event\KeyDeleteFailed;
use Spiral\Cache\Event\KeyDeleting;
use Spiral\Cache\Event\KeyWriteFailed;
use Spiral\Cache\Event\KeyWriting;
use Spiral\Cache\Event\KeyWritten;

/**
Expand All @@ -25,37 +30,53 @@ public function __construct(

public function get(string $key, mixed $default = null): mixed
{
$value = $this->storage->get($this->resolveKey($key));
$key = $this->resolveKey($key);

$this->dispatcher?->dispatch(new CacheRetrieving($key));

$value = $this->storage->get($key);

if ($value === null) {
$this->dispatcher?->dispatch(new CacheMissed($this->resolveKey($key)));
$this->dispatcher?->dispatch(new CacheMissed($key));

return $default;
}

$this->dispatcher?->dispatch(new CacheHit($this->resolveKey($key), $value));
$this->dispatcher?->dispatch(new CacheHit($key, $value));

return $value;
}

public function set(string $key, mixed $value, \DateInterval|int|null $ttl = null): bool
{
$result = $this->storage->set($this->resolveKey($key), $value, $ttl);
$key = $this->resolveKey($key);

if ($result) {
$this->dispatcher?->dispatch(new KeyWritten($this->resolveKey($key), $value));
}
$this->dispatcher?->dispatch(new KeyWriting($key, $value));

$result = $this->storage->set($key, $value, $ttl);

$this->dispatcher?->dispatch(
$result
? new KeyWritten($key, $value)
: new KeyWriteFailed($key, $value),
);

return $result;
}

public function delete(string $key): bool
{
$result = $this->storage->delete($this->resolveKey($key));
$key = $this->resolveKey($key);

if ($result) {
$this->dispatcher?->dispatch(new KeyDeleted($this->resolveKey($key)));
}
$this->dispatcher?->dispatch(new KeyDeleting($key));

$result = $this->storage->delete($key);

$this->dispatcher?->dispatch(
$result
? new KeyDeleted($key)
: new KeyDeleteFailed($key),
);

return $result;
}
Expand Down Expand Up @@ -111,10 +132,6 @@ public function getStorage(): CacheInterface

private function resolveKey(string $key): string
{
if (!empty($this->prefix)) {
return $this->prefix . $key;
}

return $key;
return $this->prefix . $key;
}
}
16 changes: 16 additions & 0 deletions src/Cache/src/Event/CacheEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Spiral\Cache\Event;

/**
* Base class for all cache events.
*/
abstract class CacheEvent
{
public function __construct(
public readonly string $key,
) {
}
}
8 changes: 6 additions & 2 deletions src/Cache/src/Event/CacheHit.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

namespace Spiral\Cache\Event;

final class CacheHit
/**
* Triggered when cache item is successfully retrieved.
*/
final class CacheHit extends CacheEvent
{
public function __construct(
public readonly string $key,
string $key,
public readonly mixed $value
) {
parent::__construct($key);
}
}
9 changes: 4 additions & 5 deletions src/Cache/src/Event/CacheMissed.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

namespace Spiral\Cache\Event;

final class CacheMissed
/**
* Triggered when cache item is not found.
*/
final class CacheMissed extends CacheEvent
{
public function __construct(
public readonly string $key,
) {
}
}
12 changes: 12 additions & 0 deletions src/Cache/src/Event/CacheRetrieving.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Spiral\Cache\Event;

/**
* Triggered before cache item is retrieved.
*/
final class CacheRetrieving extends CacheEvent
{
}
12 changes: 12 additions & 0 deletions src/Cache/src/Event/KeyDeleteFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Spiral\Cache\Event;

/**
* Triggered when cache delete operation failed.
*/
final class KeyDeleteFailed extends CacheEvent
{
}
9 changes: 4 additions & 5 deletions src/Cache/src/Event/KeyDeleted.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

namespace Spiral\Cache\Event;

final class KeyDeleted
/**
* Triggered when cache item is deleted.
*/
final class KeyDeleted extends CacheEvent
{
public function __construct(
public readonly string $key,
) {
}
}
12 changes: 12 additions & 0 deletions src/Cache/src/Event/KeyDeleting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Spiral\Cache\Event;

/**
* Triggered before cache item is deleted.
*/
final class KeyDeleting extends CacheEvent
{
}
18 changes: 18 additions & 0 deletions src/Cache/src/Event/KeyWriteFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Spiral\Cache\Event;

/**
* Triggered when cache write operation failed.
*/
final class KeyWriteFailed extends CacheEvent
{
public function __construct(
string $key,
public readonly mixed $value,
) {
parent::__construct($key);
}
}
18 changes: 18 additions & 0 deletions src/Cache/src/Event/KeyWriting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Spiral\Cache\Event;

/**
* Triggered before cache item is written.
*/
final class KeyWriting extends CacheEvent
{
public function __construct(
string $key,
public readonly mixed $value,
) {
parent::__construct($key);
}
}
8 changes: 6 additions & 2 deletions src/Cache/src/Event/KeyWritten.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

namespace Spiral\Cache\Event;

final class KeyWritten
/**
* Triggered after cache item is written.
*/
final class KeyWritten extends CacheEvent
{
public function __construct(
public readonly string $key,
string $key,
public readonly mixed $value,
) {
parent::__construct($key);
}
}
Loading

0 comments on commit 8b0beb3

Please sign in to comment.