1 package Koha::Persistant;
3 # Copyright (c) 2012 Dobrica Pavlinusic
4 # This file is part of Koha.
6 # Koha is free software; you can redistribute it and/or modify it under the
7 # terms of the GNU General Public License as published by the Free Software
8 # Foundation; either version 2 of the License, or (at your option) any later
11 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along with
16 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
17 # Suite 330, Boston, MA 02111-1307 USA
24 use Data::Dump qw(dump);
27 use version; our $VERSION = qv('1.0.0');
30 qw( sql_cache authorised_value )
35 Koha::Persistant - make data objects in Koha persistant
39 Koha needs nice centralized way to cache data for plack
41 Name of this module was choosen to be non-conflicting with possible
48 $row = sql_cache($sql, $value1 [, $value2, ... ]);
50 Takes C<SELECT col1,col2 FROM table WHERE value1 = ? AND value2 = ?>
51 SQL query and cache result returning cached row.
55 Syntax inside SQL query will override default cache key generation
56 which is simple normalization of SQL strings.
61 warn "## Koha::Persistent::DESTROY";
80 if ( $key =~ s/^.*\s*--\s*key:\s*(.+)// ) {
84 $eval .= dump($_)."}->{" foreach @_;
85 $eval =~ s/\Q->{\E$//;
87 eval "\$cache = $eval;";
90 $key = join(' ', $key, @var);
94 if ( exists $cache->{$key} ) {
95 warn "### _sql_cache HIT $key\n";
96 $stats->{$stat_key}->[0]++;
97 return $cache->{$key};
99 warn "### _sql_cache MISS $key\n";
100 $stats->{$stat_key}->[1]++;
101 my $dbh = C4::Context->dbh;
102 my $sth = $dbh->prepare( $sql );
103 $sth->execute( @var );
104 my $v = $sth->fetchrow_hashref;
106 eval $eval.'->{'.dump($key).'} = $v;';
110 warn "# row $stat_key $key = ",dump($v);
115 =head2 autorhised_value
117 my $row = authorised_value( category => $category, $value );
121 sub authorised_value {
122 shift if $_[0] eq 'category';
123 my ( $category, $value ) = @_;
124 my $row = _sql_cache("SELECT lib, lib_opac FROM authorised_values WHERE category = ? AND authorised_value = ? -- key:authorised_value", $category, $value);