| 1 | |||
| 2 | package Apache2::ASP::ApplicationStateManager::Memcached; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings 'all'; | ||
| 6 | use base 'Apache2::ASP::ApplicationStateManager'; | ||
| 7 | use Cache::Memcached; | ||
| 8 | use Digest::MD5 'md5_hex'; | ||
| 9 | |||
| 10 | |||
| 11 | #============================================================================== | ||
| 12 | sub new | ||
| 13 | { | ||
| 14 | my ($class, %args) = @_; | ||
| 15 | |||
| 16 | my $s = bless { }, $class; | ||
| 17 | |||
| 18 | if( my $res = $s->retrieve ) | ||
| 19 | { | ||
| 20 | return $res; | ||
| 21 | } | ||
| 22 | else | ||
| 23 | { | ||
| 24 | return $s->create; | ||
| 25 | }# end if() | ||
| 26 | }# end new() | ||
| 27 | |||
| 28 | |||
| 29 | #============================================================================== | ||
| 30 | sub memd | ||
| 31 | { | ||
| 32 | my $s = shift; | ||
| 33 | |||
| 34 | $s->{memd} ||= new Cache::Memcached { | ||
| 35 | 'servers' => [ | ||
| 36 | split(/,\s*/, $s->context->config->data_connections->session->dsn ) | ||
| 37 | ], | ||
| 38 | namespace => 'app' . $s->context->config->web->application_name, | ||
| 39 | }; | ||
| 40 | $s->{memd}; | ||
| 41 | }# end memd() | ||
| 42 | |||
| 43 | |||
| 44 | #============================================================================== | ||
| 45 | sub retrieve | ||
| 46 | { | ||
| 47 | my ($s) = @_; | ||
| 48 | |||
| 49 | my $got = $s->memd->get( $s->context->config->web->application_name ); | ||
| 50 | $got->{memd} = $s->memd; | ||
| 51 | $got = bless $got, ref($s) || $s | ||
| 52 | unless UNIVERSAL::isa( $got, __PACKAGE__ ); | ||
| 53 | return $got; | ||
| 54 | }# end retrieve() | ||
| 55 | |||
| 56 | |||
| 57 | #============================================================================== | ||
| 58 | sub create | ||
| 59 | { | ||
| 60 | my ($s, $SessionID) = @_; | ||
| 61 | |||
| 62 | eval { | ||
| 63 | no warnings 'uninitialized'; | ||
| 64 | $s->{__signature} = md5_hex( | ||
| 65 | join ":", | ||
| 66 | map { "$_:$s->{$_}" } | ||
| 67 | grep { $_ !~ m/^(memd|__signature)/ } sort keys(%$s) | ||
| 68 | ); | ||
| 69 | }; | ||
| 70 | |||
| 71 | $s->memd->set( | ||
| 72 | $s->context->config->web->application_name => $s | ||
| 73 | ); | ||
| 74 | $s; | ||
| 75 | }# end create() | ||
| 76 | |||
| 77 | |||
| 78 | #============================================================================== | ||
| 79 | sub save | ||
| 80 | { | ||
| 81 | my ($s) = @_; | ||
| 82 | |||
| 83 | no warnings 'uninitialized'; | ||
| 84 | return if eval { $s->{__signature} eq md5_hex( | ||
| 85 | join ":", map { "$_:$s->{$_}" } | ||
| 86 | grep { $_ !~ m/^(memd|__signature)/ } sort keys(%$s) | ||
| 87 | )}; | ||
| 88 | eval { | ||
| 89 | $s->{__signature} = md5_hex( | ||
| 90 | join ":", | ||
| 91 | map { "$_:$s->{$_}" } | ||
| 92 | grep { $_ !~ m/^(memd|__signature)/ } sort keys(%$s) | ||
| 93 | ); | ||
| 94 | }; | ||
| 95 | |||
| 96 | $s->memd->set( | ||
| 97 | $s->context->config->web->application_name => $s, | ||
| 98 | ); | ||
| 99 | 1; | ||
| 100 | }# end save() | ||
| 101 | |||
| 102 | |||
| 103 | 1;# return true: | ||
| 104 |