댄서와 스타 먼 (Starman)과 패키지 / 글로벌 변수를 사용하는 방법이 있습니까?

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

  •  21-12-2019
  •  | 
  •  

문제

StarMan과 함께 실행할 때 댄서 앱의 패키지 변수 (또는 정렬 중)를 사용하는 방법을 알아낼 수 없습니다.나는 그것이 스타 맨 프리킥과 어떻게 든 관련이 있지만 버그가 아닌 기능이되어야한다고 가정합니다.

여기에 예제 댄서 앱입니다.

package nafig;
use Dancer;

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

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

start;
.

그런 다음 해당 앱에 3 개의 연속적인 전화를 걸었습니다.첫째, Plack 참조 서버로 실행하면 모든 것이 예상대로 작동합니다.

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

그러나 페이지를 빠르게 새로 고치면 때로는 값이 예상대로 증가합니다.나는 스타먼이 그 사건에서 같은 포크에 남아 있기 때문에

이 질문은 이전에 Stackoverflow에서 묻지 않았습니다.지속적인 변수는 나에게 유용한 것처럼 보입니다. 사람들은 어떻게 춤을 춥니 까?

어떤 도움에도 미리 감사드립니다.

도움이 되었습니까?

해결책

캐시 :: 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