Package buildbot :: Package process :: Module cache
[frames] | no frames]

Source Code for Module buildbot.process.cache

 1  # This file is part of Buildbot.  Buildbot is free software: you can 
 2  # redistribute it and/or modify it under the terms of the GNU General Public 
 3  # License as published by the Free Software Foundation, version 2. 
 4  # 
 5  # This program is distributed in the hope that it will be useful, but WITHOUT 
 6  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 7  # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
 8  # details. 
 9  # 
10  # You should have received a copy of the GNU General Public License along with 
11  # this program; if not, write to the Free Software Foundation, Inc., 51 
12  # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
13  # 
14  # Copyright Buildbot Team Members 
15   
16  from buildbot.util import lru 
17   
18 -class CacheManager(object):
19 """ 20 A manager for a collection of caches, each for different types of objects 21 and with potentially-overlapping key spaces. 22 23 There is generally only one instance of this class, available at 24 C{master.caches}. 25 """ 26 27 # a cache of length one still has many benefits: it collects objects that 28 # remain referenced elsewhere; it collapses simultaneous misses into one 29 # miss function; and it will optimize repeated fetches of the same object. 30 DEFAULT_CACHE_SIZE = 1 31
32 - def __init__(self):
33 self.config = {} 34 self._caches = {}
35
36 - def get_cache(self, cache_name, miss_fn):
37 """ 38 Get an L{AsyncLRUCache} object with the given name. If such an object 39 does not exist, it will be created. Since the cache is permanent, this 40 method can be called only once, e.g., in C{startService}, and it value 41 stored indefinitely. 42 43 @param cache_name: name of the cache (usually the name of the type of 44 object it stores) 45 @param miss_fn: miss function for the cache; see L{AsyncLRUCache} 46 constructor. 47 @returns: L{AsyncLRUCache} instance 48 """ 49 try: 50 return self._caches[cache_name] 51 except KeyError: 52 max_size = self.config.get(cache_name, self.DEFAULT_CACHE_SIZE) 53 assert max_size >= 1 54 c = self._caches[cache_name] = lru.AsyncLRUCache(miss_fn, max_size) 55 return c
56
57 - def load_config(self, new_config):
58 self.config = new_config 59 for name, cache in self._caches.iteritems(): 60 cache.set_max_size(new_config.get(name, self.DEFAULT_CACHE_SIZE))
61
62 - def get_metrics(self):
63 return dict([ 64 (n, dict(hits=c.hits, refhits=c.refhits, 65 misses=c.misses, max_size=c.max_size)) 66 for n, c in self._caches.iteritems()])
67