laravel / ideas

Issues board used for Laravel internals discussions.
938 stars 28 forks source link

Cache tagging should work on File & Database drivers #1068

Open CmdrSharp opened 6 years ago

CmdrSharp commented 6 years ago

Possible duplicate of #812, but I found this is worth another pass.

The problem

Currently, the tagging implementation works on Redis/Memcached and possibly other custom cache stores where tagging support comes natively. Whilst these are both preferred caching tools in a production environment, it breaks on most local environments where memcached and Redis aren't installed. This makes development that much more difficult, unless you write your own trait/helper/extension of the Cache implementation.

The solutions - for databases

  1. Create a separate CacheTags table with relations to the primary cache table. This is likely the most efficient method of dealing with these records.

  2. Simply store tags as a comma-delimited string in the cache table, and sort it when fetching. I haven't tested the approach, but I imagine relations would be faster out of a performance standpoint, not to mention cleaner.

The solution for files

It should be noted that tagging for files is so much more inefficient than other cache stores, that implementing it only makes sense for development. Documentation should, if any method of tagging files is implemented, clearly reflect that it's not a recommended approach.

File tagging is a bit more tricky. One option would be to create folders for each tag, and in the case of multiple tags, also make multiple copies of the cache file.

Another possible approach would be to store cache like we do now, but create a cache map file that maps each cache item into their respective tags.


If this is interesting, I can try to take a crack at making a PR for this. I do feel that it is a sound idea to attempt, where ever possible, to not limit the frameworks methods by the chosen implementation.

RicLeP commented 5 years ago

For anyone arriving here from Google take a look at this package: https://github.com/swayok/alternative-laravel-cache

dellow commented 3 years ago

Would also be interested in this. In my case, the database driver.

I'm using a couple of auxillary servers for various scheduling tasks and I need the onOneServer() command which requires all servers to use a central cache. On the face of it the database driver works fine for this but the rest of my application (on Vapor) uses ElastiCache with tagging so I immediately get tagging not supported errors.

Either that, or if the Cache facade could just ignore the tags method if it's not supported on the current driver, that would work too.