Bug 16088: Introduce Koha::Cache::Memory::Lite to cache the language
[koha.git] / Koha / Cache / Memory / Lite.pm
1 package Koha::Cache::Memory::Lite;
2
3 # Copyright 2016 Koha Development Team
4 #
5 # This file is part of Koha.
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 =head1 NAME
21
22 Koha::Cache::Memory::Lite - Handling caching of objects in memory *only* for Koha
23
24 =head1 SYNOPSIS
25
26   use Koha::Cache::Memory::Lite;
27   my $cache = Koha::Cache::Memory::Lite->get_instance();
28   $cache->set($key, $value);
29   my $retrieved_from_cache_value = $cache->get($key);
30   $cache->clear_from_cache($key);
31   $cache->flush();
32
33 =head1 DESCRIPTION
34
35 Koha in memory only caching routines.
36
37 =cut
38
39 use Modern::Perl;
40
41 use base qw(Class::Accessor);
42
43 our %L1_cache;
44
45 our $singleton_cache;
46 sub get_instance {
47     my ($class) = @_;
48     $singleton_cache = $class->new() unless $singleton_cache;
49     return $singleton_cache;
50 }
51
52 sub get_from_cache {
53     my ( $self, $key ) = @_;
54     return $L1_cache{$key};
55 }
56
57 sub set_in_cache {
58     my ( $self, $key, $value ) = @_;
59     $L1_cache{$key} = $value;
60 }
61
62 sub clear_from_cache {
63     my ( $self, $key ) = @_;
64     delete $L1_cache{$key};
65 }
66
67 sub flush {
68     my ( $self ) = @_;
69     %L1_cache = ();
70 }
71
72 1;