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