简体   繁体   English

在mod_perl处理程序的不同模块中使用方法

[英]use methods in different modules in mod_perl handler

I want to share a variable between different perl modules. 我想在不同的perl模块之间共享一个变量。 So I created a perl module called MyCache.pm which saves the variable (in my case a hash variable): 因此,我创建了一个名为MyCache.pm的perl模块,用于保存变量(在我的情况下为哈希变量):

package PerlModules::MyCache;
my %cache = ();
sub set {
  my ($key, $value) = @_;
  $cache{$key} = $value;
}
sub get {
  my ($key) = @_;
  return $cache{$key};
}

Now I have two handlers. 现在我有两个处理程序。 The one handler will call the set method and the other one will call the get method to access the information. 一个处理程序将调用set方法,另一个处理程序将调用get方法以访问信息。

package PerlModules::MyCacheSetter;
use Apache2::RequestRec();
use Apache2::RequestIO();
use Apache2::Const -compile => qw(OK);
use PerlModules::MyCache;
sub handler {
  my $r = shift;
  PerlModules::MyCache::set('test1', "true");
  PerlModules::MyCache::set('test2', "false");
  PerlModules::MyCache::set('test3', "true");
  return Apache2::Const::OK;
}

And here is the getter handler: 这是getter处理程序:

package PerlModules::MyCacheGetter;
use Apache2::RequestRec();
use Apache2::RequestIO();
use Apache2::Const -compile => qw(OK);
use PerlModules::MyCache;
sub handler {
  my $r = shift;
  $r->print(PerlModules::MyCache::get('test1'));
  $r->print(PerlModules::MyCache::get('test2'));
  $r->print(PerlModules::MyCache::get('test3'));
  return Apache2::Const::OK;
}

Now I've configured apache (via http.conf) to access these perl modules. 现在,我已经配置了Apache(通过http.conf)来访问这些perl模块。 I run the setter handler and then the getter, but there was no output. 我先运行setter处理程序,然后运行getter,但是没有输出。

In the error.log there are now some entries: 在error.log中,现在有一些条目:

Use of uninitialized value in subroutine entry at ../MyCacheGetter.pm line 14.
Use of uninitialized value in subroutine entry at ../MyCacheGetter.pm line 15.
Use of uninitialized value in subroutine entry at ../MyCacheGetter.pm line 16.

This lines are the three calls of the get method. 这几行是get方法的三个调用。 So what am I doing wrong? 那我在做什么错? How can I fix the problem and share my cache variable between different handlers? 如何解决该问题并在不同的处理程序之间共享我的缓存变量?

Your cache will only exist for the lifetime of a given Apache child process. 您的缓存将仅在给定的Apache子进程的生存期内存在。 If you want other processes to see it, you'll need to store it somewhere they can all get at it. 如果您想让其他进程看到它,则需要将其存储在他们都能使用的位置。

This is untested, but you can get the general idea: (Now tested). 这未经测试,但是您可以得到大致的想法:( 现在已测试)。 EDIT: OK, it seems like you can get some issues with Storable depending on what perl version and Storable version you're running. 编辑:好的,看来您可能会遇到Storable问题,具体取决于您正在运行的Perl版本和Storable版本。 I've replaced Storable with Data::Serialize in my example. 在我的示例中,我已将Storable替换为Data::Serialize I've also added a line to the get / set methods so that either the -> or :: syntax can be used. 我还在get / set方法中添加了一行,以便可以使用->::语法。

package PerlModules::MyCache;

use IPC::ShareLite qw/:lock/;
use Data::Serializer;
use 5.10.0;

my $key = 1234; # Your shared memory key (you set this!)

my $ipc = IPC::ShareLite->new(
    -key     => $key,
    -create  => 'yes',
    -destroy => 'no'
);

my $ser = Data::Serializer->new(
    serializer => 'Data::Dumper'
);

sub set {
    shift @_ if $_[0] eq __PACKAGE__;
    my ($key, $value) = @_;
    $ipc->lock(LOCK_EX);
    my $frozen; eval { $frozen = $ipc->fetch; };
    my $cache = defined($frozen) ? $ser->thaw($frozen) : {};
    $cache->{$key} = $value;
    $ipc->store($ser->freeze($cache));
    $ipc->unlock;
    return $value;
}

sub get {
    shift @_ if $_[0] eq __PACKAGE__;
    my ($key) = @_;
    my $frozen; eval { $frozen = $ipc->fetch; };
    my $cache = defined($frozen) ? $ser->thaw($frozen) : {};
    return $cache->{$key};
}

sub clear {
    shift @_ if $_[0] eq __PACKAGE__;
    $ipc->store($ser->freeze({}));
    return {};
}

1;

You might want to run PerlModules::MyCache->clear once before you test to ensure the correct structure of the cache storage. 您可能需要先运行PerlModules::MyCache->clear ,然后再进行测试以确保缓存存储的正确结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM