1 package MooseX::Attribute::Cached;
8 MooseX::Attribute::Cached; Cache your Moose Attribute Value
16 our $AUTHORITY = 'cpan:JJNAPIORK';
24 our $VERSION = '0.01';
31 with 'MooseX::Attribute::Cached';
33 ## Cache Storage Options Declared Manually
34 has 'shared_key_1' => (
36 cache_storage => ['Memcached' => {
37 'servers' => [ "10.0.0.15:11211", "10.0.0.15:11212"],
39 %other_attribute_options_1,
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' => (
46 storage_key=>'something_different_shared_key_2',
47 %other_attribute_options_2,
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.
62 "/var/sock/memcached",
64 [ "10.0.0.17:11211", 3 ],
67 'compress_threshold' => 10_000,
72 ## <Rest of your Class Definition>
78 Store the value of your Moose Attributes in a cached storage. The purpose of
79 this is to faciliate sharing of attribute values over various processes or
80 every across machines, presuming you are using a distributed cache,
81 like Memcached. All instances with access to the same cache will share and
82 update a common value. That way:
84 1) All instances share the same attribute value. Updates made by one
85 attribute are seen by all instances, even on different servers as long as
86 they share a distributed caching system (such as Memcached).
88 2) It can be a sort of 'persistance lite' although I highly recommend using
89 a real persistance system, such as a database or L<MooseX::Storage>.
91 3) You could probably use this as a sort of expensive class attribute, but
92 you will likely be more happy with L<MooseX::ClassAttribute>
94 Please keep in mind that the process of setting and getting values in and out
95 of the Cache is asynchronise. When a value is updated, this is no 'Publish /
96 Subscribe' system running to let all the instances using this attribute know it
97 ha been changed. Also, since the last value that was retrieved from the Cache
98 is stored by the instance, any references will point to this.
100 At this time, this supports two Cachings systems, Memcached and FastMmap. If
101 you have need for other cache types, adding a driver for it should be easy, so
102 please send your patches and test cases.
106 John Napiorkowski, C<< <john.napiorkowski at takkle.com> >>
110 You can find documentation for this module with the perldoc command.
112 perldoc MooseX::Attributes::Cached
114 You can also look for information at:
118 =item * RT: CPAN's request tracker
120 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-Attributes-Cached>
122 =item * AnnoCPAN: Annotated CPAN documentation
124 L<http://annocpan.org/dist/MooseX-Attributes-Cached>
128 L<http://cpanratings.perl.org/d/MooseX-Attributes-Cached>
132 L<http://search.cpan.org/dist/MooseX-Attributes-CachedF>
138 L<Moose>, L<MooseX::ClassAttribute>, L<MooseX::Storage>, L<Cache::FastMmap>,
141 =head1 COPYRIGHT & LICENSE
143 Copyright 2008 John Napiorkowski
145 This program is free software; you can redistribute it and/or modify it
146 under the same terms as Perl itself.
150 no Moose; 1; # End of MooseX::Attributes::Cached