From 67d19e3141500a3f8f287d8fdfec1315841ec0d5 Mon Sep 17 00:00:00 2001 From: David Gustafsson Date: Fri, 15 Mar 2024 21:12:33 +0100 Subject: [PATCH] Bug 36350: Add caching for Objects::find and Objects::search Add subclass of Koha::Objects that provides cached find and search. To test: 1) Ensure the following tests pass: - t/db_dependend/Koha/Libraries.t - t/db_dependent/Koha/ItemTypes.t - t/db_dependent/Koha/Patrons.t - t/db_dependent/Koha/Acquisition/Currencies.t Sponsored-by: Gothenburg University Library --- Koha/Acquisition/Currencies.pm | 4 +- Koha/Acquisition/Currency.pm | 2 +- Koha/ItemType.pm | 2 +- Koha/ItemTypes.pm | 2 +- Koha/Libraries.pm | 2 +- Koha/Library.pm | 2 +- Koha/Object.pm | 3 + Koha/Object/CachedExpiration.pm | 68 +++++++++++ Koha/Objects/Cached.pm | 92 +++++++++++++++ Koha/Objects/Cached/Base.pm | 119 +++++++++++++++++++ Koha/Patron.pm | 2 +- Koha/Patrons.pm | 2 +- t/db_dependent/Koha/Libraries.t | 203 +++++++++++++++++++++++++++++++- 13 files changed, 493 insertions(+), 10 deletions(-) create mode 100644 Koha/Object/CachedExpiration.pm create mode 100644 Koha/Objects/Cached.pm create mode 100644 Koha/Objects/Cached/Base.pm diff --git a/Koha/Acquisition/Currencies.pm b/Koha/Acquisition/Currencies.pm index 42f1d2ff200..09848688898 100644 --- a/Koha/Acquisition/Currencies.pm +++ b/Koha/Acquisition/Currencies.pm @@ -22,7 +22,7 @@ use Koha::Database; use Koha::Acquisition::Currency; -use base qw(Koha::Objects); +use base qw(Koha::Objects::Cached); =head1 NAME @@ -40,7 +40,7 @@ Koha::Acquisition::Currencies - Koha Acquisition Currency Object set class sub get_active { my ( $self ) = @_; - return $self->SUPER::search( { active => 1 } )->next; + return $self->search( { active => 1 } )->next; } =head3 _type diff --git a/Koha/Acquisition/Currency.pm b/Koha/Acquisition/Currency.pm index bf1daaf1651..6e3841b31eb 100644 --- a/Koha/Acquisition/Currency.pm +++ b/Koha/Acquisition/Currency.pm @@ -20,7 +20,7 @@ use Modern::Perl; use Koha::Database; -use base qw(Koha::Object); +use base qw(Koha::Object::CachedExpiration); =head1 NAME diff --git a/Koha/ItemType.pm b/Koha/ItemType.pm index fb88518c257..67b87cdbcb4 100644 --- a/Koha/ItemType.pm +++ b/Koha/ItemType.pm @@ -24,7 +24,7 @@ use Koha::Database; use Koha::CirculationRules; use Koha::Localizations; -use base qw(Koha::Object Koha::Object::Limit::Library); +use base qw(Koha::Object::CachedExpiration Koha::Object::Limit::Library); my $cache = Koha::Caches->get_instance(); diff --git a/Koha/ItemTypes.pm b/Koha/ItemTypes.pm index 13caa648a81..9498a90bf91 100644 --- a/Koha/ItemTypes.pm +++ b/Koha/ItemTypes.pm @@ -23,7 +23,7 @@ use C4::Languages; use Koha::Database; use Koha::ItemType; -use base qw(Koha::Objects Koha::Objects::Limit::Library); +use base qw(Koha::Objects::Cached Koha::Objects::Limit::Library); =head1 NAME diff --git a/Koha/Libraries.pm b/Koha/Libraries.pm index 20ceb8aa348..2ec9020c1da 100644 --- a/Koha/Libraries.pm +++ b/Koha/Libraries.pm @@ -29,7 +29,7 @@ use Koha::Items; use Koha::Library; use Koha::Patrons; -use base qw(Koha::Objects); +use base qw(Koha::Objects::Cached); =head1 NAME diff --git a/Koha/Library.pm b/Koha/Library.pm index decfa136170..fb771c341ed 100644 --- a/Koha/Library.pm +++ b/Koha/Library.pm @@ -27,7 +27,7 @@ use Koha::Database; use Koha::StockRotationStages; use Koha::SMTP::Servers; -use base qw(Koha::Object); +use base qw(Koha::Object::CachedExpiration); my $cache = Koha::Caches->get_instance(); diff --git a/Koha/Object.pm b/Koha/Object.pm index 560dccaff25..5b5d9ed1dc1 100644 --- a/Koha/Object.pm +++ b/Koha/Object.pm @@ -945,6 +945,9 @@ sub AUTOLOAD { my $accessor = sub { my $self = shift; if (@_) { + if ($self->isa("Koha::Object::CachedExpiration")) { + $self->_objects_cache_expire(); + } $self->_result()->set_column( $method, @_ ); return $self; } else { diff --git a/Koha/Object/CachedExpiration.pm b/Koha/Object/CachedExpiration.pm new file mode 100644 index 00000000000..ca57ca4b5f6 --- /dev/null +++ b/Koha/Object/CachedExpiration.pm @@ -0,0 +1,68 @@ +package Koha::Object::CachedExpiration; + +use parent qw( Koha::Object Koha::Objects::Cached::Base ); +use Modern::Perl; +use Data::Dumper; + +=head1 NAME + +Koha::Object::CachedExpiration + +=head1 SYNOPSIS + + package Koha::Foo; + + use parent qw( Koha::Object::CachedExpiration ); + +=head1 API + +=head2 Class Methods + +=cut + +=head3 delete + +Overloaded I method for cache expiration + +=cut + +sub delete { + my ($self) = @_; + $self->_objects_cache_expire(); + $self->SUPER::delete(); +} + +=head3 store + +Overloaded I method for cache expiration + +=cut + +sub store { + my ($self) = @_; + $self->_objects_cache_expire(); + $self->SUPER::store(); +} + +=head2 Internal methods + +=head3 _objects_cache_expire + +=cut + +sub _objects_cache_expire { + my ($self) = @_; + if ($self->_result->in_storage) { + my @primary_keys = $self->_result->id; + my $ids_cache_key = $self->_objects_cache_cache_key('find', @primary_keys); + $self->_objects_cache_clear($ids_cache_key); + } +} + +=head1 AUTHOR + +David Gustafsson + +=cut + +1; diff --git a/Koha/Objects/Cached.pm b/Koha/Objects/Cached.pm new file mode 100644 index 00000000000..e469228d2a3 --- /dev/null +++ b/Koha/Objects/Cached.pm @@ -0,0 +1,92 @@ +package Koha::Objects::Cached; + +use parent qw( Koha::Objects Koha::Objects::Cached::Base ); +use Modern::Perl; + +=head1 NAME + +Koha::Object::Cached + +=head1 SYNOPSIS + + package Koha::Foo; + + use parent qw( Koha:Objects::Cached ); + +=head1 API + +=head2 Class Methods + +=cut + +=head3 find + +Overloaded I method that provides in memory caching by arguments. + +=cut + +sub find { + my ($class, @args) = @_; + my $object; + my $cache_key = $class->_objects_cache_cache_key('find', @args); + + # Store in the args bucket if has column value condiditions + # or for some reason the attributes argument is provided + if (ref $args[0] eq 'HASH' || @args > 1 && ref $args[$#args] eq 'HASH') { + $object = $class->_objects_cache_get($cache_key, 'args'); + unless ($object) { + $object = $class->SUPER::find(@args); + $class->_objects_cache_set($cache_key, $object, 'args'); + } + } + else { + $object = $class->_objects_cache_get($cache_key, 'ids'); + unless ($object) { + $object = $class->SUPER::find(@args); + $class->_objects_cache_set($cache_key, $object, 'ids'); + } + } + return $object; +} + +=head3 search + +Overloaded I method that provides in memory caching by arguments. + +=cut + +sub search { + my ($class, $cond, $attrs) = @_; + my $has_resultset = ref($class) && $class->{_resultset}; + $attrs //= {}; + $attrs->{cache} //= 1 unless $has_resultset; + + + if ($has_resultset || !$attrs->{cache}) { + return $class->SUPER::search($cond, $attrs); + } + else { + my @args; + push(@args , $cond) if defined $cond; + # Don't include if only contains "cache" key + push(@args, $attrs) unless keys %{$attrs} == 1; + my $cache_key = $class->_objects_cache_cache_key('search', @args); + my $objects = $class->_objects_cache_get($cache_key, 'args'); + if ($objects) { + $objects->reset; + } + else { + $objects = $class->SUPER::search($cond, $attrs); + $class->_objects_cache_set($cache_key, $objects, 'args'); + } + return $objects; + } +} + +=head1 AUTHOR + +David Gustafsson + +=cut + +1; diff --git a/Koha/Objects/Cached/Base.pm b/Koha/Objects/Cached/Base.pm new file mode 100644 index 00000000000..035335b677b --- /dev/null +++ b/Koha/Objects/Cached/Base.pm @@ -0,0 +1,119 @@ +package Koha::Objects::Cached::Base; + +use Modern::Perl; +use Digest::MD5 qw( md5_hex ); +use JSON; +use Data::Dumper; + +use Koha::Cache::Memory::Lite; + +=head1 NAME + +Koha::Objects::Cached::Base + +=head2 Internal methods + +=head3 _objects_cache_cache_key + +=cut + +sub _objects_cache_cache_key { + my ($self, @args) = @_; + if (@args == 2 && !ref $args[1]) { + return join(':', @args); + } + # JSON is much faster than Data::Dumper but throws + # an exception for certain perl data strucures + # (non boolean scalar refs for example which can + # be used in DBIx conditions) + my $cache_key = eval { md5_hex(JSON->new->canonical(1)->encode(\@args)) }; + if ($cache_key) { + return $cache_key; + } else { + local $Data::Dumper::Sortkeys = 1; + return md5_hex(Dumper(\@args)); + } +} + +=head3 _objects_cache_bucket_key + +=cut + +sub _objects_cache_bucket_key { + my ($self, $bucket) = @_; + my %buckets = ( + 'ids' => 'ObjectsCachedIds', + 'args' => 'ObjectsCachedArgs' + ); + unless (exists $buckets{$bucket}) { + croak "Invalid cache bucket $bucket"; + } + return $self->_type . $buckets{$bucket}; + + +} + +=head3 _objects_cache_bucket + +=cut + +sub _objects_cache_bucket { + my ($self, $bucket) = @_; + my $memory_cache = Koha::Cache::Memory::Lite->get_instance(); + my $bucket_key = $self->_objects_cache_bucket_key($bucket); + my $cache = $memory_cache->get_from_cache($bucket_key); + unless ($cache) { + $cache = {}; + $memory_cache->set_in_cache($bucket_key, $cache); + } + return $cache; +} + +=head3 _objects_cache_get + +=cut + +sub _objects_cache_get { + my ($self, $cache_key, $bucket) = @_; + my $cache = $self->_objects_cache_bucket($bucket); + return exists $cache->{$cache_key} ? $cache->{$cache_key} : undef; +} + +=head3 _objects_cache_set + +=cut + +sub _objects_cache_set { + my ($self, $cache_key, $object, $bucket) = @_; + my $cache = $self->_objects_cache_bucket($bucket); + $cache->{$cache_key} = $object; +} + +=head3 _objects_cache_clear + +=cut + +sub _objects_cache_clear { + my ($self, $ids_cache_key) = @_; + my $memory_cache = Koha::Cache::Memory::Lite->get_instance(); + if ($ids_cache_key) { + my $cache = $self->_objects_cache_bucket('ids'); + delete $cache->{$ids_cache_key}; + } + else { + $memory_cache->clear_from_cache( + $self->_objects_cache_bucket_key('ids') + ); + } + $memory_cache->clear_from_cache( + $self->_objects_cache_bucket_key('args') + ); +} + +=head1 AUTHOR + +David Gustafsson + +=cut + +1; diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 5b0bd9ea7f5..5d144873527 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -66,7 +66,7 @@ use Koha::Subscription::Routinglists; use Koha::Token; use Koha::Virtualshelves; -use base qw(Koha::Object); +use base qw(Koha::Object::CachedExpiration); use constant ADMINISTRATIVE_LOCKOUT => -1; diff --git a/Koha/Patrons.pm b/Koha/Patrons.pm index 92d3918363a..edfcc42dc88 100644 --- a/Koha/Patrons.pm +++ b/Koha/Patrons.pm @@ -29,7 +29,7 @@ use Koha::Patron; use Koha::Exceptions::Patron; use Koha::Patron::Categories; -use base qw(Koha::Objects); +use base qw(Koha::Objects::Cached); =head1 NAME diff --git a/t/db_dependent/Koha/Libraries.t b/t/db_dependent/Koha/Libraries.t index 5c746d679d5..947eb20d3a8 100755 --- a/t/db_dependent/Koha/Libraries.t +++ b/t/db_dependent/Koha/Libraries.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 13; +use Test::More tests => 14; use C4::Biblio; use C4::Context; @@ -32,6 +32,7 @@ use Koha::Library; use Koha::Libraries; use Koha::Database; use Koha::CirculationRules; +use Koha::Cache::Memory::Lite; use t::lib::Mocks; use t::lib::TestBuilder; @@ -413,3 +414,203 @@ subtest 'outgoing_transfers' => sub { $schema->storage->txn_rollback; }; + +subtest 'cache tests' => sub { + plan tests => 24; + + $schema->storage->txn_begin; + + my $library1 = $builder->build_object( + { + class => 'Koha::Libraries', + value => { + branchname => 'My library' + } + } + ); + my $library2 = $builder->build_object( + { + class => 'Koha::Libraries', + value => { + branchname => 'My other library' + } + } + ); + + # Since build_object calls find internally + # we first have to flush the cache since these + # objects are now already cached + Koha::Cache::Memory::Lite->get_instance()->flush(); + + my $ids_bucket = Koha::Libraries->_objects_cache_bucket('ids'); + ok(!%{$ids_bucket}, 'Find cache has been cleared after flushing memory cache'); + + my $cached_library = Koha::Libraries->find($library1->branchcode); + Koha::Libraries->find($library2->branchcode); + + my $cached_library_ids_bucket = Koha::Libraries->_objects_cache_get( + Koha::Libraries->_objects_cache_cache_key('find', $library1->branchcode), + 'ids' + ); + is (ref($cached_library_ids_bucket), 'Koha::Library', 'Library should be cached after previously have been fetched'); + is ($cached_library_ids_bucket->branchcode, $library1->branchcode, 'The cashed library should be the expected library'); + + # Set property of library by-passing the cache expiration logic + # so we can veriy library is not only present in cache by also + # properly retrieved in find + $cached_library->_result->set_column('branchname', 'My library in cache'); + $cached_library = Koha::Libraries->find($library1->branchcode); + is ( + $cached_library->branchname, + 'My library in cache', + 'The cashed library returned by find should be the cached library' + ); + + $library1->branchname('My expired library'); + $cached_library_ids_bucket = Koha::Libraries->_objects_cache_get( + Koha::Libraries->_objects_cache_cache_key('find', $library1->branchcode), + 'ids' + ); + is ( + $cached_library_ids_bucket, + undef, + 'Cache has been expired after changing library property' + ); + + $cached_library = Koha::Libraries->_objects_cache_get( + Koha::Libraries->_objects_cache_cache_key('find', $library2->branchcode), + 'ids' + ); + is ( + ref($cached_library), + 'Koha::Library', 'Cache should still contain other cached libraries' + ); + + my $stored_library = Koha::Libraries->find($library1->branchcode); + is ( + $stored_library->branchname, + 'My library', + 'Library is fetched from database after cache has been expired' + ); + + $cached_library = Koha::Libraries->_objects_cache_get( + Koha::Libraries->_objects_cache_cache_key('find', $library1->branchcode), + 'ids' + ); + is ( + ref($cached_library), + 'Koha::Library', + 'Library should be cached after previously have been fetched' + ); + + $library1->store(); + $cached_library = Koha::Libraries->_objects_cache_get( + Koha::Libraries->_objects_cache_cache_key('find', $library1->branchcode), + 'ids' + ); + is ($cached_library, undef, 'Cache has been expired after saving library to database'); + + Koha::Libraries->find($library1->branchcode, { key => 'primary' }); + $cached_library = Koha::Libraries->_objects_cache_get( + Koha::Libraries->_objects_cache_cache_key('find', $library1->branchcode, { key => 'primary' }), + 'args' + ); + is ( + ref($cached_library), + 'Koha::Library', + 'The args cache bucket should be used if find is called with the attrs argument' + ); + my $cached_value; + + for my $method ('find', 'search') { + + Koha::Libraries->$method({ branchcode => $library1->branchcode }); + Koha::Libraries->$method({ branchcode => $library2->branchcode }); + + $cached_value = Koha::Libraries->_objects_cache_get( + Koha::Libraries->_objects_cache_cache_key($method, { branchcode => $library1->branchcode }), + 'args' + ); + $cached_library = $method eq 'search' ? $cached_value->next : $cached_value; + is ( + ref($cached_library), + 'Koha::Library', + 'Library should be cached after previously have been fetched, and use the args cache bucket ' . ($method eq 'find' ? 'if find was called with column conditions' : 'if search was called') + ); + + $cached_library->_result->set_column('branchname', 'My library in cache'); + $cached_value = Koha::Libraries->$method({ branchcode => $library1->branchcode }); + $cached_library = $method eq 'search' ? $cached_value->next : $cached_value; + is ( + $cached_library->branchname, + 'My library in cache', + "The cashed library returned by $method should be the cached library, using the args cache bucket" + ); + + $cached_library->branchname('My expired library'); + $cached_value = Koha::Libraries->_objects_cache_get( + Koha::Libraries->_objects_cache_cache_key($method, { branchcode => $library1->branchcode }), + 'args' + ); + is ( + $cached_value, + undef, + 'Cache has been expired after changing branchname, using the args cache bucket' + ); + + $cached_value = Koha::Libraries->_objects_cache_get( + Koha::Libraries->_objects_cache_cache_key($method, { branchcode => $library2->branchcode }), + 'args' + ); + is ( + $cached_value, + undef, + 'The whole args cache bucket has been emptied after triggering cache expiration' + ); + } + + Koha::Libraries->find($library2->branchcode); + + my $libraries = Koha::Libraries->search({ branchcode => $library1->branchcode }); + $libraries->next; + is($libraries->next, undef, 'Cached libraries search result iterator has been exhausted'); + + $libraries = Koha::Libraries->search({ branchcode => $library1->branchcode }); + is(ref($libraries->next), 'Koha::Library', 'Cached libraries search result iterator has been reset'); + + + my $cached_libraries = Koha::Libraries->_objects_cache_get( + Koha::Libraries->_objects_cache_cache_key('search', { 'branchcode' => $library1->branchcode }), + 'args' + ); + is ( + ref($cached_libraries), + 'Koha::Libraries', + 'Libraries should be cached after previously have been fetched' + ); + + $library1->delete(); + $cached_libraries = Koha::Libraries->_objects_cache_get( + Koha::Libraries->_objects_cache_cache_key('search', { 'branchcode' => $library1->branchcode }), + 'args' + ); + is ($cached_libraries, undef, 'Search cache has been expired after deleting library'); + + $cached_library = Koha::Libraries->_objects_cache_get( + Koha::Libraries->_objects_cache_cache_key('find', $library2->branchcode), + 'ids' + ); + is ( + ref($cached_library), + 'Koha::Library', + 'Library should be cached after previously have been fetched' + ); + $library2->delete(); + $cached_library = Koha::Libraries->_objects_cache_get( + Koha::Libraries->_objects_cache_cache_key('find', $library2->branchcode), + 'ids' + ); + is ($cached_library, undef, 'Find cache has been expired after deleting library'); + + $schema->storage->txn_rollback; +} -- 2.43.0