NAME
KeyedMutex - An interprocess keyed mutex
SYNOPSIS
# 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;
}
}
DESCRIPTION
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.
THE CONSTRUCTOR
Following parameters are recognized.
sock
Optional. Path to a unix domain socket or a tcp port on which keyedmutexd
is running. Defaults to /tmp/keyedmutexd.sock.
auto_reconnect
Optional. Whether or not to automatically reconnect to server on communication failure. Default is on.
METHODS
lock($key, [ use_raii ])
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.
release
Releases the lock acquired by a procedural-style lock (i.e. use_raii flag not being set).
locked
Returns if the object is currently holding a lock.
auto_reconnect
Sets or retrieves auto_reconnect flag.
SEE ALSO
http://labs.cybozu.co.jp/blog/kazuhoatwork/
AUTHOR
Copyright (c) 2007 Cybozu Labs, Inc. All rights reserved.
written by Kazuho Oku <kazuhooku@gmail.com>
LICENSE
This program is free software; you can redistribute it and/or modify it under th e same terms as Perl itself.
See http://www.perl.com/perl/misc/Artistic.html