简体   繁体   English

PHP Memcached会话锁定启用

[英]PHP Memcached Session Locking Enable

I use "memcached" to store php sessions. 我使用“memcached”来存储php会话。 It is important, that request must be synchronously (to avoid duplicate transactions or operations), but while using "memcached" session, "session locking" not works. 重要的是,该请求必须是同步的(以避免重复的事务或操作),但在使用“memcached”会话时,“会话锁定”不起作用。

is some method to lock "memcached" session until one request will executed? 是一种锁定“memcached”会话直到执行一个请求的方法?

There's nothing built in no, but you can write stuff yourself to make your code atomic. 没有内置的东西,但你可以自己写东西来使你的代码成为原子。

$key = 'lockable_key_name';
$lockkey = $key.'##LOCK';

if($memcached->add($lockkey, '', 60)) {
    $storedvalue = $memcached->get($key);

    // do something with $storedvalue
    $memcached->set($key, $newvalue);

    // release
    $memcached->delete($lockkey);
}

In your code you could check for the lock by doing: 在您的代码中,您可以通过执行以下操作来检查锁定:

if(!$memcached->get($lockkey)) {
    // then do something
}

If the get method returns false then there's no lock, or the operation has hung and passed the 60 second timeout specified in the add call above. 如果get方法返回false,则表示没有锁定,或者操作已挂起并超过上面add调用中指定的60秒超时。

Since you were asking for credible/official sources: 既然你要求可信/官方消息来源:

The memcached extension supports session locking since version 3.0.4, according to the changelog document on the PECL extension page: http://pecl.php.net/package-info.php?package=memcache&version=3.0.4 根据PECL扩展页面上的changelog文档,memcached扩展支持3.0.4版以来的会话锁定: http ://pecl.php.net/package-info.php?package = memcache&version = 3.0.4

If you happen to run an earlier version (it means that your version of the memcached extension is more than 4 years old), you are out of luck and should upgrade. 如果您碰巧运行的是早期版本(这意味着您的memcached扩展版本的版本已超过4年),那么您运气不佳并且应该进行升级。

当你完成$(field_name)_is_locked = false ,尝试使用$(field_name)_is_locked = true类的东西,并在更新时将变量传递给服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM