Archive Ensembl HomeArchive Ensembl Home
SeqRegionCache.pm
Go to the documentation of this file.
00001 =head1 LICENSE
00002 
00003   Copyright (c) 1999-2012 The European Bioinformatics Institute and
00004   Genome Research Limited.  All rights reserved.
00005 
00006   This software is distributed under a modified Apache license.
00007   For license details, please see
00008 
00009     http://www.ensembl.org/info/about/code_licence.html
00010 
00011 =head1 CONTACT
00012 
00013   Please email comments or questions to the public Ensembl
00014   developers list at <dev@ensembl.org>.
00015 
00016   Questions may also be sent to the Ensembl help desk at
00017   <helpdesk@ensembl.org>.
00018 
00019 =cut
00020 
00021 =head1 NAME
00022 
00023 Bio::EnsEMBL::Utils::SeqRegionCache - A shared LRU cache of information about
00024 seq_regions
00025 
00026 =head1 SYNOPSIS
00027 
00028   use Bio::EnsEMBL::DBSQL::DBAdaptor;
00029 
00030   $db = Bio::EnsEMBL::DBSQL::DBAdaptor->new(...);
00031 
00032   $seq_region_cache = $db->get_SeqRegionCache();
00033 
00034   $key = "$seq_region_name:$coord_system_id";
00035 
00036   $array = $seq_region_cache->{$key};
00037 
00038   if ($array) {
00039     $name   = $array->[1];
00040     $length = $array->[3];
00041   } else {
00042     # cache miss, get the info from the database
00043     # ...
00044 
00045     # cache the retrieved information
00046     $seq_region_cache->{$key} = [
00047       $seq_region_id,   $seq_region_name,
00048       $coord_system_id, $seq_region_length
00049     ];
00050   }
00051 
00052 =head1 DESCRIPTION
00053 
00054 This module is simply a convenient place to put a cache of sequence
00055 region information which is shared by several adaptors for a given
00056 database.
00057 
00058 =head1 METHODS
00059 
00060 =cut
00061 
00062 use strict;
00063 use Bio::EnsEMBL::Utils::Cache;
00064 
00065 package Bio::EnsEMBL::Utils::SeqRegionCache;
00066 
00067 our $SEQ_REGION_CACHE_SIZE = 40000;
00068 
00069 
00070 
00071 sub new {
00072   my $class = shift;
00073 
00074   my %id_cache;
00075   my %name_cache;
00076 
00077   #
00078   # the items to cache should be listrefs to
00079   # [ sr_id, sr_name, cs_id, sr_length ]
00080   #
00081   # The name cache key is "sr_name:cs_id"
00082   # The id cache is keyed on "sr_id"
00083   #
00084 
00085   tie(%name_cache, 'Bio::EnsEMBL::Utils::Cache', $SEQ_REGION_CACHE_SIZE);
00086   tie(%id_cache, 'Bio::EnsEMBL::Utils::Cache', $SEQ_REGION_CACHE_SIZE);
00087 
00088   return bless {'name_cache' => \%name_cache, 
00089                 'id_cache' => \%id_cache}, $class;
00090 }
00091 
00092 
00093 1;
00094 
00095 
00096 
00097