Name

File::DataClass::Cache - Accessors and mutators for the cache object

Synopsis

   package File::DataClass::Schema;

   use Moo;
   use File::DataClass::Types qw(Cache);
   use File::DataClass::Cache;

   has 'cache'            => is => 'lazy', isa => Cache;

   has 'cache_attributes' => is => 'ro', isa => 'HashRef',
      default             => sub { return {} };

   my $_cache_objects = {};

   sub _build_cache {
      my $self  = shift; (my $ns = lc __PACKAGE__) =~ s{ :: }{-}gmx; my $cache;

      my $attrs = { cache_attributes => { %{ $self->cache_attributes } },
                    builder          => $self };

      $ns    = $attrs->{cache_attributes}->{namespace} ||= $ns;
      exists $_cache_objects->{ $ns } and return $_cache_objects->{ $ns };
      $self->cache_class eq 'none' and return Class::Null->new;
      $attrs->{cache_attributes}->{share_file}
           ||= NUL.$self->tempdir->catfile( "${ns}.dat" );

      return $_cache_objects->{ $ns } = $self->cache_class->new( $attrs );
   }

Description

Adds meta data and compound keys to the CHI caching API. In instance of this class is created by File::DataClass::Schema

Configuration and Environment

The class defines these attributes

cache

An instance of the CHI cache object

cache_attributes

A hash ref passed to the CHI constructor

cache_class

The class name of the cache object, defaults to CHI

log

Log object which defaults to Class::Null

Subroutines/Methods

BUILDARGS

Constructs the attribute hash passed to the constructor method.

get

   ($data, $meta) = $schema->cache->get( $key );

Returns the data and metadata associated with the given key. If no cache entry exists the data returned is undef and the metadata is a hash ref with a key of mtime and a value of 0

get_by_paths

   ($data, $meta, $newest) = $schema->cache->get_by_paths( $paths );

The paths passed in the array ref are concatenated to form a compound key. The CHI cache entry is fetched and the data and meta data returned along with the modification time of the newest file in the list of paths

get_mtime

   $mod_time = $schema->cache->get_mtime( $key );

Returns the mod time of a file if it's in the cache. Returns undef if it is not. Returns zero if the filesystem was checked and the file did not exist

remove

   $schema->cache->remove( $key );

Removes the CHI cache entry for the given key

set

   ($data, $meta) = $schema->cache->set( $key, $data, $meta );

Sets the CHI cache entry for the given key

set_by_paths

   ($data, $meta) = $schema->cache->set_by_paths( $paths, $data, $meta );

Set the CHI cache entry for the compound key formed from the array ref $paths

set_mtime

   $schema->cache->set_mtime( $key, $value );

Sets the mod time in the cache for the given key. Setting the mod time to zero means the filesystem was checked and the file did not exist

_get_key_and_newest

   ($key, $newest) = $schema->cache->_get_key_and_newest( $paths );

Creates a key from the array ref of path names and also returns the most recent mod time. Will return undef for newest if the cache entry is invalid

Diagnostics

None

Dependencies

CHI

Incompatibilities

There are no known incompatibilities in this module

Bugs and Limitations

There are no known bugs in this module. Please report problems to the address below. Patches are welcome

Acknowledgements

Larry Wall - For the Perl programming language

Author

Peter Flanigan, <pjfl@cpan.org>

License and Copyright

Copyright (c) 2017 Peter Flanigan. All rights reserved

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

This program is distributed in the hope that it will be useful, but WITHOUT WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE