Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It isn't possible to access an entry tagged with multiple tags by providing just one of this tags [Cache Tags] #25234

Closed
sgasser opened this issue Aug 16, 2018 · 3 comments

Comments

@sgasser
Copy link

sgasser commented Aug 16, 2018

  • Laravel Version: 5.6.29
  • PHP Version: 7.2

https://laravel.com/docs/5.6/cache#cache-tags

Description:

It is possible to invalidate all entries tagged with multiple tags by providing just one of this tags, but to access an entry you have to pass all tags in the exact order. It would be great if you could access all entries tagged with multiple tags by providing just one of this tags.


Example for my case:

I'm creating a package to cache eloquent queries.

Storing

List of authors
Cache::tags(['author-1', 'author-2', 'author-3', 'author-all'])->put('List'.$checksum, $authorList, $minutes);

Invalidate

Invalidate author (for example update author)
Cache::tags(['author-1'])->flush();
Invalidate list (for exampe I added a new author)
Cache::tags(['author-all'])->flush();

Accessing

To get the list I have to know all tags:
Cache::tags(['author-1', 'author-2', 'author-3', 'author-all'])->get('List'.$checksum);

Better solution

Cache::tags(['author-all'])->get('List'.$checksum);

@sgasser sgasser changed the title It is possible to remove a tag or list of tags with Cache Tags but not to access It is possible to remove all caches tagged by a tag or list of tags but not to access Aug 16, 2018
@sgasser sgasser changed the title It is possible to remove all caches tagged by a tag or list of tags but not to access It is possible to remove all cache items tagged by a tag or list of tags but not to access Aug 16, 2018
@sgasser sgasser changed the title It is possible to remove all cache items tagged by a tag or list of tags but not to access It isn't possible to access an entry tagged with multiple tags by providing just one of this tags [Cache Tags] Aug 16, 2018
@driesvints
Copy link
Member

This is unfortunately how it works. If you want to propose a different behavior it's best to propose an idea at https://github.com/laravel/ideas

@alexryall
Copy link

Looked into this as it would have been a useful feature for something I was trying to do. The way the cache currently works in Laravel is quite far from this working because in the key names it has a hash of all the tags combined.

Cache::tags(['author-1', 'author-all'])->put('List'.$checksum, $authorList, $minutes)
Cache::tags(['author-2', 'author-all'])->put('List'.$checksum, $authorList, $minutes)

So the above would create 2 different records, but we'd need this to only create 1 record for it be accessible when only having some of the tags. Possible with Redis but would be a pretty breaking change with how Laravel currently works.

@kolirt
Copy link

kolirt commented Aug 19, 2024

I faced the same problem and after spending a lot of time searching I found the problem

The problem is in this line

return sha1($this->tags->getNamespace()).':'.$key;

It leads to excessive duplication of data in the cache when you use multiple tags. If you return only $key, everything works as expected. After testing for a long time, I did not find any side effects

You can copy this modified file with the fix Overrides/TaggedCache.php or just install my kolirt/laravel-cacheable package if it's useful to you

And then you need to add the following code to composer.json and specify either your path to the new file or use the file from the package if you installed it

{
    "autoload": {
        "exclude-from-classmap": [
            "vendor/laravel/framework/src/Illuminate/Cache/TaggedCache.php"
        ],
        "files": [
            "vendor/kolirt/laravel-cacheable/src/Overrides/TaggedCache.php"
            // or your path
            "app/your-path/TaggedCache.php"
        ]
    }
}

After updating the composer (composer dump-autoload), the new file will replace the file from Laravel and everything will work as you expect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants