首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有一种方法可以在Dancer和Starman中使用包/全局变量?

有没有一种方法可以在Dancer和Starman中使用包/全局变量?
EN

Stack Overflow用户
提问于 2013-11-17 19:49:40
回答 1查看 707关注 0票数 3

当与Starman一起运行时,我想不出一种如何在Dancer应用程序中使用包变量(或任何类似的)。我想这与星人的预叉有某种关系,但这应该是一个特征,而不是一个bug。

下面是一个示例Dancer应用程序:

代码语言:javascript
复制
package nafig;
use Dancer;

my $a = 0;
$b = 0;
$nafig::c = 0;

any '/' => sub {
    warn join " ", $a++, $b++, $nafig::c++;
};

start;

然后我连续打三个电话到那个应用程序。首先,我在plack参考服务器上运行它,一切都如预期的那样工作:

代码语言:javascript
复制
$ plackup app.pl
HTTP::Server::PSGI: Accepting connections at http://0:5000/
0 0 0 at ... blah-blah-blah
1 1 1 at ... blah-blah-blah
2 2 2 at ... blah-blah-blah

但是当我对Starman做同样的事情时,我得到了以下信息。

代码语言:javascript
复制
$ plackup -s Starman app.pl
2013/11/17-23:33:35 Starman::Server (type Net::Server::PreFork) starting! pid(527)
Resolved [*]:5000 to [::]:5000, IPv6
Not including resolved host [0.0.0.0] IPv4 because it will be handled by [::] IPv6
Binding to TCP port 5000 on host :: with IPv6
Setting gid to "1000 1000 20 24 25 29 30 44 46 108 109 115 121 1000"
Starman: Accepting connections at http://*:5000/
0 0 0 at ... blah-blah-blah
0 0 0 at ... blah-blah-blah
0 0 0 at ... blah-blah-blah

但是,当快速刷新页面时,有时值会按预期递增。我想,在这些案子里,星人还是一样的。

我很惊讶这个问题从来没有在堆叠溢出问题上被问过。持久化变量对我来说是有用的,没有它们人们怎么跳舞?

提前感谢您的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-17 20:22:39

您将需要一个像缓存::Memcached这样的模块,它允许您在分叉线程上存储状态。

像这样的东西(未经测试)

代码语言:javascript
复制
use strict;
use warnings;

package nafig; #this should start with a capital letter
use Dancer;
use Cache::Memcached;

my $cache =  new Cache::Memcached {
    'servers' => ['127.0.0.1:11211'],
    'compress_threshold' => 10_000,
};

$cache->set("var1", 0);

any '/' => sub {

    my $value = $cache->get("var1");

    warn join " ", $value++;

    $cache->set("var1", $value);
};

start;

改编自这里http://perl.postbit.com/how-to-use-memcached-with-perl.html

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20035318

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档