Skip to content

Commit

Permalink
refactor how cache events are fired
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jan 1, 2017
1 parent f3aaf57 commit b7454f0
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 92 deletions.
19 changes: 19 additions & 0 deletions src/Illuminate/Cache/Events/CacheEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Illuminate\Cache\Events;

class CacheEvent
{
/**
* Set the tags for the cache event.
*
* @param array $tags
* @return $this
*/
public function setTags($tags)
{
$this->tags = $tags;

return $this;
}
}
2 changes: 1 addition & 1 deletion src/Illuminate/Cache/Events/CacheHit.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Illuminate\Cache\Events;

class CacheHit
class CacheHit extends CacheEvent
{
/**
* The key that was hit.
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Cache/Events/CacheMissed.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Illuminate\Cache\Events;

class CacheMissed
class CacheMissed extends CacheEvent
{
/**
* The key that was missed.
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Cache/Events/KeyForgotten.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Illuminate\Cache\Events;

class KeyForgotten
class KeyForgotten extends CacheEvent
{
/**
* The key that was forgotten.
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Cache/Events/KeyWritten.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Illuminate\Cache\Events;

class KeyWritten
class KeyWritten extends CacheEvent
{
/**
* The key that was written.
Expand Down
122 changes: 44 additions & 78 deletions src/Illuminate/Cache/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
use ArrayAccess;
use Carbon\Carbon;
use BadMethodCallException;
use Illuminate\Cache\Events\CacheHit;
use Illuminate\Contracts\Cache\Store;
use Illuminate\Cache\Events\KeyWritten;
use Illuminate\Cache\Events\CacheMissed;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Cache\Events\KeyForgotten;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Cache\Repository as CacheContract;

Expand Down Expand Up @@ -50,58 +54,6 @@ public function __construct(Store $store)
$this->store = $store;
}

/**
* Set the event dispatcher instance.
*
* @param \Illuminate\Contracts\Events\Dispatcher $events
* @return void
*/
public function setEventDispatcher(Dispatcher $events)
{
$this->events = $events;
}

/**
* Fire an event for this cache instance.
*
* @param string $event
* @param array $payload
* @return void
*/
protected function fireCacheEvent($event, $payload)
{
if (! isset($this->events)) {
return;
}

switch ($event) {
case 'hit':
if (count($payload) == 2) {
$payload[] = [];
}

return $this->events->fire(new Events\CacheHit($payload[0], $payload[1], $payload[2]));
case 'missed':
if (count($payload) == 1) {
$payload[] = [];
}

return $this->events->fire(new Events\CacheMissed($payload[0], $payload[1]));
case 'delete':
if (count($payload) == 1) {
$payload[] = [];
}

return $this->events->fire(new Events\KeyForgotten($payload[0], $payload[1]));
case 'write':
if (count($payload) == 3) {
$payload[] = [];
}

return $this->events->fire(new Events\KeyWritten($payload[0], $payload[1], $payload[2], $payload[3]));
}
}

/**
* Determine if an item exists in the cache.
*
Expand Down Expand Up @@ -131,11 +83,11 @@ public function get($key, $default = null)
$value = $this->store->get($this->itemKey($key));

if (is_null($value) || $value === false) {
$this->fireCacheEvent('missed', [$key]);
$this->event(new CacheMissed($key));

$value = value($default);
} else {
$this->fireCacheEvent('hit', [$key, $value]);
$this->event(new CacheHit($key, $value));
}

return $value;
Expand All @@ -161,11 +113,11 @@ public function many(array $keys)

foreach ($values as $key => &$value) {
if (is_null($value) || $value === false) {
$this->fireCacheEvent('missed', [$key]);
$this->event(new CacheMissed($key));

$value = isset($keys[$key]) ? value($keys[$key]) : null;
} else {
$this->fireCacheEvent('hit', [$key, $value]);
$this->event(new CacheHit($key, $value));
}
}

Expand All @@ -181,11 +133,9 @@ public function many(array $keys)
*/
public function pull($key, $default = null)
{
$value = $this->get($key, $default);

$this->forget($key);

return $value;
return tap($this->get($key, $default), function ($value) use ($key) {
$this->forget($key);
});
}

/**
Expand All @@ -202,12 +152,10 @@ public function put($key, $value, $minutes = null)
return $this->putMany($key, $value);
}

$minutes = $this->getMinutes($minutes);

if (! is_null($minutes)) {
if (! is_null($minutes = $this->getMinutes($minutes))) {
$this->store->put($this->itemKey($key), $value, $minutes);

$this->fireCacheEvent('write', [$key, $value, $minutes]);
$this->event(new KeyWritten($key, $value, $minutes));
}
}

Expand All @@ -220,13 +168,11 @@ public function put($key, $value, $minutes = null)
*/
public function putMany(array $values, $minutes)
{
$minutes = $this->getMinutes($minutes);

if (! is_null($minutes)) {
if (! is_null($minutes = $this->getMinutes($minutes))) {
$this->store->putMany($values, $minutes);

foreach ($values as $key => $value) {
$this->fireCacheEvent('write', [$key, $value, $minutes]);
$this->event(new KeyWritten($key, $value, $minutes));
}
}
}
Expand All @@ -241,9 +187,7 @@ public function putMany(array $values, $minutes)
*/
public function add($key, $value, $minutes)
{
$minutes = $this->getMinutes($minutes);

if (is_null($minutes)) {
if (is_null($minutes = $this->getMinutes($minutes))) {
return false;
}

Expand Down Expand Up @@ -297,7 +241,7 @@ public function forever($key, $value)
{
$this->store->forever($this->itemKey($key), $value);

$this->fireCacheEvent('write', [$key, $value, 0]);
$this->event(new KeyWritten($key, $value, 0));
}

/**
Expand Down Expand Up @@ -367,11 +311,9 @@ public function rememberForever($key, Closure $callback)
*/
public function forget($key)
{
$success = $this->store->forget($this->itemKey($key));

$this->fireCacheEvent('delete', [$key]);

return $success;
return tap($this->store->forget($this->itemKey($key)), function () use ($key) {
$this->event(new KeyForgotten($key));
});
}

/**
Expand Down Expand Up @@ -441,6 +383,30 @@ public function getStore()
return $this->store;
}

/**
* Fire an event for this cache instance.
*
* @param string $event
* @return void
*/
protected function event($event)
{
if (isset($this->events)) {
$this->events->fire($event);
}
}

/**
* Set the event dispatcher instance.
*
* @param \Illuminate\Contracts\Events\Dispatcher $events
* @return void
*/
public function setEventDispatcher(Dispatcher $events)
{
$this->events = $events;
}

/**
* Determine if a cached value exists.
*
Expand Down
21 changes: 11 additions & 10 deletions src/Illuminate/Cache/TaggedCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ public function __construct(Store $store, TagSet $tags)
$this->tags = $tags;
}

/**
* {@inheritdoc}
*/
protected function fireCacheEvent($event, $payload)
{
$payload[] = $this->tags->getNames();

parent::fireCacheEvent($event, $payload);
}

/**
* Increment the value of an item in the cache.
*
Expand Down Expand Up @@ -91,4 +81,15 @@ public function taggedItemKey($key)
{
return sha1($this->tags->getNamespace()).':'.$key;
}

/**
* Fire an event for this cache instance.
*
* @param string $event
* @return void
*/
protected function event($event)
{
parent::event($event->setTags($this->tags->getNames()));
}
}

0 comments on commit b7454f0

Please sign in to comment.