user492589
user492589

Reputation: 571

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?

Upvotes: 0

Views: 2229

Answers (3)

Sven
Sven

Reputation: 70893

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.

Upvotes: 1

Garry Welding
Garry Welding

Reputation: 3609

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.

Upvotes: 1

Joshua G
Joshua G

Reputation: 2136

Maybe try something like $(field_name)_is_locked = true when you start then when you are done $(field_name)_is_locked = false and pass the variable to the server when you update it.

Upvotes: 0

Related Questions