1 package MooseX::Attribute::Cached;
7 MooseX::Attribute::Cached; Cache your Moose Attribute Value
15 our $AUTHORITY = 'cpan:JJNAPIORK';
23 our $VERSION = '0.01';
30 with 'MooseX::Attribute::Cached.pm';
32 ## Cache Storage Options Declared Manually
33 has 'shared_key_1' => (
35 cache_storage => ['Memcached' => {
36 'servers' => [ "10.0.0.15:11211", "10.0.0.15:11212"],
38 %other_attribute_options_1,
41 ## This attribute get's it's cache storage from a method, which can be
42 ## called from a lazy attribute or as a package method
43 has 'shared_key_2' => (
45 storage_key=>'something_different_shared_key_2',
46 %other_attribute_options_2,
49 ## Here's the provider for the attributes storage. Basically you need to
50 ## return the storage provider name and it's instantiation args.
51 sub _cache_storage_options_shared_key_2 {
52 ## If the calling attribute is lazy get's $self, otherwise we
53 ## gets __PACKAGE__. Getting $self could be useful if you are reading
54 ## the cache options from something like your config object, etc.
61 "/var/sock/memcached",
63 [ "10.0.0.17:11211", 3 ],
66 'compress_threshold' => 10_000,
71 ## <Rest of your Class Definition>
77 Store the value of your Moose Attributes in a cached storage. The purpose of
78 this is to faciliate sharing of attribute values over various processes or
79 every across machines, presuming you are using a distributed cache,
80 like Memcached. All instances with access to the same cache will share and
81 update a common value. That way:
83 1) All instances share the same attribute slot. Updates made by one
84 attribute are seen by all instances, even on different servers as long as
85 they share a distributed caching system (such as Memcached).
87 2) It can be a sort of 'persistance lite' although I highly recommend using
88 a real persistance system, such as a database or L<MooseX::Storage>.
90 3) You could probably use this as a sort of expensive class attribute, but
91 you will likely be more happy with L<MooseX::ClassAttribute>
93 At this time, this supports two Cachings systems, Memcached and FastMmap. If
94 you have need for other cache types, adding a driver for it should be easy, so
95 please send your patches and test cases.
99 John Napiorkowski, C<< <john.napiorkowski at takkle.com> >>
103 You can find documentation for this module with the perldoc command.
105 perldoc MooseX::Attributes::Cached
107 You can also look for information at:
111 =item * RT: CPAN's request tracker
113 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-Attributes-Cached>
115 =item * AnnoCPAN: Annotated CPAN documentation
117 L<http://annocpan.org/dist/MooseX-Attributes-Cached>
121 L<http://cpanratings.perl.org/d/MooseX-Attributes-Cached>
125 L<http://search.cpan.org/dist/MooseX-Attributes-CachedF>
131 L<Moose>, L<MooseX::ClassAttribute>, L<MooseX::Storage>, L<Cache::FastMmap>,
134 =head1 COPYRIGHT & LICENSE
136 Copyright 2008 John Napiorkowski, all rights reserved.
138 This program is free software; you can redistribute it and/or modify it
139 under the same terms as Perl itself.
143 no Moose; 1; # End of MooseX::Attributes::Cached