note 82277 added to function.Memcache-add
| From: | php at tapirpirates dot net | Date: | Thu, 03 Apr 2008 16:32:19 +0000 |
| Subject: | note 82277 added to function.Memcache-add | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-137899@lists.php.net to get a copy of this message | ||
memcache has no locking mechanism, but you could implement it manually.
basic locking through the add method:
<?php
// locks time out after 5 seconds
Define( 'LOCK_TIMEOUT', 5 );
$lock = $memcache->add( 'lock:' . $key, 1, false, LOCK_TIMEOUT );
if ( $lock ) {
// no lock on this key, so do what you want
$value = $memcache->get( $key );
$memcache->set( $key, $value+1 );
// release lock
$memcache->delete( 'lock:' . $key );
}
else {
// variable is currently locked, so do something else
}
?>
furthermore, you could implement a loop which checks if there is a lock, and if there is, wait some
time and try again, until the lock is free.
remember: locking will heavily increase your memcache hits and obviously is not what memcache is
made for. altough it's not possible for a lock to be forgotten (there's a timeout after
all) there is the possibility to get locked out for a very long time.
an alternative may be to implement locking through apc_add (or shared memory), but i've never
tried it.
if you absolutley have to implement locks, memcached is probably the wrong solution anyway.
----
Server IP: 81.223.48.75
Probable Submitter: 213.129.231.122
----
Manual Page -- http://www.php.net/manual/en/function.Memcache-add.php
Edit -- https://master.php.net/note/edit/82277
Del: integrated -- https://master.php.net/note/delete/82277/integrated
Del: useless -- https://master.php.net/note/delete/82277/useless
Del: bad code -- https://master.php.net/note/delete/82277/bad+code
Del: spam -- https://master.php.net/note/delete/82277/spam
Del: non-english -- https://master.php.net/note/delete/82277/non-english
Del: in docs -- https://master.php.net/note/delete/82277/in+docs
Del: other reasons-- https://master.php.net/note/delete/82277
Reject -- https://master.php.net/note/reject/82277
Search -- https://master.php.net/manage/user-notes.php