View | Details | Raw Unified | Return to bug 23387
Collapse All | Expand All

(-)a/C4/ClassSource.pm (-18 / +35 lines)
Lines 21-26 use Modern::Perl; Link Here
21
use C4::Context;
21
use C4::Context;
22
use C4::ClassSortRoutine qw( GetClassSortKey );
22
use C4::ClassSortRoutine qw( GetClassSortKey );
23
23
24
use Koha::Cache::Memory::Lite;
25
24
our (@ISA, @EXPORT_OK);
26
our (@ISA, @EXPORT_OK);
25
BEGIN {
27
BEGIN {
26
    require Exporter;
28
    require Exporter;
Lines 76-90 foreach my $cn_source (sort keys %$sources) { Link Here
76
78
77
sub GetClassSources {
79
sub GetClassSources {
78
80
79
    my %class_sources = ();
81
    my $memory_cache = Koha::Cache::Memory::Lite->get_instance();
80
    my $dbh = C4::Context->dbh;
82
    my $class_sources = $memory_cache->get_from_cache( "GetClassSource:All" );;
81
    my $sth = $dbh->prepare("SELECT * FROM `class_sources`");
83
    unless( $class_sources ){
82
    $sth->execute();
84
        my $dbh = C4::Context->dbh;
83
    while (my $source = $sth->fetchrow_hashref) {
85
        my $sth = $dbh->prepare("SELECT * FROM `class_sources`");
84
        $class_sources{ $source->{'cn_source'} } = $source;
86
        $sth->execute();
87
        while (my $source = $sth->fetchrow_hashref) {
88
            $class_sources->{ $source->{'cn_source'} } = $source;
89
        }
90
        $memory_cache->set_in_cache( "GetClassSource:All", $class_sources );
85
    }
91
    }
86
92
    return $class_sources;
87
    return \%class_sources;
88
93
89
}
94
}
90
95
Lines 99-109 sub GetClassSources { Link Here
99
sub GetClassSource {
104
sub GetClassSource {
100
105
101
    my ($cn_source) = (@_);
106
    my ($cn_source) = (@_);
102
    my $dbh = C4::Context->dbh;
107
    return unless $cn_source;
103
    my $sth = $dbh->prepare("SELECT * FROM `class_sources` WHERE cn_source = ?");
108
    my $memory_cache = Koha::Cache::Memory::Lite->get_instance();
104
    $sth->execute($cn_source);
109
    my $class_source = $memory_cache->get_from_cache( "GetClassSource:" . $cn_source );
105
    my $row = $sth->fetchrow_hashref();
110
    unless( $class_source ){
106
    return $row;
111
        my $dbh = C4::Context->dbh;
112
        my $sth = $dbh->prepare("SELECT * FROM `class_sources` WHERE cn_source = ?");
113
        $sth->execute($cn_source);
114
        $class_source = $sth->fetchrow_hashref();
115
        $memory_cache->set_in_cache( "GetClassSource:" . $cn_source, $class_source );
116
    }
117
    return $class_source;
107
}
118
}
108
119
109
=head2 GetClassSortRule
120
=head2 GetClassSortRule
Lines 117-127 sub GetClassSource { Link Here
117
sub GetClassSortRule {
128
sub GetClassSortRule {
118
129
119
    my ($class_sort_rule) = (@_);
130
    my ($class_sort_rule) = (@_);
120
    my $dbh = C4::Context->dbh;
131
    return unless $class_sort_rule;
121
    my $sth = $dbh->prepare("SELECT * FROM `class_sort_rules` WHERE `class_sort_rule` = ?");
132
    my $memory_cache = Koha::Cache::Memory::Lite->get_instance();
122
    $sth->execute($class_sort_rule);
133
    my $class_sort_rules = $memory_cache->get_from_cache( "GetClassSortRule:" . $class_sort_rule );
123
    my $row = $sth->fetchrow_hashref();
134
    unless( $class_sort_rules ){
124
    return $row;
135
        my $dbh = C4::Context->dbh;
136
        my $sth = $dbh->prepare("SELECT * FROM `class_sort_rules` WHERE `class_sort_rule` = ?");
137
        $sth->execute($class_sort_rule);
138
        $class_sort_rules = $sth->fetchrow_hashref();
139
        $memory_cache->set_in_cache( "GetClassSortRule:" . $class_sort_rule, $class_sort_rules );
140
    }
141
    return $class_sort_rules;
125
}
142
}
126
143
127
=head2 GetClassSort
144
=head2 GetClassSort
(-)a/t/db_dependent/ClassSources.t (-1 / +156 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
#
3
# This file is part of Koha.
4
#
5
# Copyright (c) 2024
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Test::More tests => 4;
23
use t::lib::TestBuilder;
24
25
use Koha::ClassSortRules;
26
use Koha::ClassSources;
27
28
BEGIN {
29
    use_ok( 'C4::ClassSource', qw( GetClassSources GetClassSource GetClassSortRule ) );
30
}
31
32
my $schema  = Koha::Database->new->schema;
33
my $builder = t::lib::TestBuilder->new;
34
35
subtest 'GetClassSources' => sub {
36
37
    plan tests => 5;
38
39
    my $class_rule = $builder->build(
40
        {
41
            source => 'ClassSortRule',
42
            value  => { sort_routine => "LCC" }
43
        }
44
    );
45
    my $class_source_1 = $builder->build(
46
        {
47
            source => 'ClassSource',
48
            value  => {
49
                class_sort_rule => $class_rule->{class_sort_rule},
50
            }
51
        }
52
    );
53
    my $source_code = $class_source_1->{cn_source};
54
55
    Koha::Cache::Memory::Lite->flush();
56
57
    my $class_sources = GetClassSources();
58
    is_deeply( $class_sources->{$source_code}, $class_source_1, "The retrieved version, from the DB, is the same" );
59
60
    # Now we add a new one, but expect the old to be cached (same request)
61
    my $class_source_2 = $builder->build(
62
        {
63
            source => 'ClassSource',
64
            value  => {
65
                class_sort_rule => $class_rule->{class_sort_rule},
66
            }
67
        }
68
    );
69
    $source_code = $class_source_2->{cn_source};
70
71
    my $class_sources_cached = GetClassSources();
72
    is_deeply(
73
        $class_sources, $class_sources_cached,
74
        "We have a cached version, so we won't have the new one we added"
75
    );
76
    is( $class_sources_cached->{$source_code}, undef, "New value not present" );
77
78
    # Now we clear the cache, i.e. pretend we're a new request
79
    Koha::Cache::Memory::Lite->flush();
80
81
    $class_sources = GetClassSources();
82
    is( $class_sources_cached->{$source_code}, undef, "New value now present after cache cleared" );
83
    $class_sources_cached = GetClassSources();
84
    is_deeply( $class_sources, $class_sources_cached, "New cached version does match the updated fresh version" );
85
};
86
87
subtest 'GetClassSource' => sub {
88
89
    plan tests => 4;
90
91
    my $class_rule = $builder->build(
92
        {
93
            source => 'ClassSortRule',
94
            value  => { sort_routine => "LCC" }
95
        }
96
    );
97
    my $class_source_1 = $builder->build(
98
        {
99
            source => 'ClassSource',
100
            value  => {
101
                class_sort_rule => $class_rule->{class_sort_rule},
102
            }
103
        }
104
    );
105
    my $source_code = $class_source_1->{cn_source};
106
107
    Koha::Cache::Memory::Lite->flush();
108
    my $class_source_db = GetClassSource($source_code);
109
    is_deeply( $class_source_db, $class_source_1, "The retrieved version, from the DB, is the same" );
110
111
    my $class_source_object = Koha::ClassSources->find($source_code);
112
    $class_source_object->description("We changed the thing")->store();
113
114
    my $class_source_cache = GetClassSource($source_code);
115
    is(
116
        $class_source_cache->{description}, $class_source_db->{description},
117
        "Still have old description in cache (same request)"
118
    );
119
120
    Koha::Cache::Memory::Lite->flush();                 # New request, that's the gimmick
121
    $class_source_db = GetClassSource($source_code);    # Refetch from DB to populate cache
122
    is( $class_source_db->{description}, "We changed the thing", "DB request got update value" );
123
    $class_source_cache = GetClassSource($source_code);
124
    is( $class_source_cache->{description}, $class_source_db->{description}, "Now both get the correct value" );
125
126
};
127
128
subtest 'GetClassSortRule' => sub {
129
130
    plan tests => 4;
131
132
    my $class_rule_1 = $builder->build(
133
        {
134
            source => 'ClassSortRule',
135
            value  => { sort_routine => "LCC" }
136
        }
137
    );
138
    my $sort_rule = $class_rule_1->{class_sort_rule};
139
140
    Koha::Cache::Memory::Lite->flush();
141
    my $class_sort_rule_db = GetClassSortRule($sort_rule);
142
    is_deeply( $class_sort_rule_db, $class_rule_1, "The retrieved version, from the DB, is the same" );
143
144
    my $class_source_object = Koha::ClassSortRules->find($sort_rule);
145
    $class_source_object->sort_routine('Dewey')->store();
146
147
    my $class_sort_rule_cache = GetClassSortRule($sort_rule);
148
    is_deeply( $class_sort_rule_cache, $class_sort_rule_db, "Still have old sort rule in cache (same request)" );
149
150
    Koha::Cache::Memory::Lite->flush();                    # New request, that's the gimmick
151
    $class_sort_rule_db = GetClassSortRule($sort_rule);    # Refetch from DB to populate cache
152
    is_deeply( $class_sort_rule_db, $class_source_object->unblessed, "DB request got updated value" );
153
    $class_sort_rule_cache = GetClassSortRule($sort_rule);
154
    is_deeply( $class_sort_rule_cache, $class_sort_rule_db, "Now both get the correct value" );
155
156
};

Return to bug 23387