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

cache for individual users #35

Closed
omidMolaverdi opened this issue Jul 8, 2020 · 1 comment
Closed

cache for individual users #35

omidMolaverdi opened this issue Jul 8, 2020 · 1 comment

Comments

@omidMolaverdi
Copy link

I have a table in the database that assigned a model to. This table has a user_id and I want to cache the queries for the user individually. In other words, I need users to have their own specific cache so that I do not remove a global cache when invalidate. I did a prefix ->cachePrefix('m'.$user_id.'_') but do not know how to invalidate on update/insert/delete. Please can you help?

@omidMolaverdi omidMolaverdi changed the title cache for a user cache for individual users Jul 8, 2020
@rennokki
Copy link
Member

rennokki commented Jul 8, 2020

Prefixes are in case you have multiple applications on the same cache driver.

I assume you might want to use caching tags. You can have something like this in your controller, for example:

public function index(Request $request)
{
    $user = $request->user();

    $posts = $user->posts()->cacheTags(["user:{$user->id}"])->get();

    return response()->json($posts);
}

To invalidate it, just run:

User::flushQueryCache(["user:{$user->id}"]);

@rennokki rennokki closed this as completed Jul 8, 2020
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

2 participants