3 See the NOTICE file distributed with
this work
for additional information
4 regarding copyright ownership.
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
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.
23 Please email comments or questions to the
public Ensembl
24 developers list at <http:
26 Questions may also be sent to the Ensembl help desk at
38 my $obj = $cache->
put(1,
'a');
39 my $obj = $cache->put(2,
'b');
40 my $obj = $cache->put(3,
'c');
42 is_deeply([$cache->cache_keys()], [
'b',
'c']); #ID 1 was ejected under LRU rules
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
56 package Bio::EnsEMBL::DBSQL::Support::LruIdCache;
69 Arg [2] : Int size of the cache. Defaults to 1000
71 Description: Creates a
new cache
class instance
80 my ($class, $adaptor, $lru_size) = @_;
81 my $self = $class->SUPER::new($adaptor);
82 $lru_size ||= $DEFAULT;
83 $self->lru_size($lru_size);
89 Arg [1] : Int size of the cache
90 Example : $cache->lru_size(10);
91 Description: Resets the size of the cache and forces a rebuild of the cache
92 object to apply
this new sizing. Also functions as a getter
101 my ($self, $lru_size) = @_;
102 if(defined $lru_size) {
103 $self->{
'lru_size'} = $lru_size;
106 return $self->{
'lru_size'};
111 Description: Returns an instance of C<Bio::EnsEMBL::Utils::Cache> and sized
112 according to the
return value in C<lru_cache()>
115 Caller : BaseAdaptors
122 tie my %cache,
'Bio::EnsEMBL::Utils::Cache', $self->lru_size();
128 Arg [1] : String key to retrieve
129 Example : $is($cache->get(1),
'a');
130 Description: Retrieves the value held in the cache. If the value is not in
131 the cache we will retrieve the value from
132 C<_uncached_fetch_by_dbID> and then store that value
133 Returntype : Scalar value held in the cache or nothing
if the ID was not present
134 Exceptions : If key was undefined
135 Caller : BaseAdaptors
141 my ($self, $key) = @_;
142 my $value = $self->SUPER::get($key);
143 if(! defined $value) {
144 my $new_value = $self->adaptor()->_uncached_fetch_by_dbID($key);
145 if(defined $new_value) {
146 $self->put($key, $new_value);
150 return $value
if defined $value;
156 Arg [1] : ArrayRef keys to retrieve
158 C<_uncached_fetch_all_by_dbID_list()> delegation
159 Example : is($cache->get_by_list([1,2]), [
'a',
'b']);
160 Description: Attempts to retrieve all values currently available in the cache,
161 fetches any remaining values and stores these in the cache.
162 Returntype : ArrayRef of found values
164 Caller : BaseAdaptors
170 my ($self, $list, $slice) = @_;
171 assert_ref($list,
'ARRAY',
'list');
172 my $results = $self->SUPER::get_by_list($list);
173 my %available =
map { $_->dbID(), $_ } @{$results};
175 foreach my $input (@{$list}) {
176 next
if ! defined $input;
177 push(@to_fetch, $input)
if ! exists $available{$input};
180 my $fetched = $self->adaptor()->_uncached_fetch_all_by_dbID_list(\@to_fetch, $slice);
181 foreach my $obj (@{$fetched}) {
182 push(@{$results}, $obj);
183 $self->put($obj->dbID(), $obj);