Licenses: Updated the list of licenses and added a PDF containing all license texts
[check_mk.git] / cmk_base / caching.py
blobfa2de2928be6daad03d2d7eee5fd3eaca2e7991a
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.
26 """Managing in-memory caches through the execution time of cmk"""
28 from cmk.utils.exceptions import MKGeneralException
30 import cmk_base.utils
33 class CacheManager(object):
34 def __init__(self):
35 self._caches = {}
37 def exists(self, name):
38 return name in self._caches
40 def _get(self, name, cache_class):
41 try:
42 return self._caches[name]
43 except KeyError:
44 if not issubclass(cache_class, Cache):
45 raise MKGeneralException("The cache object must be a instance of Cache()")
47 self._caches[name] = cache_class()
48 return self._caches[name]
50 def get_dict(self, name):
51 return self._get(name, DictCache)
53 def get_set(self, name):
54 return self._get(name, SetCache)
56 def get_list(self, name):
57 return self._get(name, ListCache)
59 def clear_all(self):
60 for cache in self._caches.values():
61 cache.clear()
63 def dump_sizes(self):
64 sizes = {}
65 for name, cache in self._caches.items():
66 sizes[name] = cmk_base.utils.total_size(cache)
67 return sizes
70 class Cache(object):
71 _populated = False
73 def is_empty(self):
74 """Whether or not there is something in the collection at the moment"""
75 return not self
77 def is_populated(self):
78 """Whether or not the cache has been marked as populated. This is just a flag
79 to tell the caller the initialization state of the cache. It has to be set
80 to True manually by using self.set_populated()"""
81 return self._populated
83 def set_populated(self):
84 self._populated = True
86 def set_not_populated(self):
87 self._populated = False
90 class DictCache(dict, Cache):
91 def clear(self):
92 super(DictCache, self).clear()
93 self.set_not_populated()
96 ## Just a small wrapper round a dict to get some caching specific functionality
97 ## for analysis etc.
98 #class DictCacheStats(DictCache):
99 # def __init__(self, *args, **kwargs):
100 # super(DictCacheStats, self).__init__(*args, **kwargs)
101 # self._num_hits = 0
102 # self._num_misses = 0
103 # self._num_sets = 0
105 # def __getitem__(self, y):
106 # try:
107 # result = super(DictCacheStats, self).__getitem__(y)
108 # self._num_hits += 1
109 # return result
110 # except KeyError:
111 # self._num_misses += 1
112 # raise
114 # def __setitem__(self, i, y):
115 # self._num_sets += 1
116 # super(DictCacheStats, self).__setitem__(i, y)
118 # def get_stats(self):
119 # return {
120 # "sets": self._num_sets,
121 # "hits": self._num_hits,
122 # "misses": self._num_misses,
123 # "items": len(self),
127 class SetCache(set, Cache):
128 def clear(self):
129 super(SetCache, self).clear()
130 self.set_not_populated()
133 class ListCache(list, Cache):
134 def clear(self):
135 del self[:] # Clear the list in place
136 self.set_not_populated()