framework icon indicating copy to clipboard operation
framework copied to clipboard

Tagged caches don't fire cache events.

Open valorin opened this issue 11 years ago • 17 comments

Laravel v5.1

Tagged caches don't fire the cache events listed here: http://laravel.com/docs/5.1/cache#cache-events

Non-tagged caches use the Repository class: https://github.com/laravel/framework/blob/5.1/src/Illuminate/Cache/Repository.php#L95-L108

    public function get($key, $default = null)
    {
        $value = $this->store->get($key);
        if (is_null($value)) {
            $this->fireCacheEvent('missed', [$key]);
            $value = value($default);
        } else {
            $this->fireCacheEvent('hit', [$key, $value]);
        }
        return $value;
    }

Tagged caches extend from the TaggedCache class: https://github.com/laravel/framework/blob/5.1/src/Illuminate/Cache/TaggedCache.php#L57-L62

    public function get($key, $default = null)
    {
        $value = $this->store->get($this->taggedItemKey($key));
        return ! is_null($value) ? $value : value($default);
    }

I can have a go at adding it in and create a PR with these changes, if you'd like me to. I haven't yet as I saw https://github.com/laravel/framework/issues/9967 and figured that if it was being fixed then it this one can possibly be fixed at the same time. :-)

valorin avatar Sep 30 '15 07:09 valorin

Are you sure it's not fire all of them, the real write, read, delete happen in Repository class, both Tagged and no Tagged i believe.

ghost avatar Sep 30 '15 08:09 ghost

I've added listeners for all of the events, nothing fires. When I take out the ->tags(...) call, events fire. It is definitely not using the Repository class for a tagged cache.

valorin avatar Sep 30 '15 08:09 valorin

For reference: https://github.com/laravel/framework/blob/5.1/src/Illuminate/Cache/CacheManager.php All driver wrapped on Repository.

If you call tags method, it doesn't exists on Repository class, so it use method on implementation cache store. So, yes it will not fire the event, if you call tags(..)->get(..),

ghost avatar Sep 30 '15 08:09 ghost

Yep, which is a problem - the events should fire any time there is a cache event. The tagged cache should be no different.

valorin avatar Sep 30 '15 09:09 valorin

I believe it for consistency API for others non taggable, that's why it's not fire the events.

It will called if you call it get, or put.. So, I think its fine..

ghost avatar Sep 30 '15 09:09 ghost

I think you're missing the point. You can't use the normal cache methods if you want to use tagging. For the sake of consistency, the events need to be fired regardless if it's tagged or not. It's a bug, not a feature.

valorin avatar Sep 30 '15 10:09 valorin

Events DO fire. The taggable store MUST be wrapped in a repository before use.

GrahamCampbell avatar Sep 30 '15 15:09 GrahamCampbell

@GrahamCampbell Have you tried using tagged cache following the instructions in the documentation? The events do not fire. I am not doing anything special here. Laravel is returning the raw taggable store, not one wrapped in the repository.

Consider this code with the redis driver enabled:

Event::listen('cache.hit', function ($key, $value) {
    echo 'cache.hit'.PHP_EOL;
});
Event::listen('cache.missed', function ($key) {
    echo 'cache.missed'.PHP_EOL;
});
Event::listen('cache.write', function ($key, $value, $minutes) {
    echo 'cache.write'.PHP_EOL;
});
Event::listen('cache.delete', function ($key) {
    echo 'cache.delete'.PHP_EOL;
});

echo "Non-tagged cache usage: ".PHP_EOL;
Cache::get("one");
Cache::put("one", "two", 10);
Cache::get("one");
Cache::forget("one");

echo "Tagged cache usage: ".PHP_EOL;
Cache::tags(["tag"])->get("three");
Cache::tags(["tag"])->put("three", "four", 10);
Cache::tags(["tag"])->get("three");
Cache::tags(["tag"])->forget("three");

The output is:

Non-tagged cache usage: 
cache.missed
cache.write
cache.hit
cache.delete
Tagged cache usage:

What am I doing wrong here?

valorin avatar Sep 30 '15 20:09 valorin

Tagged cache dont fire the events, the events only fire if you call basic usage that also supported on others driver (non TaggableStore), like put, forget, etc

Consider what you params received on your listener, if the tagged cache also fire the events.. lets say, the listener:

Event::listen('cache.delete', function($key) {
  // this listener only insterested to $key == 'author';
    if ($author === 'author') {
       //do your inted
    }
});

then on other parts module you delete the author:

Cache::forget('author');

Okay is good, it fire the listener, and the listener receive the params they want, but what if the tagged cache also fire the event cache.delete, what key it should be? Is it okay?

Lets say you want to cache the author of a package, you tagged it, but you also want to cache the author of article

// this maybe store different data
Cache::forget('author');
Cache::tags(['package', 'laravel'])->forget('author');

Then here the problem, whats params is should be? only key to pass to listeners? include the tags?

ghost avatar Oct 01 '15 02:10 ghost

