package MooseX::Attribute::Cached;

use Moose;

=head1 NAME

MooseX::Attribute::Cached; Cache your Moose Attribute Value

=head2 AUTHORITY

cpan:JJNAPIORK

=cut

our $AUTHORITY = 'cpan:JJNAPIORK';

=head1 VERSION

Version 0.01

=cut

our $VERSION = '0.01';

=head1 SYNOPSIS

	package MyApp;
	
	use Moose;
	use MooseX::Attribute::Cached;

	## Cache Storage Options Declared Manually
	has 'shared_key_1' => (
		traits => ['Cached'],
		cache_storage => ['Memcached' => {
			'servers' => [ "10.0.0.15:11211", "10.0.0.15:11212"],
		}], 
		%other_attribute_options_1,
	);
	
	## This attribute get's it's cache storage from a method, which can be
	## called from a lazy attribute or as a package method
	has 'shared_key_2' => (
		traits => ['Cached'],
		storage_key=>'something_different_shared_key_2',
		%other_attribute_options_2,
	);
	
	## Here's the provider for the attributes storage.  Basically you need to
	## return the storage provider name and it's instantiation args.
	sub _cache_storage_options_shared_key_2 {
		## If the calling attribute is lazy get's $self, otherwise we
		## gets __PACKAGE__.  Getting $self could be useful if you are reading
		## the cache options from something like your config object, etc.
		my $self = shift @_;
		return [
			'Memcached' => {
				'servers' => [
					"10.0.0.15:11211", 
					"10.0.0.15:11212", 
					"/var/sock/memcached",
					"10.0.0.17:11211",
					[ "10.0.0.17:11211", 3 ],
				],
				'debug' => 0,
				'compress_threshold' => 10_000,
			},
		];
	}
	
	## <Rest of your Class Definition>
	
	1;

=head1 DESCRIPTION

Store the value of your Moose Attributes in a cached storage.  The purpose of 
this is to faciliate sharing of attribute values over various processes or 
every across machines, presuming you are using a distributed cache, 
like Memcached.  All instances with access to the same cache will share and 
update a common value.  That way:

1) All instances share the same attribute slot.  Updates made by one 
attribute are seen by all instances, even on different servers as long as
they share a distributed caching system (such as Memcached).
	
2) It can be a sort of 'persistance lite' although I highly recommend using
a real persistance system, such as a database or L<MooseX::Storage>.
	
3) You could probably use this as a sort of expensive class attribute, but
you will likely be more happy with L<MooseX::ClassAttribute>

At this time, this supports two Cachings systems, Memcached and FastMmap.  If
you have need for other cache types, adding a driver for it should be easy, so
please send your patches and test cases.

=head1 AUTHOR

John Napiorkowski, C<< <john.napiorkowski at takkle.com> >>

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc MooseX::Attributes::Cached

You can also look for information at:

=over 4

=item * RT: CPAN's request tracker

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-Attributes-Cached>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/MooseX-Attributes-Cached>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/MooseX-Attributes-Cached>

=item * Search CPAN

L<http://search.cpan.org/dist/MooseX-Attributes-CachedF>

=back

=head1 SEE ALSO

L<Moose>, L<MooseX::ClassAttribute>, L<MooseX::Storage>, L<Cache::FastMmap>,
L<Cache::Memcached>

=head1 COPYRIGHT & LICENSE

Copyright 2008 John Napiorkowski, all rights reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut

no Moose; 1; # End of MooseX::Attributes::Cached
