DancerとStarmanでパッケージ/グローバル変数を使用する方法はありますか?

StackOverflow https://stackoverflow.com//questions/20035318

  •  21-12-2019
  •  | 
  •  

質問

Starmanで実行すると、ダンサーアプリでパッケージ変数(またはソートのもの)を使用する方法を把握できません。私はそれがStarmanのPreporkingに関連しているが、それがバグではなく機能になるはずです。

これはダンサーアプリの例です:

package nafig;
use Dancer;

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

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

start;
.

それから私はそのアプリを3回連続して呼び出します。まず、Plack Reference Serverで実行し、すべてが期待どおりに機能します。

$ 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と同じことをするとき、私は以下を受けます。

$ 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
.

しかしながら、ページを素早く更新するときは、期待どおりに値が増加することがあります。私は、Starmanはそれらの場合と同じフォークに残ります。

この質問は、前にStackOverflowで尋ねられなかったことを驚かせました。持続的な変数は私にとって有用です、人々はそれらなしで踊るのですか?

事前に助けてくれてありがとう。

役に立ちましたか?

解決

cache :: memcached を必要とするモジュールが必要です。フォークスレッドの上に。

このようなもの(未テスト)

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top