Yes, the tags should be passed into the events. I don't understand what the problem here is... @memtoko why are you so against firing events for tagged cache?

I believe @GrahamCampbell is simply unfamiliar with the functionality and doesn't realise it doesn't work as he expects it to, but I don't understand why you're here arguing about it so much.

valorin avatar Oct 01 '15 03:10 valorin

Hi @GrahamCampbell @taylorotwell , can you guys please review this issue, and either let me know what I'm doing wrong, or reopen the issue so it can be fixed?

I believe I am following the documentation pretty closely, and it isn't firing events for tagged cache functions - so it definitely looks like a bug.

Consider this example with the redis driver enabled:

Event::listen('cache.hit', function ($key, $value) {
    echo 'cache.hit'.PHP_EOL;
});
Event::listen('cache.missed', function ($key) {
    echo 'cache.missed'.PHP_EOL;
});
Event::listen('cache.write', function ($key, $value, $minutes) {
    echo 'cache.write'.PHP_EOL;
});
Event::listen('cache.delete', function ($key) {
    echo 'cache.delete'.PHP_EOL;
});

echo "Non-tagged cache usage: ".PHP_EOL;
Cache::get("one");
Cache::put("one", "two", 10);
Cache::get("one");
Cache::forget("one");

echo "Tagged cache usage: ".PHP_EOL;
Cache::tags(["tag"])->get("three");
Cache::tags(["tag"])->put("three", "four", 10);
Cache::tags(["tag"])->get("three");
Cache::tags(["tag"])->forget("three");

echo "Done".PHP_EOL;

The output is:

Non-tagged cache usage: 
cache.missed
cache.write
cache.hit
cache.delete
Tagged cache usage:
Done

As you can see, the tagged cache usage doesn't fire events while the non-tagged cache does. The expected behaviour is that the tagged cache fires the same events as the non-tagged cache - but that is no the case.

Graham's suggestion of "The taggable store MUST be wrapped in a repository before use." doesn't make sense, as the documentation shows you use taggable cache exactly as I have done above - with no extra manual setup steps to wrap the output of Cache::tags() somewhere.

valorin avatar Oct 03 '15 06:10 valorin

Perhaps it's a bug. If that's the case the issue should be left open.

On Sat, Oct 3, 2015 at 1:43 AM, Stephen Rees-Carter [email protected] wrote:

Hi @GrahamCampbell @taylorotwell , can you guys please review this issue, and either let me know what I'm doing wrong, or reopen the issue so it can be fixed? I believe I am following the documentation pretty closely, and it isn't firing events for tagged cache functions - so it definitely looks like a bug. Consider this example with the redis driver enabled:

Event::listen('cache.hit', function ($key, $value) {
    echo 'cache.hit'.PHP_EOL;
});
Event::listen('cache.missed', function ($key) {
    echo 'cache.missed'.PHP_EOL;
});
Event::listen('cache.write', function ($key, $value, $minutes) {
    echo 'cache.write'.PHP_EOL;
});
Event::listen('cache.delete', function ($key) {
    echo 'cache.delete'.PHP_EOL;
});
echo "Non-tagged cache usage: ".PHP_EOL;
Cache::get("one");
Cache::put("one", "two", 10);
Cache::get("one");
Cache::forget("one");
echo "Tagged cache usage: ".PHP_EOL;
Cache::tags(["tag"])->get("three");
Cache::tags(["tag"])->put("three", "four", 10);
Cache::tags(["tag"])->get("three");
Cache::tags(["tag"])->forget("three");
echo "Done".PHP_EOL;

The output is:

Non-tagged cache usage: 
cache.missed
cache.write
cache.hit
cache.delete
Tagged cache usage:
Done

As you can see, the tagged cache usage doesn't fire events while the non-tagged cache does. The expected behaviour is that the tagged cache fires the same events as the non-tagged cache - but that is no the case.

Graham's suggestion of "The taggable store MUST be wrapped in a repository before use." doesn't make sense, as the documentation shows you use taggable cache exactly as I have done above - with no extra manual setup steps to wrap the output of Cache::tags() somewhere.

Reply to this email directly or view it on GitHub: https://github.com/laravel/framework/issues/10423#issuecomment-145210237

taylorotwell avatar Oct 03 '15 13:10 taylorotwell

I'll investigate this later today to check what's going on here.

GrahamCampbell avatar Oct 03 '15 14:10 GrahamCampbell

Thank you :-)

valorin avatar Oct 03 '15 22:10 valorin

Whats the status on this? I'm running into the same issue as @valorin. Did you make any headway on this @GrahamCampbell?

bbrink68 avatar Dec 03 '15 17:12 bbrink68

I've had no time to look into this, and would appreciate it if anyone else could look into it.

GrahamCampbell avatar Dec 03 '15 17:12 GrahamCampbell

I'm running into this too, and would consider it a bug. I don't think I'll have time to even think about submitting a PR for at least a few weeks, but I can work around it. Just wanted to confirm that I'm seeing the same thing as the OP.

kieranajp avatar Dec 11 '15 11:12 kieranajp