demand Module::Install 0.75
[gitmo/MooseX-Attribute-Cached.git] / lib / MooseX / Attribute / Cached.pm
CommitLineData
cf0ef7c5 1package MooseX::Attribute::Cached;
2
3use Moose;
4
d0e2ab77 5
cf0ef7c5 6=head1 NAME
7
8MooseX::Attribute::Cached; Cache your Moose Attribute Value
9
10=head2 AUTHORITY
11
12cpan:JJNAPIORK
13
14=cut
15
16our $AUTHORITY = 'cpan:JJNAPIORK';
17
18=head1 VERSION
19
20Version 0.01
21
22=cut
23
24our $VERSION = '0.01';
25
26=head1 SYNOPSIS
27
28 package MyApp;
29
30 use Moose;
d0e2ab77 31 with 'MooseX::Attribute::Cached';
cf0ef7c5 32
33 ## Cache Storage Options Declared Manually
34 has 'shared_key_1' => (
35 traits => ['Cached'],
36 cache_storage => ['Memcached' => {
37 'servers' => [ "10.0.0.15:11211", "10.0.0.15:11212"],
38 }],
39 %other_attribute_options_1,
40 );
41
42 ## This attribute get's it's cache storage from a method, which can be
43 ## called from a lazy attribute or as a package method
44 has 'shared_key_2' => (
45 traits => ['Cached'],
46 storage_key=>'something_different_shared_key_2',
47 %other_attribute_options_2,
48 );
49
50 ## Here's the provider for the attributes storage. Basically you need to
51 ## return the storage provider name and it's instantiation args.
52 sub _cache_storage_options_shared_key_2 {
53 ## If the calling attribute is lazy get's $self, otherwise we
54 ## gets __PACKAGE__. Getting $self could be useful if you are reading
55 ## the cache options from something like your config object, etc.
56 my $self = shift @_;
57 return [
58 'Memcached' => {
59 'servers' => [
60 "10.0.0.15:11211",
61 "10.0.0.15:11212",
62 "/var/sock/memcached",
63 "10.0.0.17:11211",
64 [ "10.0.0.17:11211", 3 ],
65 ],
66 'debug' => 0,
67 'compress_threshold' => 10_000,
68 },
69 ];
70 }
71
72 ## <Rest of your Class Definition>
73
74 1;
75
76=head1 DESCRIPTION
77
78Store the value of your Moose Attributes in a cached storage. The purpose of
79this is to faciliate sharing of attribute values over various processes or
80every across machines, presuming you are using a distributed cache,
81like Memcached. All instances with access to the same cache will share and
82update a common value. That way:
83
d0e2ab77 841) All instances share the same attribute value. Updates made by one
cf0ef7c5 85attribute are seen by all instances, even on different servers as long as
d0e2ab77 86they share a distributed caching system (such as Memcached).
87
cf0ef7c5 882) It can be a sort of 'persistance lite' although I highly recommend using
89a real persistance system, such as a database or L<MooseX::Storage>.
d0e2ab77 90
cf0ef7c5 913) You could probably use this as a sort of expensive class attribute, but
92you will likely be more happy with L<MooseX::ClassAttribute>
93
d0e2ab77 94Please keep in mind that the process of setting and getting values in and out
95of the Cache is asynchronise. When a value is updated, this is no 'Publish /
96Subscribe' system running to let all the instances using this attribute know it
97ha been changed. Also, since the last value that was retrieved from the Cache
98is stored by the instance, any references will point to this.
99
cf0ef7c5 100At this time, this supports two Cachings systems, Memcached and FastMmap. If
101you have need for other cache types, adding a driver for it should be easy, so
102please send your patches and test cases.
103
104=head1 AUTHOR
105
106John Napiorkowski, C<< <john.napiorkowski at takkle.com> >>
107
108=head1 SUPPORT
109
110You can find documentation for this module with the perldoc command.
111
112 perldoc MooseX::Attributes::Cached
113
114You can also look for information at:
115
116=over 4
117
118=item * RT: CPAN's request tracker
119
120L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-Attributes-Cached>
121
122=item * AnnoCPAN: Annotated CPAN documentation
123
124L<http://annocpan.org/dist/MooseX-Attributes-Cached>
125
126=item * CPAN Ratings
127
128L<http://cpanratings.perl.org/d/MooseX-Attributes-Cached>
129
130=item * Search CPAN
131
132L<http://search.cpan.org/dist/MooseX-Attributes-CachedF>
133
134=back
135
136=head1 SEE ALSO
137
138L<Moose>, L<MooseX::ClassAttribute>, L<MooseX::Storage>, L<Cache::FastMmap>,
139L<Cache::Memcached>
140
141=head1 COPYRIGHT & LICENSE
142
d0e2ab77 143Copyright 2008 John Napiorkowski
cf0ef7c5 144
145This program is free software; you can redistribute it and/or modify it
146under the same terms as Perl itself.
147
148=cut
149
150no Moose; 1; # End of MooseX::Attributes::Cached