NAME

Flickr::Roles::Caching - Caching behaviors for the Flickr::Tools

VERSION

CPAN: 1.22

Development: 1.22_01

SYNOPSIS

in consuming package

package Flickr::Tools::of-some-kind;

use Flickr::API::of-some-similar-kind;

with qw(Flickr::Roles::Caching);

sub getSomething-from-flickr {

  my ($self, $args) = @_;
  my $pre_expire = 0;

  $self->_set_cache_hit(1);

  if (defined($args->{clear_cache}) and $args->{clear_cache}) { $pre_expire = 1; }

  $self->_set_cache_key('meaningful-cache-key');

  $data = $self->_cache->get($self->cache_key, expire_if => sub { $pre_expire } );
  if (!defined $data) {
      $data = $self->{_api}->some-api-call;
      $self->_set_cache_hit(0);
      $self->_cache->set( $self->cache_key, $data, $self->cache_duration);
  }

  return $data;

}

in calling script

use Flickr::Tools::of-some-kind;

my $tool = Flickr::Tools::of-some-kind->new(cache_duration => 3600, ...);

say $tool->cache_duration; # will print 3600

$tool->cache_duration(300); # set the cache duration to 300 seconds

$tool->getSomething-from-flickr;

if ($tool->cache_hit) {

   say "gotSomething from cache";

 }
 else {

   say "had to go to Flickr to getSomthing";

 }

 $tool->getSomething-else-from-flickr(clear_cache => 1, arg1 => val1, arg2 => val2...);

DESCRIPTION

This module adds a caching role for the Flickr::Tools packages.

PUBLIC ATTRIBUTES

cache_duration

The duration, in seconds, for the cache to keep the information. When the cache expires, it will re-fetch the data from Flickr if you request it again.

cache_hit

Returns whether the last cache get was from cache or fetched into the cache.

cache_key

This is the last used key into the cache. It is here, but it probably doesn't return what you think it will.

PRIVATE ATTRIBUTES

_cache

This is a reference to a CHI cache kept in the Flickr::Tool object.

DIAGNOSTICS

CONFIGURATION AND ENVIRONMENT

DEPENDENCIES

CHI, Perl 5.10 and Moo.

INCOMPATIBILITIES

None known of, yet.

BUGS AND LIMITATIONS

Yes

AUTHOR

Louis B. Moore <lbmoore@cpan.org>

LICENSE AND COPYRIGHT

Copyright (C) 2015 Louis B. Moore <lbmoore@cpan.org>

This program is released under the Artistic License 2.0 by The Perl Foundation. http://www.perlfoundation.org/artistic_license_2_0