| 1 | package CGI::Session::Driver::memcached; | ||
| 2 | |||
| 3 | # $Id$ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | |||
| 7 | use Carp qw(croak); | ||
| 8 | use CGI::Session::Driver; | ||
| 9 | |||
| 10 | @CGI::Session::Driver::memcached::ISA = ( "CGI::Session::Driver" ); | ||
| 11 | our $VERSION = "0.04"; | ||
| 12 | |||
| 13 | sub init { | ||
| 14 | my $self = shift; | ||
| 15 | unless (defined $self->{Memcached}) { | ||
| 16 | return $self->set_error("init(): 'Memcached' attribute is required."); | ||
| 17 | } | ||
| 18 | |||
| 19 | return 1; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub retrieve { | ||
| 23 | my $self = shift; | ||
| 24 | my ($sid) = @_; | ||
| 25 | croak "retrieve(): usage error" unless $sid; | ||
| 26 | |||
| 27 | my $memcached = $self->{Memcached}; | ||
| 28 | my $rv = $memcached->get("$sid"); | ||
| 29 | #warn "retrieve(): sid=$sid, $rv\n"; | ||
| 30 | |||
| 31 | return 0 unless (defined $rv); | ||
| 32 | return $rv; | ||
| 33 | } | ||
| 34 | |||
| 35 | |||
| 36 | sub store { | ||
| 37 | my $self = shift; | ||
| 38 | my ($sid, $datastr) = @_; | ||
| 39 | croak "store(): usage error" unless $sid && $datastr; | ||
| 40 | |||
| 41 | #warn "store(): sid=$sid, $datastr\n"; | ||
| 42 | my $memcached = $self->{Memcached}; | ||
| 43 | $memcached->set($sid, $datastr); | ||
| 44 | |||
| 45 | return 1; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub remove { | ||
| 49 | my $self = shift; | ||
| 50 | my ($sid) = @_; | ||
| 51 | croak "remove(): usage error" unless $sid; | ||
| 52 | |||
| 53 | $self->{Memcached}->delete($sid); | ||
| 54 | |||
| 55 | return 1; | ||
| 56 | } | ||
| 57 | |||
| 58 | |||
| 59 | sub DESTROY { | ||
| 60 | my $self = shift; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub traverse { | ||
| 64 | my $self = shift; | ||
| 65 | my ($coderef) = @_; | ||
| 66 | |||
| 67 | # unless ( $coderef && ref( $coderef ) && (ref $coderef eq 'CODE') ) { | ||
| 68 | # croak "traverse(): usage error"; | ||
| 69 | # } | ||
| 70 | |||
| 71 | # do nothing | ||
| 72 | return 1; | ||
| 73 | } | ||
| 74 | |||
| 75 | 1; | ||
| 76 | |||
| 77 | |||
| 78 | =pod | ||
| 79 | |||
| 80 | =head1 NAME | ||
| 81 | |||
| 82 | CGI::Session::Driver::memcached - CGI::Session driver for memcached | ||
| 83 | |||
| 84 | =head1 SYNOPSIS | ||
| 85 | |||
| 86 | use strict; | ||
| 87 | use warnings; | ||
| 88 | use Cache::Memcached; # or Cache::Memcached::Fast | ||
| 89 | |||
| 90 | my $memcached = Cache::Memcached->new({ | ||
| 91 | servers => [ 'localhost:11211' ], | ||
| 92 | debug => 0, | ||
| 93 | compress_threshold => 10_000, | ||
| 94 | }); | ||
| 95 | my $session = CGI::Session->new( "driver:memcached", $sid, { Memcached => $memcached } ); | ||
| 96 | |||
| 97 | =head1 DESCRIPTION | ||
| 98 | |||
| 99 | B<memcached> stores session data into memcached. | ||
| 100 | |||
| 101 | =head1 DRIVER ARGUMENTS | ||
| 102 | |||
| 103 | The only supported driver argument is 'Memcached'. It's an instance of L<Cache::Memcached|Cache::Memcached>. | ||
| 104 | |||
| 105 | =head1 REQUIREMENTS | ||
| 106 | |||
| 107 | =over 4 | ||
| 108 | |||
| 109 | =item L<CGI::Session> | ||
| 110 | |||
| 111 | =item L<Cache::Memcached> or L<Cache::Memcached::Fast> | ||
| 112 | |||
| 113 | =back | ||
| 114 | |||
| 115 | =head1 TODO | ||
| 116 | |||
| 117 | =over 4 | ||
| 118 | |||
| 119 | =item Implement traverse method! | ||
| 120 | |||
| 121 | But I don't know how to get all objects store in memcached. | ||
| 122 | |||
| 123 | =back | ||
| 124 | |||
| 125 | =head1 AUTHOR | ||
| 126 | |||
| 127 | Kazuhiro Oinuma <oinume@cpan.org> | ||
| 128 | |||
| 129 | =head1 REPOSITORY | ||
| 130 | |||
| 131 | git clone git://github.com/oinume/p5-cgi-session-driver-memcached | ||
| 132 | |||
| 133 | =cut | ||
| 134 | |||
| 135 | =head1 COPYRIGHT AND LICENSE | ||
| 136 | |||
| 137 | Copyright (C) 2005 - 2009 Kazuhiro Oinuma <oinume@cpan.org>. All rights reserved. This library is free software. You can modify and or distribute it under the same terms as Perl itself. | ||
| 138 | |||
| 139 | =cut | ||
| 140 |