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
42 return {}; #sends back a very basic cache which is a hash
49 is($cache->get(1),
'a');
50 is($cache->put(1,
'b'),
'a'); #put returns the
object it replaced
51 is($cache->delete(1),
'b'); #
delete returns the
object it removed
53 is_deeply([$cache->cache_keys()], [1]);
55 $cache->clear_cache();
57 is($cache->size(), 0);
59 #Try using SQL - cache will be consulted accordingly
60 my $ids = $cache->get_by_sql(
'select dbid from table where val like ?', [
'someval%']);
64 A base
class used for holding methods common to all cache implementations.
65 Never use this class to do direct caching instead use one of the following
69 =item C<Bio::EnsEMBL::DBSQL::Support::LruIdCache>
71 =item C<Bio::EnsEMBL::DBSQL::Support::FullIdCache>
75 To provide exta functionality to the caches you should
override one of the above
76 classes and extend. Caches work when you use inheritence by composition in their
83 package Bio::EnsEMBL::DBSQL::Support::BaseCache;
90 use Scalar::Util qw/weaken/;
95 Example : $cache = CacheInheritedFromBaseCache->
new($db_adaptor);
96 Description: Creates a
new cache
class which handles all the basics of
97 working with a cache apart from what that cache implementation
98 is (apart from a hash)
101 Caller : BaseAdaptors
107 my ($class, $adaptor) = @_;
109 $class = ref($class) || $class;
110 my $self = bless({}, $class);
112 throw "Need an adaptor instance to delegate calls to" unless $adaptor;
121 Description: Getter setter
for the adaptor
this serves as an ID cacher
for
124 Caller : BaseAdaptors
131 my ($self, $adaptor) = @_;
132 if(defined $adaptor) {
133 assert_ref($adaptor,
'Bio::EnsEMBL::DBSQL::BaseAdaptor',
'adaptor');
134 $self->{
'adaptor'} = $adaptor;
135 weaken($self->{
'adaptor'});
137 return $self->{
'adaptor'};
142 Description: Returns back a Hash implementing
object and also calls
143 C<build_cache()>
for an initialise on demand system
146 Caller : BaseAdaptors and
internal
153 if(! defined $self->{cache}) {
154 my $cache = $self->build_cache();
155 $self->{cache} = $cache;
157 return $self->{cache};
162 Example : $cache->delete_cache();
163 Description: Deletes the cache. Normally used to trigger a C<build_cache()>
167 Caller : BaseAdaptors
174 delete $self->{cache};
180 Arg [1] : String key to retrieve
181 Example : $cache->put(1,
'a'); is($cache->get(1),
'a');
182 Description: Retrieves the value held in the cache. Can
return undef
if the
183 value could not be found
184 Returntype : Scalar value held in the cache or nothing
185 Exceptions : If key was undefined
186 Caller : BaseAdaptors
192 my ($self, $key) = @_;
193 throw "No key given" unless defined $key;
194 my $cache = $self->cache();
195 if(exists $cache->{$key}) {
196 return $cache->{$key};
203 Arg [1] : ArrayRef keys to retrieve
204 Example : is($cache->get_by_list([1,2]), [
'a',
'b']);
205 Description: Retrieves the values held in the cache. If a key cannot be
206 found you get no entry in the resulting array returned.
207 Returntype : ArrayRef of found values
208 Exceptions : If the given ArrayRef was undefined
209 Caller : BaseAdaptors
215 my ($self, $list) = @_;
216 assert_ref($list,
'ARRAY',
'list');
218 my $cache = $self->cache();
219 foreach my $key (@{$list}) {
220 next
if ! defined $key;
221 if(exists $cache->{$key}) {
222 push(@output, $cache->{$key});
230 Arg [1] : String SQL to execute. Should
return the key of
this cache in column 1
231 Arg [2] : ArrayRef Parameters to bind to the specified query
232 Example : $cache->get_by_sql(
'select id from tables where p like ?', [
'val%']);
233 Description: Executes the given SQL statement against the construnction adaptor
's
234 backing database. The found IDs are then passed into C<get_by_list()>
235 where the elements are returned should the cache hold them.
237 Remember if the cache is un-aware of the key or the specific
238 implementation used cannot perform database lookups based on cache misses
239 you will not be able to retrieve the object in question.
240 Returntype : ArrayRef of found values
241 Exceptions : Thrown if SQL and parameters are empty and not the expected types. All
242 other exceptions come from DBI/SQL operations.
243 Caller : BaseAdaptors
249 my ($self, $sql, $params) = @_;
250 throw "No SQL given" unless $sql;
251 assert_ref($params, 'ARRAY
', 'params
');
252 my $helper = $self->adaptor()->dbc()->sql_helper();
253 my $ids = $helper->execute_simple(-SQL => $sql, -PARAMS => $params);
254 return $self->get_by_list($ids);
259 Arg [1] : String key to store
260 Arg [2] : Scalar value to store
261 Example : $cache->put(2, 'b
');
262 Description: Stores a value in the cache. Returns the previous value held
263 under that key if one existed
264 Returntype : Scalar of the previously held value if one existed
265 Exceptions : If key was undefined
266 Caller : BaseAdaptors
272 my ($self, $key, $new) = @_;
273 throw "No key given" unless defined $key;
274 my $cache = $self->cache();
276 if(exists $cache->{$key}) {
277 $old = $cache->{$key};
279 $cache->{$key} = $new;
286 Arg [1] : String key to remove
287 Example : is($cache->remove(1), 'a
');
288 Description: Removes the supplied key from the cache
289 Returntype : Scalar value held in the cache or nothing
290 Exceptions : If key was undefined
291 Caller : BaseAdaptors
297 my ($self, $key) = @_;
298 throw "No key given" unless defined $key;
299 my $cache = $self->cache();
301 if(exists $cache->{$key}) {
302 $old = $cache->{$key};
303 delete $cache->{$key};
311 Example : $cache->clear_cache();
312 Description: Removes all values from the cache but does not delete the cache
316 Caller : BaseAdaptors
324 my $cache = $self->cache();
331 Example : my @keys = $cache->cache_keys();
332 Description: Returns all keys held by the cache
333 Returntype : List of all available keys
335 Caller : BaseAdaptors
342 return keys %{$self->cache()};
348 Example : my @values = $cache->cached_values();
349 Description: Returns all values held by the cache
350 Returntype : List of all available values
352 Caller : BaseAdaptors
359 return values %{$self->cache()};
364 Example : $cache->size();
365 Description: Returns the number of keys currrently held by the cache
366 Returntype : Int $size
368 Caller : BaseAdaptors
375 return scalar($self->cache_keys());
380 Description: Implement to return the required type of cache
383 Caller : BaseAdaptors