|
Lines 19-25
Link Here
|
| 19 |
|
19 |
|
| 20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
| 21 |
|
21 |
|
| 22 |
use Test::More tests => 13; |
22 |
use Test::More tests => 14; |
| 23 |
|
23 |
|
| 24 |
use C4::Biblio; |
24 |
use C4::Biblio; |
| 25 |
use C4::Context; |
25 |
use C4::Context; |
|
Lines 32-37
use Koha::Library;
Link Here
|
| 32 |
use Koha::Libraries; |
32 |
use Koha::Libraries; |
| 33 |
use Koha::Database; |
33 |
use Koha::Database; |
| 34 |
use Koha::CirculationRules; |
34 |
use Koha::CirculationRules; |
|
|
35 |
use Koha::Cache::Memory::Lite; |
| 35 |
|
36 |
|
| 36 |
use t::lib::Mocks; |
37 |
use t::lib::Mocks; |
| 37 |
use t::lib::TestBuilder; |
38 |
use t::lib::TestBuilder; |
|
Lines 413-415
subtest 'outgoing_transfers' => sub {
Link Here
|
| 413 |
|
414 |
|
| 414 |
$schema->storage->txn_rollback; |
415 |
$schema->storage->txn_rollback; |
| 415 |
}; |
416 |
}; |
| 416 |
- |
417 |
|
|
|
418 |
subtest 'cache tests' => sub { |
| 419 |
plan tests => 24; |
| 420 |
|
| 421 |
$schema->storage->txn_begin; |
| 422 |
|
| 423 |
my $library1 = $builder->build_object( |
| 424 |
{ |
| 425 |
class => 'Koha::Libraries', |
| 426 |
value => { |
| 427 |
branchname => 'My library' |
| 428 |
} |
| 429 |
} |
| 430 |
); |
| 431 |
my $library2 = $builder->build_object( |
| 432 |
{ |
| 433 |
class => 'Koha::Libraries', |
| 434 |
value => { |
| 435 |
branchname => 'My other library' |
| 436 |
} |
| 437 |
} |
| 438 |
); |
| 439 |
|
| 440 |
# Since build_object calls find internally |
| 441 |
# we first have to flush the cache since these |
| 442 |
# objects are now already cached |
| 443 |
Koha::Cache::Memory::Lite->get_instance()->flush(); |
| 444 |
|
| 445 |
my $ids_bucket = Koha::Libraries->_objects_cache_bucket('ids'); |
| 446 |
ok(!%{$ids_bucket}, 'Find cache has been cleared after flushing memory cache'); |
| 447 |
|
| 448 |
my $cached_library = Koha::Libraries->find($library1->branchcode); |
| 449 |
Koha::Libraries->find($library2->branchcode); |
| 450 |
|
| 451 |
my $cached_library_ids_bucket = Koha::Libraries->_objects_cache_get( |
| 452 |
Koha::Libraries->_objects_cache_cache_key('find', $library1->branchcode), |
| 453 |
'ids' |
| 454 |
); |
| 455 |
is (ref($cached_library_ids_bucket), 'Koha::Library', 'Library should be cached after previously have been fetched'); |
| 456 |
is ($cached_library_ids_bucket->branchcode, $library1->branchcode, 'The cashed library should be the expected library'); |
| 457 |
|
| 458 |
# Set property of library by-passing the cache expiration logic |
| 459 |
# so we can veriy library is not only present in cache by also |
| 460 |
# properly retrieved in find |
| 461 |
$cached_library->_result->set_column('branchname', 'My library in cache'); |
| 462 |
$cached_library = Koha::Libraries->find($library1->branchcode); |
| 463 |
is ( |
| 464 |
$cached_library->branchname, |
| 465 |
'My library in cache', |
| 466 |
'The cashed library returned by find should be the cached library' |
| 467 |
); |
| 468 |
|
| 469 |
$library1->branchname('My expired library'); |
| 470 |
$cached_library_ids_bucket = Koha::Libraries->_objects_cache_get( |
| 471 |
Koha::Libraries->_objects_cache_cache_key('find', $library1->branchcode), |
| 472 |
'ids' |
| 473 |
); |
| 474 |
is ( |
| 475 |
$cached_library_ids_bucket, |
| 476 |
undef, |
| 477 |
'Cache has been expired after changing library property' |
| 478 |
); |
| 479 |
|
| 480 |
$cached_library = Koha::Libraries->_objects_cache_get( |
| 481 |
Koha::Libraries->_objects_cache_cache_key('find', $library2->branchcode), |
| 482 |
'ids' |
| 483 |
); |
| 484 |
is ( |
| 485 |
ref($cached_library), |
| 486 |
'Koha::Library', 'Cache should still contain other cached libraries' |
| 487 |
); |
| 488 |
|
| 489 |
my $stored_library = Koha::Libraries->find($library1->branchcode); |
| 490 |
is ( |
| 491 |
$stored_library->branchname, |
| 492 |
'My library', |
| 493 |
'Library is fetched from database after cache has been expired' |
| 494 |
); |
| 495 |
|
| 496 |
$cached_library = Koha::Libraries->_objects_cache_get( |
| 497 |
Koha::Libraries->_objects_cache_cache_key('find', $library1->branchcode), |
| 498 |
'ids' |
| 499 |
); |
| 500 |
is ( |
| 501 |
ref($cached_library), |
| 502 |
'Koha::Library', |
| 503 |
'Library should be cached after previously have been fetched' |
| 504 |
); |
| 505 |
|
| 506 |
$library1->store(); |
| 507 |
$cached_library = Koha::Libraries->_objects_cache_get( |
| 508 |
Koha::Libraries->_objects_cache_cache_key('find', $library1->branchcode), |
| 509 |
'ids' |
| 510 |
); |
| 511 |
is ($cached_library, undef, 'Cache has been expired after saving library to database'); |
| 512 |
|
| 513 |
Koha::Libraries->find($library1->branchcode, { key => 'primary' }); |
| 514 |
$cached_library = Koha::Libraries->_objects_cache_get( |
| 515 |
Koha::Libraries->_objects_cache_cache_key('find', $library1->branchcode, { key => 'primary' }), |
| 516 |
'args' |
| 517 |
); |
| 518 |
is ( |
| 519 |
ref($cached_library), |
| 520 |
'Koha::Library', |
| 521 |
'The args cache bucket should be used if find is called with the attrs argument' |
| 522 |
); |
| 523 |
my $cached_value; |
| 524 |
|
| 525 |
for my $method ('find', 'search') { |
| 526 |
|
| 527 |
Koha::Libraries->$method({ branchcode => $library1->branchcode }); |
| 528 |
Koha::Libraries->$method({ branchcode => $library2->branchcode }); |
| 529 |
|
| 530 |
$cached_value = Koha::Libraries->_objects_cache_get( |
| 531 |
Koha::Libraries->_objects_cache_cache_key($method, { branchcode => $library1->branchcode }), |
| 532 |
'args' |
| 533 |
); |
| 534 |
$cached_library = $method eq 'search' ? $cached_value->next : $cached_value; |
| 535 |
is ( |
| 536 |
ref($cached_library), |
| 537 |
'Koha::Library', |
| 538 |
'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') |
| 539 |
); |
| 540 |
|
| 541 |
$cached_library->_result->set_column('branchname', 'My library in cache'); |
| 542 |
$cached_value = Koha::Libraries->$method({ branchcode => $library1->branchcode }); |
| 543 |
$cached_library = $method eq 'search' ? $cached_value->next : $cached_value; |
| 544 |
is ( |
| 545 |
$cached_library->branchname, |
| 546 |
'My library in cache', |
| 547 |
"The cashed library returned by $method should be the cached library, using the args cache bucket" |
| 548 |
); |
| 549 |
|
| 550 |
$cached_library->branchname('My expired library'); |
| 551 |
$cached_value = Koha::Libraries->_objects_cache_get( |
| 552 |
Koha::Libraries->_objects_cache_cache_key($method, { branchcode => $library1->branchcode }), |
| 553 |
'args' |
| 554 |
); |
| 555 |
is ( |
| 556 |
$cached_value, |
| 557 |
undef, |
| 558 |
'Cache has been expired after changing branchname, using the args cache bucket' |
| 559 |
); |
| 560 |
|
| 561 |
$cached_value = Koha::Libraries->_objects_cache_get( |
| 562 |
Koha::Libraries->_objects_cache_cache_key($method, { branchcode => $library2->branchcode }), |
| 563 |
'args' |
| 564 |
); |
| 565 |
is ( |
| 566 |
$cached_value, |
| 567 |
undef, |
| 568 |
'The whole args cache bucket has been emptied after triggering cache expiration' |
| 569 |
); |
| 570 |
} |
| 571 |
|
| 572 |
Koha::Libraries->find($library2->branchcode); |
| 573 |
|
| 574 |
my $libraries = Koha::Libraries->search({ branchcode => $library1->branchcode }); |
| 575 |
$libraries->next; |
| 576 |
is($libraries->next, undef, 'Cached libraries search result iterator has been exhausted'); |
| 577 |
|
| 578 |
$libraries = Koha::Libraries->search({ branchcode => $library1->branchcode }); |
| 579 |
is(ref($libraries->next), 'Koha::Library', 'Cached libraries search result iterator has been reset'); |
| 580 |
|
| 581 |
|
| 582 |
my $cached_libraries = Koha::Libraries->_objects_cache_get( |
| 583 |
Koha::Libraries->_objects_cache_cache_key('search', { 'branchcode' => $library1->branchcode }), |
| 584 |
'args' |
| 585 |
); |
| 586 |
is ( |
| 587 |
ref($cached_libraries), |
| 588 |
'Koha::Libraries', |
| 589 |
'Libraries should be cached after previously have been fetched' |
| 590 |
); |
| 591 |
|
| 592 |
$library1->delete(); |
| 593 |
$cached_libraries = Koha::Libraries->_objects_cache_get( |
| 594 |
Koha::Libraries->_objects_cache_cache_key('search', { 'branchcode' => $library1->branchcode }), |
| 595 |
'args' |
| 596 |
); |
| 597 |
is ($cached_libraries, undef, 'Search cache has been expired after deleting library'); |
| 598 |
|
| 599 |
$cached_library = Koha::Libraries->_objects_cache_get( |
| 600 |
Koha::Libraries->_objects_cache_cache_key('find', $library2->branchcode), |
| 601 |
'ids' |
| 602 |
); |
| 603 |
is ( |
| 604 |
ref($cached_library), |
| 605 |
'Koha::Library', |
| 606 |
'Library should be cached after previously have been fetched' |
| 607 |
); |
| 608 |
$library2->delete(); |
| 609 |
$cached_library = Koha::Libraries->_objects_cache_get( |
| 610 |
Koha::Libraries->_objects_cache_cache_key('find', $library2->branchcode), |
| 611 |
'ids' |
| 612 |
); |
| 613 |
is ($cached_library, undef, 'Find cache has been expired after deleting library'); |
| 614 |
|
| 615 |
$schema->storage->txn_rollback; |
| 616 |
} |