具有不同会话生存期的Laravel [英] Laravel with different session lifetimes

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

问题描述

问:如何在Laravel 5中创建具有不同生存期的会话?

Q: How to create sessions with different lifetimes in Laravel 5?

此问题不是此问题的重复项.我不想将其用于任何形式的登录或注册.我只是想将其存储5分钟,因为获取此数据的调用位于另一台服务器上,并且需要一段时间才能获取.

This question is not a duplicate of this question. I do not want to use it for any kind of sign in or register. I simply want to store it for 5mins because the call to fetch this data sits on a different server and takes a while to fetch.

问题是我有另一个会话需要存储更长的时间,并且当前正在使用该会话的全局配置:

The problem is that I have another session that needs to be stored for longer and currently using the global config for this session:

/*
|---------------------------------
| Session Lifetime
|---------------------------------
*/

'lifetime' => 30,
'expire_on_close' => true,

如何给他们不同的一生?谢谢!

How can I give them different lifetimes? Thanks!

推荐答案

对于您的特殊情况,应改用缓存.我通常会这样做:

For your particular case you should use the cache instead. I generally do:

$value = Cache::remember('key', now()->addMinutes(5), function () { 
    /* get and return the value if it's not in the cache */ 
});

remember是一种便捷的方法,可以从缓存中获取值或运行回调,将结果放入缓存中并返回结果(这意味着下一个调用将直接到达缓存).

remember is a convenience method to either get the value from cache or run the callback, put the result in the cache and return the result (meaning the next call will just hit the cache).

更多信息可以在文档中的检索并删除;商店

More information can be found in the documentation under Retrieve & Store

这篇关于具有不同会话生存期的Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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