Laravel用户特定的缓存 [英] Laravel user specific caching

查看:237
本文介绍了Laravel用户特定的缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有做过很多缓存工作,但是现在尝试尝试一下.我有一个返回大量数据的仪表板,并且为了减轻负载,我在缓存数据,如下所示:

I've never done much with caching, but am trying to play around with it a bit now. I have a dashboard that returns a lot of data, and to make the load a bit lighter, I am caching data like so:

return cache()->rememberForever('something', function () {
    return auth()->user()->something()->get();
});

其中某物"只是一个相关模型.创建新记录时,在控制器storeupdate方法中,我只是这样做:

Where "something" is just a related model. When creating a new record, in the controller store and update methods I just do this:

cache()->forget('something');

这一切都完美无瑕.但是,当我与另一个用户登录时,显然前一个用户的所有缓存数据都显示在仪表板上.

This all works flawlessly. But when I login with another user, all cached data from the previous user is obviously being displayed on the dashboard.

是否有一种简单的方法来简单地为每个用户缓存数据?

Is there an easy way to simply cache data per user?

推荐答案

您可以执行以下操作为每个用户分别存储用户对象:

You could do something like this to store user object for each user separately:

return cache()->rememberForever('something' . auth()->id(), function () {
    return auth()->user()->something()->get();
});

要获取经过身份验证的用户的数据,请执行以下操作:

To get the data for an authenticated user:

 cache('something' . auth()->id());

这篇关于Laravel用户特定的缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