EnsEMBL/DBSQL/Support/LruIdCache.pm untested

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 88 CC 7 SCAR 21.8
CC
7
Cov
0%
get
20.0
get_by_list
12.0
lru_size
6.0
CRAP 8.9
1 =head1 LICENSE
2  
3 See the NOTICE file distributed with this work for additional information
4 regarding copyright ownership.
5  
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9  
10 http://www.apache.org/licenses/LICENSE-2.0
11  
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17  
18 =cut
19  
20  
21 =head1 CONTACT
22  
23 Please email comments or questions to the public Ensembl
24 developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
25  
26 Questions may also be sent to the Ensembl help desk at
27 <http://www.ensembl.org/Help/Contact>.
28  
29 =cut
30  
31 =head1 NAME
32  
33 Bio::EnsEMBL::DBSQL::Support::LruIdCache - ID based caching using an LRU backed cache
34  
35 =head1 SYNOPSIS
36  
37 my $cache = Bio::EnsEMBL::DBSQL::Support::LruIdCache->new($adaptor, 2);
38 my $obj = $cache->put(1, 'a');
39 my $obj = $cache->put(2, 'b');
40 my $obj = $cache->put(3, 'c');
41
42 is_deeply([$cache->cache_keys()], ['b','c']); #ID 1 was ejected under LRU rules
43  
44 =head1 DESCRIPTION
45  
46 An implementation of caching which uses the oldest accessed key as the
47 value to be ejected from the cache when the maximum size has been hit. See
48 the following page for more information about this algorithm
49  
50 http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used
51  
52 =head1 METHODS
53  
54 =cut
55  
56 package Bio::EnsEMBL::DBSQL::Support::LruIdCache;
57 $Bio::EnsEMBL::DBSQL::Support::LruIdCache::VERSION = '114.0.0_50'; # TRIAL
58 $Bio::EnsEMBL::DBSQL::Support::LruIdCache::VERSION = '114.0.050';
59 use strict;
60 use warnings;
61 use base qw/Bio::EnsEMBL::DBSQL::Support::BaseCache/;
62 use Bio::EnsEMBL::Utils::Cache;
63 use Bio::EnsEMBL::Utils::Scalar qw/assert_ref/;
64  
65 my $DEFAULT = 1000;
66  
67 =head2 new
68  
69 Arg [1] : Bio::EnsEMBL::DBSQL::BaseAdaptor $db_adaptor
70 Arg [2] : Int size of the cache. Defaults to 1000
71 Example : my $cache = Bio::EnsEMBL::DBSQL::Support::LruIdCache->new($db_adaptor, 10);
72 Description: Creates a new cache class instance
73 Returntype : Bio::EnsEMBL::DBSQL::Support::BaseCache
74 Exceptions : none
75 Caller : BaseAdaptors
76 Status : Beta
77  
78 =cut
79  
80 sub new {
81 my ($class, $adaptor, $lru_size) = @_;
82 my $self = $class->SUPER::new($adaptor);
83 $lru_size ||= $DEFAULT;
84 $self->lru_size($lru_size);
85 return $self;
86 }
87  
88 =head2 lru_size
89  
90 Arg [1] : Int size of the cache
91 Example : $cache->lru_size(10);
92 Description: Resets the size of the cache and forces a rebuild of the cache
93 object to apply this new sizing. Also functions as a getter
94 Returntype : Int
95 Exceptions : none
96 Caller : BaseAdaptors
97 Status : Beta
98  
99 =cut
100  
101 sub lru_size {
102 my ($self, $lru_size) = @_;
103 if(defined $lru_size) {
104 $self->{'lru_size'} = $lru_size;
105 $self->delete_cache();
106 }
107 return $self->{'lru_size'};
108 }
109  
110 =head2 build_cache
111  
112 Description: Returns an instance of C<Bio::EnsEMBL::Utils::Cache> and sized
113 according to the return value in C<lru_cache()>
114 Returntype : Bio::EnsEMBL::Utils::Cache
115 Exceptions : none
116 Caller : BaseAdaptors
117 Status : Beta
118  
119 =cut
120  
121 sub build_cache {
122 my ($self) = @_;
123 tie my %cache, 'Bio::EnsEMBL::Utils::Cache', $self->lru_size();
124 return \%cache;
125 }
126  
127 =head2 get
128  
129 Arg [1] : String key to retrieve
130 Example : $is($cache->get(1), 'a');
131 Description: Retrieves the value held in the cache. If the value is not in
132 the cache we will retrieve the value from
133 C<_uncached_fetch_by_dbID> and then store that value
134 Returntype : Scalar value held in the cache or nothing if the ID was not present
135 Exceptions : If key was undefined
136 Caller : BaseAdaptors
137 Status : Beta
138  
139 =cut
140  
141 sub get {
142 my ($self, $key) = @_;
143 my $value = $self->SUPER::get($key);
144 if(! defined $value) {
145 my $new_value = $self->adaptor()->_uncached_fetch_by_dbID($key);
146 if(defined $new_value) {
147 $self->put($key, $new_value);
148 $value = $new_value;
149 }
150 }
151 return $value if defined $value;
152 return;
153 }
154  
155 =head2 get_by_list
156  
157 Arg [1] : ArrayRef keys to retrieve
158 Arg [2] : Bio::EnsEMBL::Slice optional attribute for
159 C<_uncached_fetch_all_by_dbID_list()> delegation
160 Example : is($cache->get_by_list([1,2]), ['a','b']);
161 Description: Attempts to retrieve all values currently available in the cache,
162 fetches any remaining values and stores these in the cache.
163 Returntype : ArrayRef of found values
164 Exceptions : None
165 Caller : BaseAdaptors
166 Status : Beta
167  
168 =cut
169  
170 sub get_by_list {
171 my ($self, $list, $slice) = @_;
172 assert_ref($list, 'ARRAY', 'list');
173 my $results = $self->SUPER::get_by_list($list);
174 my %available = map { $_->dbID(), $_ } @{$results};
175 my @to_fetch;
176 foreach my $input (@{$list}) {
177 next if ! defined $input;
178 push(@to_fetch, $input) if ! exists $available{$input};
179 }
180
181 my $fetched = $self->adaptor()->_uncached_fetch_all_by_dbID_list(\@to_fetch, $slice);
182 foreach my $obj (@{$fetched}) {
183 push(@{$results}, $obj);
184 $self->put($obj->dbID(), $obj);
185 }
186
187 return $results;
188 }
189  
190 1;