NAME

KeyedMutex - An interprocess keyed mutex

SYNOPSIS

% keyedmutexd >/dev/null &

use KeyedMutex;

my $km = KeyedMutex->new;

until ($value = $cache->get($key)) {
  if ($km->lock($key)) {
    # locked, read from DB
    $value = get_from_db($key);
    $cache->set($key, $value);
    $km->release;
    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.

METHODS

lock($key)

Tries to obtain a mutex lock for given key. 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.

release

Releases the lock.

locked

Returns if the object is currently holding a lock.

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