Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

4jZW7jVSdS4U6PC's avatar

Redis cache best practice

Hello everyone.

I'm using Redis as caching system and it works great for most of the part, I've a little question tho

I've an index() method which contains this code

        $companies = Cache::rememberForever('auth.companies', function () {
            return Company::with([
                'tags',
                'emails'
            ])->get();
        });

This should store the companies inside the key auth.companies, and it works fine. The doubt appears when I try to edit a specific company in update() method like so

$company = Company::whereId($id)->firstOrFail();
$company->update($request->all());
Cache::forget('auth.companies');

My questions are

  • When I try to update a specific entity should i work with the cached object or retrieve it from the Database? (which is NOT redis atm)
  • If I should work with the cached object how can I persist the data inside the Database?

EDIT: typos

0 likes
0 replies

Please or to participate in this conversation.