Pylint no-else-return refactoring: tenth (and so far last) set of files
[check_mk.git] / cmk_base / caching.py
blob5de4ff766d48caaf32686ed5aa406550dc08038d
1 #!/usr/bin/env python
2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
9 # | |
10 # | Copyright Mathias Kettner 2014 mk@mathias-kettner.de |
11 # +------------------------------------------------------------------+
13 # This file is part of Check_MK.
14 # The official homepage is at http://mathias-kettner.de/check_mk.
16 # check_mk is free software; you can redistribute it and/or modify it
17 # under the terms of the GNU General Public License as published by
18 # the Free Software Foundation in version 2. check_mk is distributed
19 # in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
20 # out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
21 # PARTICULAR PURPOSE. See the GNU General Public License for more de-
22 # tails. You should have received a copy of the GNU General Public
23 # License along with GNU Make; see the file COPYING. If not, write
24 # to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
25 # Boston, MA 02110-1301 USA.
27 """Managing in-memory caches through the execution time of cmk"""
29 from cmk.exceptions import MKGeneralException
31 import cmk_base.utils
33 class CacheManager(object):
34 def __init__(self):
35 self._caches = {}
38 def exists(self, name):
39 return name in self._caches
42 def _get(self, name, cache_class):
43 try:
44 return self._caches[name]
45 except KeyError:
46 if not issubclass(cache_class, Cache):
47 raise MKGeneralException("The cache object must be a instance of Cache()")
49 self._caches[name] = cache_class()
50 return self._caches[name]
53 def get_dict(self, name):
54 return self._get(name, DictCache)
57 def get_set(self, name):
58 return self._get(name, SetCache)
61 def get_list(self, name):
62 return self._get(name, ListCache)
65 def clear_all(self):
66 for cache in self._caches.values():
67 cache.clear()
70 def dump_sizes(self):
71 sizes = {}
72 for name, cache in self._caches.items():
73 sizes[name] = cmk_base.utils.total_size(cache)
74 return sizes
78 class Cache(object):
79 _populated = False
81 def is_empty(self):
82 """Whether or not there is something in the collection at the moment"""
83 return not self
86 def is_populated(self):
87 """Whether or not the cache has been marked as populated. This is just a flag
88 to tell the caller the initialization state of the cache. It has to be set
89 to True manually by using self.set_populated()"""
90 return self._populated
93 def set_populated(self):
94 self._populated = True
97 def set_not_populated(self):
98 self._populated = False
102 # Just a small wrapper round a dict to get some caching specific functionality
103 # for analysis etc.
104 class DictCache(dict, Cache):
105 def __init__(self, *args, **kwargs):
106 super(DictCache, self).__init__(*args, **kwargs)
107 self._num_hits = 0
108 self._num_misses = 0
109 self._num_sets = 0
112 def __getitem__(self, y):
113 try:
114 result = super(DictCache, self).__getitem__( y)
115 self._num_hits += 1
116 return result
117 except KeyError:
118 self._num_misses += 1
119 raise
122 def __setitem__(self, i, y):
123 self._num_sets += 1
124 super(DictCache, self).__setitem__(i, y)
127 def get_stats(self):
128 return {
129 "sets" : self._num_sets,
130 "hits" : self._num_hits,
131 "misses" : self._num_misses,
132 "items" : len(self),
136 def clear(self):
137 super(DictCache, self).clear()
138 self.set_not_populated()
142 class SetCache(set, Cache):
143 def clear(self):
144 super(SetCache, self).clear()
145 self.set_not_populated()
149 class ListCache(list, Cache):
150 def clear(self):
151 del self[:] # Clear the list in place
152 self.set_not_populated()