/
Persistant.pm
129 lines (98 loc) · 2.88 KB
/
Persistant.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package Koha::Persistant;
# Copyright (c) 2012 Dobrica Pavlinusic
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA
use strict;
use warnings;
use C4::Context;
use Data::Dump qw(dump);
use base 'Exporter';
use version; our $VERSION = qv('1.0.0');
our @EXPORT = (
qw( sql_cache authorised_value )
);
=head1 Persistant
Koha::Persistant - make data objects in Koha persistant
=head1 DESCRIPTION
Koha needs nice centralized way to cache data for plack
Name of this module was choosen to be non-conflicting with possible
future C<Koha::Cache>
=cut
=head2 sql_cache
$row = sql_cache($sql, $value1 [, $value2, ... ]);
Takes C<SELECT col1,col2 FROM table WHERE value1 = ? AND value2 = ?>
SQL query and cache result returning cached row.
-- key: name-of-key
Syntax inside SQL query will override default cache key generation
which is simple normalization of SQL strings.
=cut
sub DESTROY {
warn "## Koha::Persistent::DESTROY";
}
our $_sql_cache;
our $_cache;
our $stats;
sub _sql_cache {
my $sql = shift;
my @var = @_;
my $cache;
my $key = $sql;
$key =~ s/\s\s+/ /gs;
my $stat_key = $key;
my $eval;
if ( $key =~ s/^.*\s*--\s*key:\s*(.+)// ) {
$stat_key = $1;
$key = pop @_;
$eval = '$_cache->{';
$eval .= dump($_)."}->{" foreach @_;
$eval =~ s/\Q->{\E$//;
warn "# EVAL $eval";
eval "\$cache = $eval;";
die $! if $!;
} else {
$key = join(' ', $key, @var);
$cache = $_sql_cache;
}
if ( exists $cache->{$key} ) {
warn "### _sql_cache HIT $key\n";
$stats->{$stat_key}->[0]++;
return $cache->{$key};
}
warn "### _sql_cache MISS $key\n";
$stats->{$stat_key}->[1]++;
my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare( $sql );
$sth->execute( @var );
my $v = $sth->fetchrow_hashref;
if ( $eval ) {
eval $eval.'->{'.dump($key).'} = $v;';
} else {
$cache->{$key} = $v;
}
warn "# row $stat_key $key = ",dump($v);
warn dump($_cache);
return $v;
}
=head2 autorhised_value
my $row = authorised_value( category => $category, $value );
=cut
sub authorised_value {
shift if $_[0] eq 'category';
my ( $category, $value ) = @_;
my $row = _sql_cache("SELECT lib, lib_opac FROM authorised_values WHERE category = ? AND authorised_value = ? -- key:authorised_value", $category, $value);
warn dump $row;
return $row;
}
1;