[Koha-bugs] [Bug 32476] Add caching for relatively expensive patron methods
bugzilla-daemon at bugs.koha-community.org
bugzilla-daemon at bugs.koha-community.org
Thu Dec 22 01:06:17 CET 2022
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=32476
--- Comment #14 from David Cook <dcook at prosentient.com.au> ---
I did have some little mistakes in my previous example. Hopefully this is more
illustrative.
You don't need to use closures and personally I'm not a fan of "maybe_*"
functions.
--
Koha::Patron
sub is_expired {
my ($self, $params) = @_;
( $param->{cache} ) ? return $self->cache({ method => 'is_expired' }) :
$self->cache({ method => 'is_expired', reset => 1 });
return ( $self->dateexpiry &&
$self->dateexpiry !~ '^9999' &&
dt_from_string( $self->dateexpiry ) < dt_from_string->truncate( to =>
'day' ) ) ? 1 : 0;
}
sub has_overdues {
my ($self, $params) = @_;
( $param->{cache} ) ? return $self->cache({ method => 'has_overdues' }) :
$self->cache({ method => 'has_overdues', reset => 1 });
my $dtf = Koha::Database->new->schema->storage->datetime_parser;
return $self->_result->issues->search({ date_due => { '<' =>
$dtf->format_datetime( dt_from_string() ) } })->count;
}
Koha::Object
sub cache {
my ($self, $args) = @_;
my $method = $args->{method};
my $reset = $args->{reset};
my $value;
if ($method){
if ($reset){
delete $self->{_cache}->{$method};
}
else {
if ( defined $self->{_cache}->{$method } ){
$value = $self->{_cache}->{$method };
}
else {
$value = $self->$method();
#Set cache
$self->{_cache}->{$method} = $value;
}
}
}
return $value;
}
--
You are receiving this mail because:
You are watching all bug changes.
More information about the Koha-bugs
mailing list