gpt4 book ai didi

laravel - 使用 Redis 在 Laravel 中缓存

转载 作者:可可西里 更新时间:2023-11-01 11:30:14 26 4
gpt4 key购买 nike

我在 Laravel 中构建应用程序,其中我使用 Redis 进行缓存。成功存储在缓存中但问题是,我在一个浏览器中运行应用程序并登录成功并存储缓存,现在在另一个浏览器中它会自动登录。

我使用代码存储和加载缓存

if(Cache::has('mykey'))
{
return Cache::get('mykey');
}
else
{
// Do some operation and store it in cache
Cache::put('mykey',content,10);
// and then return
}

请告诉我哪里出了问题...提前致谢

最佳答案

试试下面的代码:

// Check cache first
$catchPage = Cache::get('mykey');
if ($catchPage != null) {
return $catchPage;
}
else {
Cache::tags('tag_name')->put('mykey',$content,10);
// and then return
}

引用自 here .

根据 laravel 5.3 Docs :
您可以通过 store 方法访问各种缓存存储。

传递给 store 方法的键应该对应于缓存配置文件中 stores 配置数组中列出的商店之一:

$value = Cache::store('file')->get('foo');

Cache::store('redis')->put('bar', 'baz', 10);

如果您需要从缓存中检索一个项目,然后删除该项目,您可以使用 pull 方法。与 get 方法一样,如果该项目不存在于缓存中,将返回 null:

$value = Cache::pull('key');

add 方法只会将项目添加到缓存中,前提是它不存在于缓存存储区中。如果项目实际添加到缓存中,该方法将返回 true。否则,该方法将返回 false:

Cache::add('key', 'value', $minutes);

关于laravel - 使用 Redis 在 Laravel 中缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41566770/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com