Bug 32154: DBIC schema
[koha-ffzg.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 our $singleton_cache;
45
46 =head2 get_instance
47
48 This gets a shared instance of the lite cache, set up in a very default
49 way. The lite cache is an in memory only cache that's automatically flushed
50 for every request.
51
52 =cut
53
54 sub get_instance {
55     my ($class) = @_;
56     $singleton_cache = $class->new() unless $singleton_cache;
57     return $singleton_cache;
58 }
59
60 =head2 get_from_cache
61
62     my $value = $cache->get_from_cache($key);
63
64 Retrieve the value stored under the specified key in the cache.
65
66 The retrieved value is a direct reference so should not be modified.
67
68 =cut
69
70 sub get_from_cache {
71     my ( $self, $key ) = @_;
72     return $L1_cache{$key};
73 }
74
75 =head2 set_in_cache
76
77     $cache->set_in_cache($key, $value);
78
79 Save a value to the specified key in the cache.
80
81 =cut
82
83 sub set_in_cache {
84     my ( $self, $key, $value ) = @_;
85     $L1_cache{$key} = $value;
86 }
87
88 =head2 clear_from_cache
89
90     $cache->clear_from_cache($key);
91
92 Remove the value identified by the specified key from the lite cache.
93
94 =cut
95
96 sub clear_from_cache {
97     my ( $self, $key ) = @_;
98     delete $L1_cache{$key};
99 }
100
101 =head2 all_keys
102
103     my @keys = $cache->all_keys();
104
105 Returns an array of all keys currently in the lite cache.
106
107 =cut
108
109 sub all_keys {
110     my ( $self ) = @_;
111     return keys %L1_cache;
112 }
113
114 =head2 flush
115
116     $cache->flush();
117
118 Clear the entire lite cache.
119
120 =cut
121
122 sub flush {
123     my ( $self ) = @_;
124     %L1_cache = ();
125 }
126
127 1;