NAME
Tie::Concurrent - Paranoid tie for concurrent access
SYNOPSIS
use Tie::Concurrent;
tie %data, 'Tie::Concurrent', {READER=>[....], WRITER=>[....]};
DESCRIPTION
Modules like GDBM_File are fraught when you have potentialy many readers/writers (like say in a long running forking daemon). While they might handle file locking properly, if any program holds the lock for too long, others will not be able to write to the file.
This module solves the problem by doing a tie/operation/untie for each and every operation. NOTE THAT THIS IS ONE HUGE PERFORMANCE HIT. Only use this where all other methods fail.
The params to tie are :
- READER
-
Array ref that is used to tie the underlying hash when only reading is desired.
- WRITER
-
Array ref that is used to tie the underlying hash when writing is needed.
EXAMPLE
use Tie::Concurrent;
use GDBM_File;
use Storable;
use MLDBM qw(GDBM_File Storable);
my $file="search-cache.gdbm";
my %cache;
tie %cache, 'Tie::Concurrent', {
READER=>['MLDBM', $file, GDBM_READER, 0660],
WRITER=>['MLDBM', $file, GDBM_WRCREAT, 0660]
};
my $res=$cache{$key};
unless($res) {
$res=very_long_search($key);
$cache{$key}=$res;
}
print "\n", @$res;
NOTES
Please note that there are many problems with this aproach. For instance, in the above example, another process might have created $cache{$key} while we did our search and those values would be lost.
If a process tries to lock the file whist another already has the lock, we wait one second before trying again. This is not very friendly if to things like POE.
In fact, the truth is that Tie::Concurrent does locking only if the underlying object does locking. If you an AnyDBM_File which doesn't lock, Tie::Concurrent isn't safe. All it will gain you is that results are automatically flushed each fetch/store.
BUGS
FIRSTKEY/NEXTKEY not implemented yet.
MLDBM spits out warnings if it can't tie the file.
MLDBM::DELETE is broken. It does not return the deleted value.
AUTHOR
Philip Gwyn, <gwyn-at-cpan.org>
COPYRIGHT AND LICENSE
Copyright (C) 2009 by Philip Gwyn
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.
SEE ALSO
perl(1). perltie(1)