1 year ago

Question about caching and best practices

Posted 1 year ago by nexxai

So I've finally put on my big boy pants and have decided to look into caching as one method to help increase performance of my application, but I'm having a hard time wrapping my head around the best way to go about it.

I know that physically it's simple: enable the Redis cache driver, add

use Illuminate\Support\Facades\Cache;

to the top of my controller and presto finito, we're caching. What I don't understand is how and when I should be caching.

My app is something vaguely similar to a wiki of sorts, and so there's no schedule in which data can be assumed to be static for. With that being the case, I know that I can't just do:

$sprockets = Cache::remember('sprockets', 60, function () {
    return Sprocket::all();

because what if someone updates a Data object within those 60 seconds; they're gonna be fed old data (right?). And because the site is very much based on user contributions, I don't want someone to make an update only to go back to the main index view and not see their changes.

So I feel like I'm missing a step, like one where after every update, not only is the database table updated, but also several keys in the cache (e.g. not only the individual model record, but also the overall index). And if that's the case, can someone point me in the direction of some information about how and when it's best to do that?

I feel like I've got this massive hole in my understanding here but I want - nay, need - to figure it out, if for no other reason than my own sanity.


Please sign in or create an account to participate in this conversation.