| 1 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||
| 2 | # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de> | ||||||||||
| 3 | # File: GX/Session/Store/Cache.pm | ||||||||||
| 4 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||
| 5 | |||||||||||
| 6 | package GX::Session::Store::Cache; | ||||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use GX::Exception; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | |||||||||||
| 11 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||
| 12 | # Class setup | ||||||||||
| 13 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||
| 14 | |||||||||||
| 15 | 1 | ▶ | use GX::Class; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 1 | extends 'GX::Session::Store'; | |||||||||
| 18 | |||||||||||
| 19 | has 'cache' => ( | ||||||||||
| 20 | isa => 'Object', | ||||||||||
| 21 | 0 | ▶ | preprocessor => sub { eval { $_ = $_->instance } }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 22 | 0 | ▶ | constraint => sub { $_->isa( 'GX::Cache' ) }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 23 | 1 | required => 1, | |||||||||
| 24 | weaken => 1, | ||||||||||
| 25 | accessor => { type => 'get' } | ||||||||||
| 26 | ); | ||||||||||
| 27 | |||||||||||
| 28 | 1 | has 'key_prefix' => ( | |||||||||
| 29 | isa => 'String', | ||||||||||
| 30 | initialize => 1, | ||||||||||
| 31 | accessor => { type => 'get' } | ||||||||||
| 32 | ); | ||||||||||
| 33 | |||||||||||
| 34 | 1 | build; | |||||||||
| 35 | |||||||||||
| 36 | |||||||||||
| 37 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||
| 38 | # Public methods | ||||||||||
| 39 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||
| 40 | |||||||||||
| 41 | 0 | ▶ | sub delete { | ||||||||
|
Subroutine
delete: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 42 | |||||||||||
| 43 | 0 | my $self = shift; | |||||||||
| 44 | 0 | my $session_id = shift; | |||||||||
| 45 | |||||||||||
| 46 | 0 | ▶ | if ( ! defined $session_id ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 0 | complain "Missing argument"; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 0 | my $result = $self->cache->remove( $self->key_prefix . $session_id ); | |||||||||
| 51 | |||||||||||
| 52 | 0 | return $result; | |||||||||
| 53 | |||||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 0 | ▶ | sub load { | ||||||||
|
Subroutine
load: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 57 | |||||||||||
| 58 | 0 | my $self = shift; | |||||||||
| 59 | 0 | my $session_id = shift; | |||||||||
| 60 | |||||||||||
| 61 | 0 | ▶ | if ( ! defined $session_id ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 0 | complain "Missing argument"; | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 0 | my $cache_data = $self->cache->get( $self->key_prefix . $session_id ); | |||||||||
| 66 | |||||||||||
| 67 | 0 | ▶ | return $cache_data ? @{ $self->serializer->unserialize( $cache_data ) } : (); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | |||||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | 0 | ▶ | sub save { | ||||||||
|
Subroutine
save: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 72 | |||||||||||
| 73 | 0 | my $self = shift; | |||||||||
| 74 | 0 | my $session_id = shift; | |||||||||
| 75 | 0 | my $session_info = shift; | |||||||||
| 76 | 0 | my $session_data = shift; | |||||||||
| 77 | |||||||||||
| 78 | 0 | ▶ | if ( ! defined $session_id ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 0 | complain "Missing argument"; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | 0 | ▶ | if ( ! defined $session_info ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 0 | complain "Missing argument"; | |||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | 0 | ▶ | if ( ref $session_info ne 'HASH' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 0 | complain "Invalid argument"; | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | 0 | my $cache_key = $self->key_prefix . $session_id; | |||||||||
| 91 | 0 | my $cache_data = $self->serializer->serialize( [ $session_info, $session_data ] ); | |||||||||
| 92 | |||||||||||
| 93 | 0 | my $result = $self->cache->add( $cache_key, $cache_data, $session_info->{'expires_at'} ); | |||||||||
| 94 | |||||||||||
| 95 | 0 | return $result; | |||||||||
| 96 | |||||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | 0 | ▶ | sub update { | ||||||||
|
Subroutine
update: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 100 | |||||||||||
| 101 | 0 | my $self = shift; | |||||||||
| 102 | 0 | my $session_id = shift; | |||||||||
| 103 | 0 | my $session_info = shift; | |||||||||
| 104 | 0 | my $session_data = shift; | |||||||||
| 105 | |||||||||||
| 106 | 0 | ▶ | if ( ! defined $session_id ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 107 | 0 | complain "Missing argument"; | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | 0 | ▶ | if ( ! defined $session_info ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 0 | complain "Missing argument"; | |||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | 0 | ▶ | if ( ref $session_info ne 'HASH' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | 0 | complain "Invalid argument"; | |||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | 0 | my $cache_key = $self->key_prefix . $session_id; | |||||||||
| 119 | 0 | my $cache_data = $self->serializer->serialize( [ $session_info, $session_data ] ); | |||||||||
| 120 | |||||||||||
| 121 | 0 | my $result = $self->cache->replace( $cache_key, $cache_data, $session_info->{'expires_at'} ); | |||||||||
| 122 | |||||||||||
| 123 | 0 | return $result; | |||||||||
| 124 | |||||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | |||||||||||
| 128 | 1 | 1; | |||||||||
| 129 | |||||||||||
| 130 | __END__ | ||||||||||