Apache::SharedMem version 0.09

This module make it easer to share data between Apache children processes trough the shared memory. This module internal working is a lot inspired by IPC::SharedCache, but without any cache managment. The share memory segment key is automatically deduced by the caller package name, that's mine 2 modules can use same keys (variable's name) without be concerned about namespace clash.

INSTALLATION

To install this module type the following:

perl Makefile.PL
make
make test
make install

DEPENDENCIES

This module requires these other modules and libraries:

IPC::ShareLite
IPC::SysV
Storable
Data::Dumper

CHANGES

0.09 Thu Oct 4, 2001 13:54:53

0.08 Tue Oct 2, 2001 16:19:08

0.07 Mon August 27, 2001 18:36:52

0.06 Mon August 20, 2001 15:55:50

0.05 Tue Jully 03, 2001 17:41:30

0.04 Wen Junary 27, 2001 08:58:54

0.03 Tue Junary 26, 2001 21:09:46

0.02 Tue Junary 26, 2001 14:20:02

0.01 Thu Junary 21, 2001 23:37:46

NAME

Apache::SharedMem - Share data between Apache children processes through the shared memory

SYNOPSIS

use Apache::SharedMem qw(:lock :status);

my $share = new Apache::SharedMem || die($Apache::SharedMem::ERROR);

$share->set(key => 'some data');

        # ...maybe in another apache child
        my $var = $share->get(key);

        $share->delete(key);

        # delete all keys if the total size is larger than $max_size
        $share->clear if($share->size > $max_size);

        # using an exclusive blocking lock, but with a timeout
        my $lock_timeout = 40; # seconds
        if($share->lock(LOCK_EX, $lock_timeout))
        {
            my $data =...
            ...some traitement...
        
            $share->set(key => $data); # the implicite lock is not overrided
            warn('failed to store data in shared memory') if($share->status & FAILURE);

            $share->unlock;
        }
    
        $share->release;

DESCRIPTION

This module make it easier to share data between Apache children processes through shared memory. This module internal functionment is much inspired from IPC::SharedCache, but without any cache management. The share memory segment key is automatically deduced by the caller package, which means that 2 modules can use same keys without being concerned about namespace clash. An additionnal namespace is used per application, which means that the same module, with the same namespace used in two applications doesn't clash too. Application distinction is made on two things : the process' UID and DOCUMENT_ROOT (for http applications) or current working directory.

This module handles all shared memory interaction via the IPC::SharedLite and all data serialization with Storable. See the IPC::ShareLite manpage and the Storable manpage for details.

USAGE

If you are running under mod_perl, you should put this line in your httpd.conf:

        # must be a valid path
        PerlAddVar PROJECT_DOCUMENT_ROOT /path/to/your/projects/root

and in your startup.pl:

use Apache::SharedMem;

This allow Apache::SharedMem to determine a unique rootkey for all virtual hosts, and to cleanup this rootkey on Apache stop. PROJECT_DOCUMENT_ROOT is used instead of a per virtal host's DOCUMENT_ROOT for rootkey's generation.

You can also provide a PROJECT_ID, it's the server's uid by default. This value have to be numeric:

PerlAddVar PROJECT_ID 10

METHODS
new (namespace => 'Namespace', ipc_mode => 0666, ipc_segment_size => 1_000, debug => 1)

In most case, you don't need to give any arguments to the constructor.

"ipc_mode" and "ipc_segment_size" are used only on the first namespace initialisation. Using different values on an existing key (in shared memory) has no effect.

Note that "ipc_segment_size" is default value of IPC::ShareLite, see the IPC::ShareLite manpage

On succes return an Apache::SharedMem object, on error, return undef(). You can get error string via $Apache::SharedMem::ERROR.

get (key, [wait, [timeout]])

my $var = $object->get('mykey', WAIT, 50); if($object->status & FAILURE) { die("can't get key 'mykey´: " . $object->error); }

use Apache::SharedMem qw(:wait)

timeout (optional) integer:

        if WAIT is on, timeout setup the number of seconds to wait for a
        blocking lock, usefull for preventing dead locks.

Following status can be set (needs :status tag import):

SUCCESS FAILURE

On error, method return undef(), but undef() is also a valid answer, so don't test the method status by this way, use ($obj->status & SUCCESS) instead.

set (key, value, [wait, [timeout]])

my $rv = $object->set('mykey' => 'somevalue'); if($object->status eq FAILURE) { die("can't set key 'mykey´: " . $object->error); }

Try to set element "key" to "value" from the shared segment.

return status: SUCCESS FAILURE

delete (key, [wait, [timeout]])

exists (key, [wait, [timeout]])

firstkey ([wait, [timeout]])

nextkey (lastkey, [wait, [timeout]])

clear ([wait, [timeout]])

return 0 on error

release [namespace]

Release share memory space taken by the given namespace or object's namespace. Root map will be release too if empty.

destroy

Destroy all namespace found in the root map, and root map itself.

size ([wait, [timeout]])

namespaces

Debug method, return the list of all namespace in the root map. (devel only)

lock ([lock_type, [timeout]])

get a lock on the share segment. It returns "undef()" if failed, 1 if successed.

return status: FAILURE SUCCESS

unlock

freeing a lock

error

return the last error message that happened.

status

Return the last called method status. This status should be used with bitmask operators &, ^, ~ and | like this :

        # is last method failed ?
        if($object->status & FAILURE) {something to do on failure}

        # is last method don't succed ?
        if($object->status ^ SUCCESS) {something to do on failure}

It's not recommended to use equality operator (== and !=) or (eq and ne), they may don't work in future versions.

To import status' constants, you have to use the :status import tag, like below :

use Apache::SharedMem qw(:status);

EXPORTS
Default exports

None.

Available exports

Following constant is available for exports : LOCK_EX LOCK_SH LOCK_UN LOCK_NB WAIT NOWAIT SUCCESS FAILURE

Export tags defined

The tag ":all" will get all of the above exports. Following tags are also available :

:status

Contents: SUCCESS FAILURE

This tag is really recommended to the importation all the time.

:lock

Contents: LOCK_EX LOCK_SH LOCK_UN LOCK_NB

:wait

WAIT NOWAIT

AUTHOR

Olivier Poitrey <rs@rhapsodyk.net>

LICENCE

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with the program; if not, write to the Free Software Foundation, Inc. :

59 Temple Place, Suite 330, Boston, MA 02111-1307

COPYRIGHT

Copyright (C) 2001 - Olivier Poitrey

PREREQUISITES

Apache::SharedMem needs IPC::ShareLite, Storable both available from the CPAN.

SEE ALSO

the IPC::ShareLite manpage, the shmget manpage, the ftok manpage