| 1 | #=============================================================================== | ||
| 2 | # | ||
| 3 | # MODULE: NetSDS::Session | ||
| 4 | # | ||
| 5 | # DESCRIPTION: Memcached based session data storage | ||
| 6 | # | ||
| 7 | # AUTHOR: Michael Bochkaryov (Rattler), <misha@rattler.kiev.ua> | ||
| 8 | # COMPANY: Net.Style | ||
| 9 | # | ||
| 10 | #=============================================================================== | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | B<NetSDS::Session> - memcached based session storage API | ||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | use NetSDS::Session; | ||
| 19 | |||
| 20 | # Connecting to Memcached server | ||
| 21 | my $sess = NetSDS::Session->new( | ||
| 22 | host => '12.34.56.78', | ||
| 23 | port => '12345', | ||
| 24 | ); | ||
| 25 | |||
| 26 | ... | ||
| 27 | |||
| 28 | # Retrieve session key somehow | ||
| 29 | $session_key = $cgi->param('sess_key'); | ||
| 30 | |||
| 31 | $sess->open($session_key); | ||
| 32 | |||
| 33 | my $filter = $sess->get('filter'); | ||
| 34 | ... | ||
| 35 | $sess->set('filter', $new_filter); | ||
| 36 | ... | ||
| 37 | $sess->close(); | ||
| 38 | |||
| 39 | 1; | ||
| 40 | |||
| 41 | =head1 DESCRIPTION | ||
| 42 | |||
| 43 | C<NetSDS::Session> module provides API to session data storage based on Memcached server. | ||
| 44 | |||
| 45 | Each session represented as hash reference structure identified by UUID string. | ||
| 46 | Most reasonable usage of this module is a temporary data storing for web based GUI | ||
| 47 | between HTTP requests. However it's possible to find some other tasks. | ||
| 48 | |||
| 49 | Internally session structure is transformed to/from JSON string when interacting with Memcached. | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | package NetSDS::Session; | ||
| 54 | |||
| 55 | 2 | ▶ | use 5.8.0; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 56 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 57 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 58 | |||
| 59 | 2 | ▶ | use version; our $VERSION = '1.301'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 60 | |||
| 61 | 0 | ▶ | use Cache::Memcached::Fast; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 62 | use JSON; | ||
| 63 | |||
| 64 | use base 'NetSDS::Class::Abstract'; | ||
| 65 | |||
| 66 | =head1 CLASS API | ||
| 67 | |||
| 68 | =over | ||
| 69 | |||
| 70 | =item B<new(%params)> - class constructor | ||
| 71 | |||
| 72 | Constructor establish connection to memcached server and set default session parameters. | ||
| 73 | |||
| 74 | Parameters: | ||
| 75 | |||
| 76 | * host - memcached server hostname or IP address (default: 127.0.0.1) | ||
| 77 | * port - memcached server TCP port (default: 11211) | ||
| 78 | |||
| 79 | Example: | ||
| 80 | |||
| 81 | my $sess_hdl = NetSDS::Session->new( | ||
| 82 | host => '12.34.56.78', | ||
| 83 | port => '99999', | ||
| 84 | ); | ||
| 85 | |||
| 86 | =cut | ||
| 87 | |||
| 88 | sub new { | ||
| 89 | |||
| 90 | my ( $class, %params ) = @_; | ||
| 91 | |||
| 92 | my $self = $class->SUPER::new( | ||
| 93 | session_id => undef, # session id (UUID string) | ||
| 94 | session_data => {}, # session data as hash reference | ||
| 95 | %params | ||
| 96 | ); | ||
| 97 | |||
| 98 | # Prepare server address string (host:port) | ||
| 99 | my $mc_host = $params{'host'} || '127.0.0.1'; | ||
| 100 | my $mc_port = $params{'port'} || '11211'; | ||
| 101 | |||
| 102 | # Initialize memcached handler | ||
| 103 | $self->{memcached} = Cache::Memcached::Fast->new( | ||
| 104 | { | ||
| 105 | servers => [ { address => $mc_host . ':' . $mc_port } ], | ||
| 106 | serialize_methods => [ \&JSON::encode_json, \&JSON::decode_json ], | ||
| 107 | } | ||
| 108 | ); | ||
| 109 | |||
| 110 | if ( $self->{memcached} ) { | ||
| 111 | return $self; | ||
| 112 | } else { | ||
| 113 | return $class->error("Can't create memcached connection handler"); | ||
| 114 | } | ||
| 115 | |||
| 116 | } ## end sub new | ||
| 117 | |||
| 118 | =item B<open($sess_id)> - open session | ||
| 119 | |||
| 120 | Retrieve session data from server by session key (UUID string) | ||
| 121 | |||
| 122 | If no session exists then empty hashref is returned. | ||
| 123 | |||
| 124 | =cut | ||
| 125 | |||
| 126 | sub open { | ||
| 127 | |||
| 128 | my ( $self, $sess_id ) = @_; | ||
| 129 | |||
| 130 | # Initialize session key and retrieve data | ||
| 131 | $self->{_id} = $sess_id; | ||
| 132 | $self->{_data} = $self->{memcached}->get($sess_id); | ||
| 133 | |||
| 134 | # If no such session stored then create empty hashref | ||
| 135 | $self->{_data} ||= {}; | ||
| 136 | |||
| 137 | return $self; | ||
| 138 | } | ||
| 139 | |||
| 140 | =item B<id()> - get session id | ||
| 141 | |||
| 142 | Returns current session id. | ||
| 143 | |||
| 144 | Example: | ||
| 145 | |||
| 146 | my $sess_id = $sess->id(); | ||
| 147 | |||
| 148 | =cut | ||
| 149 | |||
| 150 | sub id { | ||
| 151 | my $self = shift; | ||
| 152 | return $self->{_id}; | ||
| 153 | } | ||
| 154 | |||
| 155 | =item B<set($key, $value)> - set session parameter | ||
| 156 | |||
| 157 | Set new session parameter value identified by it's key. | ||
| 158 | |||
| 159 | Example: | ||
| 160 | |||
| 161 | $sess->set('order', 'id desc'); | ||
| 162 | |||
| 163 | =cut | ||
| 164 | |||
| 165 | sub set { | ||
| 166 | my ( $self, $key, $value ) = @_; | ||
| 167 | $self->{_data}->{$key} = $value; | ||
| 168 | return 1; | ||
| 169 | } | ||
| 170 | |||
| 171 | =item B<get($key)> - get session parameter | ||
| 172 | |||
| 173 | Return session parameter value by it's key. | ||
| 174 | |||
| 175 | Example: | ||
| 176 | |||
| 177 | my $order = $sess->get('order'); | ||
| 178 | |||
| 179 | =cut | ||
| 180 | |||
| 181 | sub get { | ||
| 182 | my ( $self, $key ) = @_; | ||
| 183 | return $self->{_data}->{$key}; | ||
| 184 | } | ||
| 185 | |||
| 186 | =item B<delete($key)> - delete session parameter by key | ||
| 187 | |||
| 188 | Delete session parameter by it's key. | ||
| 189 | |||
| 190 | Returns updated session data as hash reference. | ||
| 191 | |||
| 192 | Example: | ||
| 193 | |||
| 194 | $sess->delete('order'); | ||
| 195 | |||
| 196 | =cut | ||
| 197 | |||
| 198 | sub delete { | ||
| 199 | my ( $self, $key ) = @_; | ||
| 200 | |||
| 201 | delete $self->{_data}->{$key}; | ||
| 202 | return $self->{_data}; | ||
| 203 | } | ||
| 204 | |||
| 205 | =item B<clear()> - clear session data | ||
| 206 | |||
| 207 | This method clears all session data. | ||
| 208 | |||
| 209 | Example: | ||
| 210 | |||
| 211 | $sess->clear(); | ||
| 212 | |||
| 213 | =cut | ||
| 214 | |||
| 215 | sub clear { | ||
| 216 | my $self = shift; | ||
| 217 | $self->{_data} = {}; | ||
| 218 | } | ||
| 219 | |||
| 220 | =item B<sync()> - save session | ||
| 221 | |||
| 222 | Synchronize session data on Memcached server. | ||
| 223 | |||
| 224 | Example: | ||
| 225 | |||
| 226 | $sess->sync(); | ||
| 227 | |||
| 228 | =cut | ||
| 229 | |||
| 230 | sub sync { | ||
| 231 | |||
| 232 | my $self = shift; | ||
| 233 | |||
| 234 | return $self->id ? $self->{memcached}->set( $self->id, $self->{_data} ) : undef; | ||
| 235 | |||
| 236 | } | ||
| 237 | |||
| 238 | =item B<close()> - save and close session | ||
| 239 | |||
| 240 | This method save all data to server and clear current session id and data from object. | ||
| 241 | |||
| 242 | Example: | ||
| 243 | |||
| 244 | $session->close(); | ||
| 245 | |||
| 246 | =cut | ||
| 247 | |||
| 248 | sub close { | ||
| 249 | |||
| 250 | my $self = shift; | ||
| 251 | |||
| 252 | # Nothing to store for non existent session key | ||
| 253 | unless ( $self->id ) { | ||
| 254 | return undef; | ||
| 255 | } | ||
| 256 | |||
| 257 | # Store session data to memcached server | ||
| 258 | # or clear it from server if it's empty | ||
| 259 | if ( $self->{_data} == {} ) { | ||
| 260 | $self->{memcached}->delete( $self->id ); | ||
| 261 | } else { | ||
| 262 | $self->{memcached}->set( $self->id, $self->{_data} ); | ||
| 263 | } | ||
| 264 | |||
| 265 | # Clear session id and data | ||
| 266 | $self->{_id} = undef; | ||
| 267 | $self->{_data} = undef; | ||
| 268 | |||
| 269 | return; | ||
| 270 | } ## end sub close | ||
| 271 | |||
| 272 | 1; | ||
| 273 | |||
| 274 | __END__ | ||