Bug 42310: (follow-up) Bake cache invalidation into Koha::Object classes

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 <tomascohen@theke.io>
Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Martin Renvoize <martin.renvoize@openfifth.co.uk>
Signed-off-by: Tomás Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Pedro Amorim <pedro.amorim@openfifth.co.uk>
This commit is contained in:
Tomás Cohen Arazi 2026-04-09 12:48:59 -03:00 committed by Pedro Amorim
parent cb7968387a
commit d4e4f57072
No known key found for this signature in database
GPG Key ID: 06B75BA7F3894D55
4 changed files with 82 additions and 20 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -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

View File

@ -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