| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Cache::Ehcache; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 4 |  |  | 4 |  | 65783 | use strict; | 
|  | 4 |  |  |  |  | 6 |  | 
|  | 4 |  |  |  |  | 149 |  | 
| 4 | 4 |  |  | 4 |  | 19 | use warnings; | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 111 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 4 |  |  | 4 |  | 168 | use 5.008; | 
|  | 4 |  |  |  |  | 14 |  | 
|  | 4 |  |  |  |  | 225 |  | 
| 7 |  |  |  |  |  |  | our $VERSION = '0.03'; | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 4 |  |  | 4 |  | 4990 | use LWP::UserAgent; | 
|  | 4 |  |  |  |  | 249023 |  | 
|  | 4 |  |  |  |  | 147 |  | 
| 10 | 4 |  |  | 4 |  | 3781 | use Moose; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | has 'server' => ( | 
| 13 |  |  |  |  |  |  | is       => 'rw', | 
| 14 |  |  |  |  |  |  | isa      => 'Str', | 
| 15 |  |  |  |  |  |  | required => 1, | 
| 16 |  |  |  |  |  |  | default  => 'http://localhost:8080/ehcache/rest/', | 
| 17 |  |  |  |  |  |  | ); | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | has 'namespace' => ( | 
| 20 |  |  |  |  |  |  | is       => 'rw', | 
| 21 |  |  |  |  |  |  | isa      => 'Str', | 
| 22 |  |  |  |  |  |  | required => 1, | 
| 23 |  |  |  |  |  |  | ); | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | has 'default_expires_in' => ( | 
| 26 |  |  |  |  |  |  | is       => 'rw', | 
| 27 |  |  |  |  |  |  | isa      => 'Int', | 
| 28 |  |  |  |  |  |  | required => 1, | 
| 29 |  |  |  |  |  |  | default  => 0, | 
| 30 |  |  |  |  |  |  | ); | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | has 'ua' => ( | 
| 33 |  |  |  |  |  |  | is       => 'rw', | 
| 34 |  |  |  |  |  |  | isa      => 'Object', | 
| 35 |  |  |  |  |  |  | required => 1, | 
| 36 |  |  |  |  |  |  | default  => sub { | 
| 37 |  |  |  |  |  |  | my $self = shift; | 
| 38 |  |  |  |  |  |  | my $ua   = LWP::UserAgent->new( | 
| 39 |  |  |  |  |  |  | agent   => __PACKAGE__ . "/$VERSION", | 
| 40 |  |  |  |  |  |  | timeout => 60, | 
| 41 |  |  |  |  |  |  | ); | 
| 42 |  |  |  |  |  |  | return $ua; | 
| 43 |  |  |  |  |  |  | }, | 
| 44 |  |  |  |  |  |  | ); | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | sub BUILDARGS { | 
| 47 |  |  |  |  |  |  | my ( $class, %args ) = @_; | 
| 48 |  |  |  |  |  |  | if (   $args{server} | 
| 49 |  |  |  |  |  |  | && $args{server} !~ m{/$} ) | 
| 50 |  |  |  |  |  |  | { | 
| 51 |  |  |  |  |  |  | $args{server} = $args{server} . '/'; | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  | return {%args}; | 
| 54 |  |  |  |  |  |  | } | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | sub BUILD { | 
| 57 |  |  |  |  |  |  | my $self = shift; | 
| 58 |  |  |  |  |  |  | my $req  = HTTP::Request->new( 'PUT', $self->_make_url() ); | 
| 59 |  |  |  |  |  |  | my $res  = $self->ua->request($req); | 
| 60 |  |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | __PACKAGE__->meta->make_immutable; | 
| 63 |  |  |  |  |  |  | no Moose; | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | use HTTP::Request; | 
| 66 |  |  |  |  |  |  | use HTTP::Status qw(:constants); | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | sub set { | 
| 69 |  |  |  |  |  |  | my $self = shift; | 
| 70 |  |  |  |  |  |  | my ( $key, $value, $expires_in ) = @_; | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | my @header = | 
| 73 |  |  |  |  |  |  | ($expires_in) ? ( 'ehcacheTimeToLiveSeconds' => $expires_in ) : (); | 
| 74 |  |  |  |  |  |  | my $req = | 
| 75 |  |  |  |  |  |  | HTTP::Request->new( 'PUT', $self->_make_url($key), \@header, $value ); | 
| 76 |  |  |  |  |  |  | my $res = $self->ua->request($req); | 
| 77 |  |  |  |  |  |  | unless ( $res->is_success ) { | 
| 78 |  |  |  |  |  |  | warn $res->status_line . "\n"; | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | sub get { | 
| 83 |  |  |  |  |  |  | my $self  = shift; | 
| 84 |  |  |  |  |  |  | my ($key) = @_; | 
| 85 |  |  |  |  |  |  | my $res   = $self->ua->get( $self->_make_url($key) ); | 
| 86 |  |  |  |  |  |  | if ( $res->is_success ) { | 
| 87 |  |  |  |  |  |  | return $res->decoded_content; | 
| 88 |  |  |  |  |  |  | } | 
| 89 |  |  |  |  |  |  | elsif ( $res->code != HTTP_NOT_FOUND ) { | 
| 90 |  |  |  |  |  |  | warn $res->status_line . "\n"; | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | sub delete { | 
| 95 |  |  |  |  |  |  | my $self  = shift; | 
| 96 |  |  |  |  |  |  | my ($key) = @_; | 
| 97 |  |  |  |  |  |  | my $req   = HTTP::Request->new( 'DELETE', $self->_make_url($key) ); | 
| 98 |  |  |  |  |  |  | my $res   = $self->ua->request($req); | 
| 99 |  |  |  |  |  |  | if ( !$res->is_success && $res->code != HTTP_NOT_FOUND ) { | 
| 100 |  |  |  |  |  |  | warn $res->status_line . "\n"; | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | sub clear { | 
| 105 |  |  |  |  |  |  | my $self = shift; | 
| 106 |  |  |  |  |  |  | my $req  = HTTP::Request->new( 'DELETE', $self->_make_url('*') ); | 
| 107 |  |  |  |  |  |  | my $res  = $self->ua->request($req); | 
| 108 |  |  |  |  |  |  | unless ( $res->is_success ) { | 
| 109 |  |  |  |  |  |  | warn $res->status_line . "\n"; | 
| 110 |  |  |  |  |  |  | } | 
| 111 |  |  |  |  |  |  | } | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | sub _make_url { | 
| 114 |  |  |  |  |  |  | my $self = shift; | 
| 115 |  |  |  |  |  |  | my ($path) = @_; | 
| 116 |  |  |  |  |  |  | return $self->server . $self->namespace . ( $path ? "/$path" : '' ); | 
| 117 |  |  |  |  |  |  | } | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | 1; | 
| 120 |  |  |  |  |  |  | __END__ | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | =head1 NAME | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | Cache::Ehcache - client library for Ehcache Server | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | use Cache::Ehcache; | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | my $cache = Cache::Ehcache->new({ | 
| 131 |  |  |  |  |  |  | 'server'             => 'http://localhost:8080/ehcache/rest/', | 
| 132 |  |  |  |  |  |  | 'namespace'          => 'mynamespace', | 
| 133 |  |  |  |  |  |  | 'default_expires_in' => 600, # option | 
| 134 |  |  |  |  |  |  | }); | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | # set cache element | 
| 137 |  |  |  |  |  |  | $cache->set('key_1', 'value_1'); | 
| 138 |  |  |  |  |  |  | $cache->set('key_2', 'value_2', 3600);   # specify expires_in | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | # get cache element | 
| 141 |  |  |  |  |  |  | my $value = $cache->get('key_1'); | 
| 142 |  |  |  |  |  |  | # URL is 'http://localhost:8080/ehcache/rest/mynamespace/key_1' | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | # delete cache element | 
| 145 |  |  |  |  |  |  | $cache->delete('key_2'); | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | # remove all elements of this cache | 
| 148 |  |  |  |  |  |  | $cache->clear(); | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | Cache::Ehcache is client library for Ehcache Server. | 
| 153 |  |  |  |  |  |  | Ehcache is a widely used java distributed cache for general purpose caching. | 
| 154 |  |  |  |  |  |  | Ehcache Server is caching server like memcached, and has RESTful resource oriented API. | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | See: http://ehcache.sourceforge.net/documentation/cache_server.html | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | =head1 AUTHOR | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | YAMAMOTO Ryuzo (dragon3) E<lt>ryuzo.yamamoto@gmail.comE<gt> | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  | =head1 LICENSE | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself . | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | Ehcache Site | 
| 169 |  |  |  |  |  |  | http://ehcache.sourceforge.net/index.html, | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  | Ehcache Server | 
| 172 |  |  |  |  |  |  | http://ehcache.sourceforge.net/documentation/cache_server.html | 
| 173 |  |  |  |  |  |  |  | 
| 174 |  |  |  |  |  |  | =cut |