Lines 516-532
subtest 'cache tests' => sub {
Link Here
|
516 |
my $library1 = $builder->build_object( |
516 |
my $library1 = $builder->build_object( |
517 |
{ |
517 |
{ |
518 |
class => 'Koha::Libraries', |
518 |
class => 'Koha::Libraries', |
519 |
value => { |
519 |
value => { branchname => 'My library' } |
520 |
branchname => 'My library' |
|
|
521 |
} |
522 |
} |
520 |
} |
523 |
); |
521 |
); |
524 |
my $library2 = $builder->build_object( |
522 |
my $library2 = $builder->build_object( |
525 |
{ |
523 |
{ |
526 |
class => 'Koha::Libraries', |
524 |
class => 'Koha::Libraries', |
527 |
value => { |
525 |
value => { branchname => 'My other library' } |
528 |
branchname => 'My other library' |
|
|
529 |
} |
530 |
} |
526 |
} |
531 |
); |
527 |
); |
532 |
|
528 |
|
Lines 536-559
subtest 'cache tests' => sub {
Link Here
|
536 |
Koha::Cache::Memory::Lite->get_instance()->flush(); |
532 |
Koha::Cache::Memory::Lite->get_instance()->flush(); |
537 |
|
533 |
|
538 |
my $ids_bucket = Koha::Libraries->_objects_cache_bucket('ids'); |
534 |
my $ids_bucket = Koha::Libraries->_objects_cache_bucket('ids'); |
539 |
ok(!%{$ids_bucket}, 'Find cache has been cleared after flushing memory cache'); |
535 |
ok( !%{$ids_bucket}, 'Find cache has been cleared after flushing memory cache' ); |
540 |
|
536 |
|
541 |
my $cached_library = Koha::Libraries->find($library1->branchcode); |
537 |
my $cached_library = Koha::Libraries->find( $library1->branchcode ); |
542 |
Koha::Libraries->find($library2->branchcode); |
538 |
Koha::Libraries->find( $library2->branchcode ); |
543 |
|
539 |
|
544 |
my $cached_library_ids_bucket = Koha::Libraries->_objects_cache_get( |
540 |
my $cached_library_ids_bucket = Koha::Libraries->_objects_cache_get( |
545 |
Koha::Libraries->_objects_cache_cache_key('find', $library1->branchcode), |
541 |
Koha::Libraries->_objects_cache_cache_key( 'find', $library1->branchcode ), |
546 |
'ids' |
542 |
'ids' |
547 |
); |
543 |
); |
548 |
is (ref($cached_library_ids_bucket), 'Koha::Library', 'Library should be cached after previously have been fetched'); |
544 |
is( |
549 |
is ($cached_library_ids_bucket->branchcode, $library1->branchcode, 'The cashed library should be the expected library'); |
545 |
ref($cached_library_ids_bucket), 'Koha::Library', |
|
|
546 |
'Library should be cached after previously have been fetched' |
547 |
); |
548 |
is( |
549 |
$cached_library_ids_bucket->branchcode, $library1->branchcode, |
550 |
'The cashed library should be the expected library' |
551 |
); |
550 |
|
552 |
|
551 |
# Set property of library by-passing the cache expiration logic |
553 |
# Set property of library by-passing the cache expiration logic |
552 |
# so we can veriy library is not only present in cache by also |
554 |
# so we can veriy library is not only present in cache by also |
553 |
# properly retrieved in find |
555 |
# properly retrieved in find |
554 |
$cached_library->_result->set_column('branchname', 'My library in cache'); |
556 |
$cached_library->_result->set_column( 'branchname', 'My library in cache' ); |
555 |
$cached_library = Koha::Libraries->find($library1->branchcode); |
557 |
$cached_library = Koha::Libraries->find( $library1->branchcode ); |
556 |
is ( |
558 |
is( |
557 |
$cached_library->branchname, |
559 |
$cached_library->branchname, |
558 |
'My library in cache', |
560 |
'My library in cache', |
559 |
'The cashed library returned by find should be the cached library' |
561 |
'The cashed library returned by find should be the cached library' |
Lines 561-596
subtest 'cache tests' => sub {
Link Here
|
561 |
|
563 |
|
562 |
$library1->branchname('My expired library'); |
564 |
$library1->branchname('My expired library'); |
563 |
$cached_library_ids_bucket = Koha::Libraries->_objects_cache_get( |
565 |
$cached_library_ids_bucket = Koha::Libraries->_objects_cache_get( |
564 |
Koha::Libraries->_objects_cache_cache_key('find', $library1->branchcode), |
566 |
Koha::Libraries->_objects_cache_cache_key( 'find', $library1->branchcode ), |
565 |
'ids' |
567 |
'ids' |
566 |
); |
568 |
); |
567 |
is ( |
569 |
is( |
568 |
$cached_library_ids_bucket, |
570 |
$cached_library_ids_bucket, |
569 |
undef, |
571 |
undef, |
570 |
'Cache has been expired after changing library property' |
572 |
'Cache has been expired after changing library property' |
571 |
); |
573 |
); |
572 |
|
574 |
|
573 |
$cached_library = Koha::Libraries->_objects_cache_get( |
575 |
$cached_library = Koha::Libraries->_objects_cache_get( |
574 |
Koha::Libraries->_objects_cache_cache_key('find', $library2->branchcode), |
576 |
Koha::Libraries->_objects_cache_cache_key( 'find', $library2->branchcode ), |
575 |
'ids' |
577 |
'ids' |
576 |
); |
578 |
); |
577 |
is ( |
579 |
is( |
578 |
ref($cached_library), |
580 |
ref($cached_library), |
579 |
'Koha::Library', 'Cache should still contain other cached libraries' |
581 |
'Koha::Library', 'Cache should still contain other cached libraries' |
580 |
); |
582 |
); |
581 |
|
583 |
|
582 |
my $stored_library = Koha::Libraries->find($library1->branchcode); |
584 |
my $stored_library = Koha::Libraries->find( $library1->branchcode ); |
583 |
is ( |
585 |
is( |
584 |
$stored_library->branchname, |
586 |
$stored_library->branchname, |
585 |
'My library', |
587 |
'My library', |
586 |
'Library is fetched from database after cache has been expired' |
588 |
'Library is fetched from database after cache has been expired' |
587 |
); |
589 |
); |
588 |
|
590 |
|
589 |
$cached_library = Koha::Libraries->_objects_cache_get( |
591 |
$cached_library = Koha::Libraries->_objects_cache_get( |
590 |
Koha::Libraries->_objects_cache_cache_key('find', $library1->branchcode), |
592 |
Koha::Libraries->_objects_cache_cache_key( 'find', $library1->branchcode ), |
591 |
'ids' |
593 |
'ids' |
592 |
); |
594 |
); |
593 |
is ( |
595 |
is( |
594 |
ref($cached_library), |
596 |
ref($cached_library), |
595 |
'Koha::Library', |
597 |
'Koha::Library', |
596 |
'Library should be cached after previously have been fetched' |
598 |
'Library should be cached after previously have been fetched' |
Lines 598-640
subtest 'cache tests' => sub {
Link Here
|
598 |
|
600 |
|
599 |
$library1->store(); |
601 |
$library1->store(); |
600 |
$cached_library = Koha::Libraries->_objects_cache_get( |
602 |
$cached_library = Koha::Libraries->_objects_cache_get( |
601 |
Koha::Libraries->_objects_cache_cache_key('find', $library1->branchcode), |
603 |
Koha::Libraries->_objects_cache_cache_key( 'find', $library1->branchcode ), |
602 |
'ids' |
604 |
'ids' |
603 |
); |
605 |
); |
604 |
is ($cached_library, undef, 'Cache has been expired after saving library to database'); |
606 |
is( $cached_library, undef, 'Cache has been expired after saving library to database' ); |
605 |
|
607 |
|
606 |
Koha::Libraries->find($library1->branchcode, { key => 'primary' }); |
608 |
Koha::Libraries->find( $library1->branchcode, { key => 'primary' } ); |
607 |
$cached_library = Koha::Libraries->_objects_cache_get( |
609 |
$cached_library = Koha::Libraries->_objects_cache_get( |
608 |
Koha::Libraries->_objects_cache_cache_key('find', $library1->branchcode, { key => 'primary' }), |
610 |
Koha::Libraries->_objects_cache_cache_key( 'find', $library1->branchcode, { key => 'primary' } ), |
609 |
'args' |
611 |
'args' |
610 |
); |
612 |
); |
611 |
is ( |
613 |
is( |
612 |
ref($cached_library), |
614 |
ref($cached_library), |
613 |
'Koha::Library', |
615 |
'Koha::Library', |
614 |
'The args cache bucket should be used if find is called with the attrs argument' |
616 |
'The args cache bucket should be used if find is called with the attrs argument' |
615 |
); |
617 |
); |
616 |
my $cached_value; |
618 |
my $cached_value; |
617 |
|
619 |
|
618 |
for my $method ('find', 'search') { |
620 |
for my $method ( 'find', 'search' ) { |
619 |
|
621 |
|
620 |
Koha::Libraries->$method({ branchcode => $library1->branchcode }); |
622 |
Koha::Libraries->$method( { branchcode => $library1->branchcode } ); |
621 |
Koha::Libraries->$method({ branchcode => $library2->branchcode }); |
623 |
Koha::Libraries->$method( { branchcode => $library2->branchcode } ); |
622 |
|
624 |
|
623 |
$cached_value = Koha::Libraries->_objects_cache_get( |
625 |
$cached_value = Koha::Libraries->_objects_cache_get( |
624 |
Koha::Libraries->_objects_cache_cache_key($method, { branchcode => $library1->branchcode }), |
626 |
Koha::Libraries->_objects_cache_cache_key( $method, { branchcode => $library1->branchcode } ), |
625 |
'args' |
627 |
'args' |
626 |
); |
628 |
); |
627 |
$cached_library = $method eq 'search' ? $cached_value->next : $cached_value; |
629 |
$cached_library = $method eq 'search' ? $cached_value->next : $cached_value; |
628 |
is ( |
630 |
is( |
629 |
ref($cached_library), |
631 |
ref($cached_library), |
630 |
'Koha::Library', |
632 |
'Koha::Library', |
631 |
'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') |
633 |
'Library should be cached after previously have been fetched, and use the args cache bucket ' |
|
|
634 |
. ( $method eq 'find' ? 'if find was called with column conditions' : 'if search was called' ) |
632 |
); |
635 |
); |
633 |
|
636 |
|
634 |
$cached_library->_result->set_column('branchname', 'My library in cache'); |
637 |
$cached_library->_result->set_column( 'branchname', 'My library in cache' ); |
635 |
$cached_value = Koha::Libraries->$method({ branchcode => $library1->branchcode }); |
638 |
$cached_value = Koha::Libraries->$method( { branchcode => $library1->branchcode } ); |
636 |
$cached_library = $method eq 'search' ? $cached_value->next : $cached_value; |
639 |
$cached_library = $method eq 'search' ? $cached_value->next : $cached_value; |
637 |
is ( |
640 |
is( |
638 |
$cached_library->branchname, |
641 |
$cached_library->branchname, |
639 |
'My library in cache', |
642 |
'My library in cache', |
640 |
"The cashed library returned by $method should be the cached library, using the args cache bucket" |
643 |
"The cashed library returned by $method should be the cached library, using the args cache bucket" |
Lines 642-682
subtest 'cache tests' => sub {
Link Here
|
642 |
|
645 |
|
643 |
$cached_library->branchname('My expired library'); |
646 |
$cached_library->branchname('My expired library'); |
644 |
$cached_value = Koha::Libraries->_objects_cache_get( |
647 |
$cached_value = Koha::Libraries->_objects_cache_get( |
645 |
Koha::Libraries->_objects_cache_cache_key($method, { branchcode => $library1->branchcode }), |
648 |
Koha::Libraries->_objects_cache_cache_key( $method, { branchcode => $library1->branchcode } ), |
646 |
'args' |
649 |
'args' |
647 |
); |
650 |
); |
648 |
is ( |
651 |
is( |
649 |
$cached_value, |
652 |
$cached_value, |
650 |
undef, |
653 |
undef, |
651 |
'Cache has been expired after changing branchname, using the args cache bucket' |
654 |
'Cache has been expired after changing branchname, using the args cache bucket' |
652 |
); |
655 |
); |
653 |
|
656 |
|
654 |
$cached_value = Koha::Libraries->_objects_cache_get( |
657 |
$cached_value = Koha::Libraries->_objects_cache_get( |
655 |
Koha::Libraries->_objects_cache_cache_key($method, { branchcode => $library2->branchcode }), |
658 |
Koha::Libraries->_objects_cache_cache_key( $method, { branchcode => $library2->branchcode } ), |
656 |
'args' |
659 |
'args' |
657 |
); |
660 |
); |
658 |
is ( |
661 |
is( |
659 |
$cached_value, |
662 |
$cached_value, |
660 |
undef, |
663 |
undef, |
661 |
'The whole args cache bucket has been emptied after triggering cache expiration' |
664 |
'The whole args cache bucket has been emptied after triggering cache expiration' |
662 |
); |
665 |
); |
663 |
} |
666 |
} |
664 |
|
667 |
|
665 |
Koha::Libraries->find($library2->branchcode); |
668 |
Koha::Libraries->find( $library2->branchcode ); |
666 |
|
669 |
|
667 |
my $libraries = Koha::Libraries->search({ branchcode => $library1->branchcode }); |
670 |
my $libraries = Koha::Libraries->search( { branchcode => $library1->branchcode } ); |
668 |
$libraries->next; |
671 |
$libraries->next; |
669 |
is($libraries->next, undef, 'Cached libraries search result iterator has been exhausted'); |
672 |
is( $libraries->next, undef, 'Cached libraries search result iterator has been exhausted' ); |
670 |
|
|
|
671 |
$libraries = Koha::Libraries->search({ branchcode => $library1->branchcode }); |
672 |
is(ref($libraries->next), 'Koha::Library', 'Cached libraries search result iterator has been reset'); |
673 |
|
673 |
|
|
|
674 |
$libraries = Koha::Libraries->search( { branchcode => $library1->branchcode } ); |
675 |
is( ref( $libraries->next ), 'Koha::Library', 'Cached libraries search result iterator has been reset' ); |
674 |
|
676 |
|
675 |
my $cached_libraries = Koha::Libraries->_objects_cache_get( |
677 |
my $cached_libraries = Koha::Libraries->_objects_cache_get( |
676 |
Koha::Libraries->_objects_cache_cache_key('search', { 'branchcode' => $library1->branchcode }), |
678 |
Koha::Libraries->_objects_cache_cache_key( 'search', { 'branchcode' => $library1->branchcode } ), |
677 |
'args' |
679 |
'args' |
678 |
); |
680 |
); |
679 |
is ( |
681 |
is( |
680 |
ref($cached_libraries), |
682 |
ref($cached_libraries), |
681 |
'Koha::Libraries', |
683 |
'Koha::Libraries', |
682 |
'Libraries should be cached after previously have been fetched' |
684 |
'Libraries should be cached after previously have been fetched' |
Lines 684-709
subtest 'cache tests' => sub {
Link Here
|
684 |
|
686 |
|
685 |
$library1->delete(); |
687 |
$library1->delete(); |
686 |
$cached_libraries = Koha::Libraries->_objects_cache_get( |
688 |
$cached_libraries = Koha::Libraries->_objects_cache_get( |
687 |
Koha::Libraries->_objects_cache_cache_key('search', { 'branchcode' => $library1->branchcode }), |
689 |
Koha::Libraries->_objects_cache_cache_key( 'search', { 'branchcode' => $library1->branchcode } ), |
688 |
'args' |
690 |
'args' |
689 |
); |
691 |
); |
690 |
is ($cached_libraries, undef, 'Search cache has been expired after deleting library'); |
692 |
is( $cached_libraries, undef, 'Search cache has been expired after deleting library' ); |
691 |
|
693 |
|
692 |
$cached_library = Koha::Libraries->_objects_cache_get( |
694 |
$cached_library = Koha::Libraries->_objects_cache_get( |
693 |
Koha::Libraries->_objects_cache_cache_key('find', $library2->branchcode), |
695 |
Koha::Libraries->_objects_cache_cache_key( 'find', $library2->branchcode ), |
694 |
'ids' |
696 |
'ids' |
695 |
); |
697 |
); |
696 |
is ( |
698 |
is( |
697 |
ref($cached_library), |
699 |
ref($cached_library), |
698 |
'Koha::Library', |
700 |
'Koha::Library', |
699 |
'Library should be cached after previously have been fetched' |
701 |
'Library should be cached after previously have been fetched' |
700 |
); |
702 |
); |
701 |
$library2->delete(); |
703 |
$library2->delete(); |
702 |
$cached_library = Koha::Libraries->_objects_cache_get( |
704 |
$cached_library = Koha::Libraries->_objects_cache_get( |
703 |
Koha::Libraries->_objects_cache_cache_key('find', $library2->branchcode), |
705 |
Koha::Libraries->_objects_cache_cache_key( 'find', $library2->branchcode ), |
704 |
'ids' |
706 |
'ids' |
705 |
); |
707 |
); |
706 |
is ($cached_library, undef, 'Find cache has been expired after deleting library'); |
708 |
is( $cached_library, undef, 'Find cache has been expired after deleting library' ); |
707 |
|
709 |
|
708 |
$schema->storage->txn_rollback; |
710 |
$schema->storage->txn_rollback; |
709 |
} |
711 |
} |
710 |
- |
|
|