
KeyedMutex - An interprocess keyed mutex

# start server
% keyedmutexd >/dev/null &
use KeyedMutex;
my $km = KeyedMutex->new;
until ($value = $cache->get($key)) {
if (my $lock = $km->lock($key, 1)) {
#locked read from DB
$value = get_from_db($key);
$cache->set($key, $value);
last;
}
}

KeyedMutex is an interprocess keyed mutex. Its intended use is to prevent sending identical requests to database servers at the same time. By using KeyedMutex, only a single client would send a request to the database, and others can retrieve the result from a shared cache (namely memcached or Cache::Swifty) instead.

Following parameters are recognized.
Optional. Path to a unix domain socket or a tcp port on which keyedmutexd is running. Defaults to /tmp/keyedmutexd.sock.
Optional. Whether or not to automatically reconnect to server on communication failure. Default is on.

Tries to obtain a mutex lock for given key. When the use_raii flag is not set (or omitted), the method would return 1 if successful, or undef if not. If successful, the client should later on release the lock by calling release. A return value undef means some other client that held the lock has released it. When the use_raii flag is being set, the method would return a KeyedMutex::Lock object when successful. The lock would be automatically released when the lock object is being destroyed.
Releases the lock acquired by a procedural-style lock (i.e. use_raii flag not being set).
Returns if the object is currently holding a lock.
Sets or retrieves auto_reconnect flag.

http://labs.cybozu.co.jp/blog/kazuhoatwork/

Copyright (c) 2007 Cybozu Labs, Inc. All rights reserved.
written by Kazuho Oku <kazuhooku@gmail.com>

This program is free software; you can redistribute it and/or modify it under th e same terms as Perl itself.