KeyedMutex::Memcached - An interprocess keyed mutex using memcached
use KeyedMutex::Memcached; my $key = 'query:XXXXXX'; my $cache = Cache::Memcached::Fast->new( ... ); my $mutex = KeyedMutex::Memcached->new( cache => $cache ); until ( my $value = $cache->get($key) ) { { if ( my $lock = $mutex->lock( $key, 1 ) ) { #locked read from DB $value = get_from_db($key); $cache->set($key, $value); last; } }; }
KeyedMutex::Memcached is an interprocess keyed mutex using memcached. This module is inspired by KeyedMutex.
Following parameters are recognized.
Required. Cache::Memcached::Fast object or similar interface object.
Optional. The seconds for busy loop interval. Defaults to 0.01 seconds.
Optional. When the value is being set zero, lock() method will be waiting until lock becomes released. When the value is being set positive integer value, lock() method will be stopped on reached trial count. Defaults to 0.
Optional. The seconds until lock becomes released. Defaults to 30 seconds.
Optional. Prefix of key to store memcached. The real key is prefix + ':' + key. Defaults to 'km'.
'km'
Get lock by each key. When getting lock successfully, returns 1, on failed returns 0. If use_raii is being set true, return Scope::Guard object as RAII.
Which is the object has locked.
Release lock.
Toru Yamaguchi <zigorou@cpan.org>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install KeyedMutex::Memcached, copy and paste the appropriate command in to your terminal.
cpanm
cpanm KeyedMutex::Memcached
CPAN shell
perl -MCPAN -e shell install KeyedMutex::Memcached
For more information on module installation, please visit the detailed CPAN module installation guide.