0

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?

1
  • Can you provide more info on that ? I don't understand your question I think. Commented Aug 12, 2013 at 19:22

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

session handler is configured in php.ini: session.save_handler = memcache session.save_path = "tcp://192.168.*.*:*" PHP uses php_memcache.dll (Win Server 2008,php 5.3) switch to PECL is good idea. thanks.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.