Class::Simple::Readonly::Cached - cache messages to an object
Version 0.10
A sub-class of Class::Simple which caches calls to read the status of an object that are otherwise expensive.
It is up to the caller to maintain the cache if the object comes out of sync with the cache, for example by changing its state.
You can use this class to create a caching layer to an object of any class that works on objects which doesn't change its state based on input:
use Class::Simple::Readonly::Cached;
my $obj = Class::Simple->new();
$obj->val('foo');
$obj = Class::Simple::Readonly::Cached->new(object => $obj, cache => {});
my $val = $obj->val();
print "$val\n"; # Prints "foo"
#... set $obj to be some other class which will take an argument 'a',
# with a value 'b'
$val = $obj->val(a => 'b');
Creates a Class::Simple::Readonly::Cached object.
It takes one mandatory parameter: cache, which is either an object which understands clear(), get() and set() calls, such as an CHI object; or is a reference to a hash where the return values are to be stored.
It takes one optional argument: object, which is an object which is taken to be the object to be cached. If not given, an object of the class Class::Simple is instantiated and that is used.
use Gedcom;
my %hash;
my $person = Gedcom::Person->new();
# ...Set up some data
my $object = Class::Simple::Readonly::Cached(object => $person, cache => \%hash);
my $father1 = $object->father(); # Will call gedcom->father() to get the person's father
my $father2 = $object->father(); # Will retrieve the father from the cache without calling person->father()
Takes one optional argument: quiet, if you attempt to cache an object that is already cached, rather than create another copy you receive a warning and the previous cached copy is returned. The 'quiet' option, when non-zero, silences the warning.
Return the encapsulated object
Returns the state of the object
print Data::Dumper->new([$obj->state()])->Dump();
Returns if the embedded object can handle a message
Returns if the embedded object is the given type of object
Nigel Horne, <njh at bandsman.co.uk>
Doesn't work with Memoize.
Please report any bugs or feature requests to https://github.com/nigelhorne/Class-Simple-Readonly-Cached/issues. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
You can find documentation for this module with the perldoc command.
perldoc Class::Simple::Readonly::Cached
You can also look for information at:
-
MetaCPAN
-
Source Repository
-
CPANTS
http://cpants.cpanauthors.org/dist/Class-Simple-Readonly-Cached
-
CPAN Testers' Matrix
http://matrix.cpantesters.org/?dist=Class-Simple-Readonly-Cached
-
CPAN Ratings
-
CPAN Testers Dependencies
http://deps.cpantesters.org/?module=Class::Simple::Readonly::Cached
-
Search CPAN
Author Nigel Horne: njh@bandsman.co.uk
Copyright (C) 2019-2024 Nigel Horne
Usage is subject to licence terms. The licence terms of this software are as follows: Personal single user, single computer use: GPL2 All other users (including Commercial, Charity, Educational, Government) must apply in writing for a licence for use from Nigel Horne at the above e-mail.