ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> perl-mod_perl
perl-mod_perl
Re: Storing config values in-memory between sessions
by Torsten Foertsch other posts by this author
Oct 29 2009 6:10AM messages near this date
Re: Storing config values in-memory between sessions | Re: Storing config values in-memory between sessions
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
Thread:
Mahesh Khambadkone
Perrin Harkins
Torsten Foertsch
Alan Young

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved