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  from buildbot import config 
18  from twisted.application import service 
19   
20 -class CacheManager(config.ReconfigurableServiceMixin, service.Service):
21 """ 22 A manager for a collection of caches, each for different types of objects 23 and with potentially-overlapping key spaces. 24 25 There is generally only one instance of this class, available at 26 C{master.caches}. 27 """ 28 29 # a cache of length one still has many benefits: it collects objects that 30 # remain referenced elsewhere; it collapses simultaneous misses into one 31 # miss function; and it will optimize repeated fetches of the same object. 32 DEFAULT_CACHE_SIZE = 1 33
34 - def __init__(self):
35 self.setName('caches') 36 self.config = {} 37 self._caches = {}
38
39 - def get_cache(self, cache_name, miss_fn):
40 """ 41 Get an L{AsyncLRUCache} object with the given name. If such an object 42 does not exist, it will be created. Since the cache is permanent, this 43 method can be called only once, e.g., in C{startService}, and it value 44 stored indefinitely. 45 46 @param cache_name: name of the cache (usually the name of the type of 47 object it stores) 48 @param miss_fn: miss function for the cache; see L{AsyncLRUCache} 49 constructor. 50 @returns: L{AsyncLRUCache} instance 51 """ 52 try: 53 return self._caches[cache_name] 54 except KeyError: 55 max_size = self.config.get(cache_name, self.DEFAULT_CACHE_SIZE) 56 assert max_size >= 1 57 c = self._caches[cache_name] = lru.AsyncLRUCache(miss_fn, max_size) 58 return c
59
60 - def reconfigService(self, new_config):
61 self.config = new_config.caches 62 for name, cache in self._caches.iteritems(): 63 cache.set_max_size(new_config.caches.get(name, 64 self.DEFAULT_CACHE_SIZE)) 65 66 return config.ReconfigurableServiceMixin.reconfigService(self, 67 new_config)
68
69 - def get_metrics(self):
70 return dict([ 71 (n, dict(hits=c.hits, refhits=c.refhits, 72 misses=c.misses, max_size=c.max_size)) 73 for n, c in self._caches.iteritems()])
74