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