From d4e4f57072e86b65624a93115f4fd41b15281781 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Cohen=20Arazi?= Date: Thu, 9 Apr 2026 12:48:59 -0300 Subject: [PATCH] Bug 42310: (follow-up) Bake cache invalidation into Koha::Object classes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When using the new Koha::Calendar::* classes for writing the CRUD API endpoints I noticed that in my attempt to keep C4::Calendar similar to what it was, I ended up with cache invalidation not always done in the right place. The holidays cache (keyed by {branchcode}_holidays) stores dates and their open/closed status, not titles or descriptions. This means only inserts and deletes need to invalidate it — metadata updates (title/description) do not. Following the pattern established by Koha::AuthorisedValue and Koha::ItemType, I added store() and delete() overrides to Koha::Calendar::SingleClosure and Koha::Calendar::Exception: - store() flushes the cache only on insert (!in_storage), not on updates, since the cache doesn't store titles or descriptions. - delete() always flushes, since removing a closure changes the cached set. This allowed removing the now-redundant _clear_cache calls from Koha::Calendar::add_single_closure, add_exception, delete_single_closure, delete_exception, and from C4::Calendar::ModSingleholiday and ModExceptionholiday. Signed-off-by: Tomás Cohen Arazi Signed-off-by: David Nind Signed-off-by: Martin Renvoize Signed-off-by: Tomás Cohen Arazi Signed-off-by: Pedro Amorim --- C4/Calendar.pm | 3 --- Koha/Calendar.pm | 19 ++-------------- Koha/Calendar/Exception.pm | 40 ++++++++++++++++++++++++++++++++++ Koha/Calendar/SingleClosure.pm | 40 ++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 20 deletions(-) diff --git a/C4/Calendar.pm b/C4/Calendar.pm index 74396b5743..54f422c916 100644 --- a/C4/Calendar.pm +++ b/C4/Calendar.pm @@ -400,7 +400,6 @@ sub ModSingleholiday { my $closure = Koha::Calendar::SingleClosures->search( { library_id => $self->{branchcode}, date => $date } )->next; if ($closure) { $closure->title( $options{title} )->description( $options{description} )->store; - Koha::Caches->get_instance()->clear_from_cache( $self->{branchcode} . '_holidays' ); } return $self; } @@ -434,7 +433,6 @@ sub ModExceptionholiday { my $exception = Koha::Calendar::Exceptions->search( { library_id => $self->{branchcode}, date => $date } )->next; if ($exception) { $exception->title( $options{title} )->description( $options{description} )->store; - Koha::Caches->get_instance()->clear_from_cache( $self->{branchcode} . '_holidays' ); } return $self; } @@ -491,7 +489,6 @@ sub delete_holiday { $cal->delete_repeating_closure( { day => $options{day}, month => $options{month} } ); } - $cal->_clear_cache; return $self; } diff --git a/Koha/Calendar.pm b/Koha/Calendar.pm index 6492a2ab75..81b766592f 100644 --- a/Koha/Calendar.pm +++ b/Koha/Calendar.pm @@ -435,16 +435,12 @@ sub add_repeating_closure { sub add_single_closure { my ( $self, $params ) = @_; - my $result = Koha::Calendar::SingleClosure->new( { library_id => $self->{branchcode}, %$params } )->store; - $self->_clear_cache; - return $result; + return Koha::Calendar::SingleClosure->new( { library_id => $self->{branchcode}, %$params } )->store; } sub add_exception { my ( $self, $params ) = @_; - my $result = Koha::Calendar::Exception->new( { library_id => $self->{branchcode}, %$params } )->store; - $self->_clear_cache; - return $result; + return Koha::Calendar::Exception->new( { library_id => $self->{branchcode}, %$params } )->store; } sub delete_weekly_closure { @@ -464,14 +460,12 @@ sub delete_repeating_closure { sub delete_single_closure { my ( $self, $params ) = @_; Koha::Calendar::SingleClosures->search( { library_id => $self->{branchcode}, date => $params->{date} } )->delete; - $self->_clear_cache; return $self; } sub delete_exception { my ( $self, $params ) = @_; Koha::Calendar::Exceptions->search( { library_id => $self->{branchcode}, date => $params->{date} } )->delete; - $self->_clear_cache; return $self; } @@ -536,11 +530,6 @@ sub copy_to { return 1; } -sub _clear_cache { - my ($self) = @_; - Koha::Caches->get_instance()->clear_from_cache( $self->{branchcode} . '_holidays' ); -} - 1; __END__ @@ -761,10 +750,6 @@ list of specified dates Copies all closure rules to another branch. Only copies future single closures and exceptions. -=head3 _clear_cache - -Clears the holidays cache for this branch. - =head2 Internal methods =head3 _init diff --git a/Koha/Calendar/Exception.pm b/Koha/Calendar/Exception.pm index c9cc342f56..b9d935420d 100644 --- a/Koha/Calendar/Exception.pm +++ b/Koha/Calendar/Exception.pm @@ -19,6 +19,7 @@ package Koha::Calendar::Exception; use Modern::Perl; +use Koha::Caches; use Koha::Database; use base qw(Koha::Object); @@ -29,6 +30,45 @@ Koha::Calendar::Exception - Koha closure exception (open override) Object class =head1 API +=head2 Class methods + +=head3 store + +Overloaded store method that clears the holidays cache on insert. + +Only flushes on insert (new exception), not on title/description updates, +because the holidays cache only stores dates and their open/closed status. + +=cut + +sub store { + my ($self) = @_; + + my $flush = !$self->in_storage; + + $self = $self->SUPER::store; + + if ($flush) { + Koha::Caches->get_instance()->clear_from_cache( $self->library_id . '_holidays' ); + } + + return $self; +} + +=head3 delete + +Overloaded delete method that clears the holidays cache. + +=cut + +sub delete { + my ($self) = @_; + my $library_id = $self->library_id; + $self->SUPER::delete; + Koha::Caches->get_instance()->clear_from_cache( $library_id . '_holidays' ); + return $self; +} + =head2 Internal methods =head3 _type diff --git a/Koha/Calendar/SingleClosure.pm b/Koha/Calendar/SingleClosure.pm index 52b01204f5..fa7817a1db 100644 --- a/Koha/Calendar/SingleClosure.pm +++ b/Koha/Calendar/SingleClosure.pm @@ -19,6 +19,7 @@ package Koha::Calendar::SingleClosure; use Modern::Perl; +use Koha::Caches; use Koha::Database; use base qw(Koha::Object); @@ -29,6 +30,45 @@ Koha::Calendar::SingleClosure - Koha single-date closure Object class =head1 API +=head2 Class methods + +=head3 store + +Overloaded store method that clears the holidays cache on insert. + +Only flushes on insert (new closure), not on title/description updates, +because the holidays cache only stores dates and their open/closed status. + +=cut + +sub store { + my ($self) = @_; + + my $flush = !$self->in_storage; + + $self = $self->SUPER::store; + + if ($flush) { + Koha::Caches->get_instance()->clear_from_cache( $self->library_id . '_holidays' ); + } + + return $self; +} + +=head3 delete + +Overloaded delete method that clears the holidays cache. + +=cut + +sub delete { + my ($self) = @_; + my $library_id = $self->library_id; + $self->SUPER::delete; + Koha::Caches->get_instance()->clear_from_cache( $library_id . '_holidays' ); + return $self; +} + =head2 Internal methods =head3 _type