Laravel with different session lifetimes

白昼怎懂夜的黑 提交于 2019-12-11 02:12:29

问题


Q: How to create sessions with different lifetimes in Laravel 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!


回答1:


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

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

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



来源:https://stackoverflow.com/questions/46091713/laravel-with-different-session-lifetimes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!