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