perl-modperl mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Torsten Foertsch <torsten.foert...@gmx.net>
Subject Re: Storing config values in-memory between sessions
Date Thu, 29 Oct 2009 13:10:06 GMT
On Thu 29 Oct 2009, Mahesh Khambadkone wrote:
> As it seldom changes, we dont want to use a database for these
> 'config values', yet need a way to retain in memory and dirty its'
> value from time to time.

Have a look at MMapDB which I have just uploaded to CPAN.

I wrote this module some time ago exactly for that case: configuration 
data that seldom changes. Before I have used BerkeleyDB. But that is a 
bit touchy when it comes to sudden process death. I wanted something 
simple, stable and fast. So an MMapDB database cannot be modified by a 
connected process because it is mapped read-only. Changing data means 
rewriting the database file and reconnect the new one. This also means 
the whole database is shared memory between all connected processes.

There is a tie() based interface. So, even dumping human readable data 
is simple:

perl -MMMapDB -MData::Dumper -e '
  print Dumper(MMapDB->new(filename=>shift)->start->main_index);
' /etc/opt/TRANSCONFIG/transconfig.mmdb

And a more complicated but faster interface:

perl -MMMapDB -le '
  # open the DB
  my $db=MMapDB->new(filename=>shift)->start;
  # get data record positions
  my @pos=$db->index_lookup($db->mainidx, qw!actn opi /svn!);
  # fetch a data item
  print $db->data_record($pos[0])->[2];
' /etc/opt/TRANSCONFIG/transconfig.mmdb

One even can store large values there and pass references to them around 
and thus avoid copying and mallocing that happens normally when you do 
$x=$y:

perl -MMMapDB -MDevel::Peek -e '
  my $db=MMapDB->new(filename=>shift)->start;
  my @pos=$db->index_lookup($db->mainidx, qw!actn opi /svn!);
  # get a reference to the data item to avoid copying
  my $v=\$db->data_record($pos[0])->[2];
  print $$v, "\n";
  Dump $$v
' /etc/opt/TRANSCONFIG/transconfig.mmdb
File: '/opt/svnbook'.$MATCHED_PATH_INFO
SV = PV(0x9d4b50) at 0x7c9248
  REFCNT = 1
  FLAGS = (POK,READONLY,pPOK)
  PV = 0x7f956388d240 "File: '/opt/svnbook'.$MATCHED_PATH_INFO"
  CUR = 39
  LEN = 0

You see $v points to a read-only variable. The PV pointer of this 
variable references the string inside the read-only mapped database 
file. Now you can do $x=$v and only the reference is copied.

Torsten

-- 
Need professional mod_perl support?
Just hire me: torsten.foertsch@gmx.net

Mime
View raw message