[英]Laravel4 - Sessions are disappearing on simultaneous requests
I have a kind of shopping basket and I am keeping the content as separate sessions for various reasons. 我有一个购物篮,出于各种原因,我将内容保留为单独的会话。
The problem is, when I send simultaneous requests to the following add function, Laravel's loosing previous, unrelated/different sessions and keeping the last one but it's working fine if I wait for the previous process to finish. 问题是,当我将同时的请求发送到以下add函数时,Laravel失去了先前的,不相关的/不同的会话并保留了最后一个会话,但是如果我等待上一个过程完成,它就可以正常工作。
I think the problem is Laravel's session management method, it tries to keep everything in a single file or single field in the database. 我认为问题是Laravel的会话管理方法,它试图将所有内容保存在数据库的单个文件或单个字段中。 If so, I don't see any solution for this but here's the code;
如果是这样,我看不到任何解决方案,但这是代码;
public function add($param1, $param2, $param3, $param4){
sleep(2);
$results = DB::select("CALL ...(?, ?, ?, ?)", array($param1, $param2, $param3, $param4));
$sessionName = "basket-item-$param1-$param2-$param3-$param4";
$selectionIDs = array();
$status = null;
if(!Session::has($sessionName)){
if($results){
foreach($results as $result){
$selectionIDs[] = $result->selection_id;
}
}
if($selectionIDs) Session::put($sessionName, $selectionIDs);
$status = 'new';
}
return array('count' => $this->count(), "has_session_this" => Session::has($sessionName), 'session_name' => $sessionName, 'status' => $status);
}
Count is item count
, has_session_this
is the response of Session::has($key)
and the session_name
is the $key
Count是项目
count
, has_session_this
是Session::has($key)
的响应, session_name
是$key
ADD TO BASKET RESPONSE : Object { count=1, has_session_this=true, session_name="basket-item-1-256-7894628-21494", more...}
ADD TO BASKET RESPONSE : Object { count=2, has_session_this=true, session_name="basket-item-1-256-14834686-21494", more...}
ADD TO BASKET RESPONSE : Object { count=3, has_session_this=true, session_name="basket-item-1-256-21132688-21494", more...}
ADD TO BASKET RESPONSE : Object { count=4, has_session_this=true, session_name="basket-item-1-256-3500057-21494", more...}
That's the expected result and if I don't wait for the response; 那是预期的结果,如果我不等待回应的话;
ADD TO BASKET RESPONSE : Object { count=1, has_session_this=true, session_name="basket-item-1-256-7894628-21494", more...}
ADD TO BASKET RESPONSE : Object { count=1, has_session_this=true, session_name="basket-item-1-256-14834686-21494", more...}
ADD TO BASKET RESPONSE : Object { count=1, has_session_this=true, session_name="basket-item-1-256-21132688-21494", more...}
ADD TO BASKET RESPONSE : Object { count=1, has_session_this=true, session_name="basket-item-1-256-3500057-21494", more...}
Any help, any idea is appreciated. 任何帮助,任何想法表示赞赏。
Looks like a problem with your logic. 看来您的逻辑有问题。 Has nothing to do with sessions.
与会话无关。
If the previous functions have not completed - then the count start at 1. Only if/when the previous function is complete would the count increase. 如果先前的功能尚未完成-则计数从1开始。只有/当先前的功能完成时,计数才会增加。
But you are using $this->count()
- so it looks like it might be getting the count on the current session object when you started the function - not the updated count. 但是您使用的是
$this->count()
-因此,当您启动该函数时,它似乎正在获取当前会话对象的计数-而不是更新的计数。
If you want to ensure a correct count increment - you need to use 如果要确保正确的计数增量-您需要使用
Cache::increment('key')
And your cache driver needs to be Memcache or Redis for it to work. 并且您的缓存驱动程序必须是Memcache或Redis,它才能正常工作。 See docs for more info .
有关更多信息,请参阅文档 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.