简体   繁体   中英

PHP Memcached Session Locking Enable

I use "memcached" to store php sessions. It is important, that request must be synchronously (to avoid duplicate transactions or operations), but while using "memcached" session, "session locking" not works.

is some method to lock "memcached" session until one request will executed?

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.

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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