2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
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
33 class CacheManager(object):
37 def exists(self
, name
):
38 return name
in self
._caches
40 def _get(self
, name
, cache_class
):
42 return self
._caches
[name
]
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
)
60 for cache
in self
._caches
.values():
65 for name
, cache
in self
._caches
.items():
66 sizes
[name
] = cmk_base
.utils
.total_size(cache
)
74 """Whether or not there is something in the collection at the moment"""
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
):
92 super(DictCache
, self
).clear()
93 self
.set_not_populated()
96 ## Just a small wrapper round a dict to get some caching specific functionality
98 #class DictCacheStats(DictCache):
99 # def __init__(self, *args, **kwargs):
100 # super(DictCacheStats, self).__init__(*args, **kwargs)
102 # self._num_misses = 0
105 # def __getitem__(self, y):
107 # result = super(DictCacheStats, self).__getitem__(y)
108 # self._num_hits += 1
111 # self._num_misses += 1
114 # def __setitem__(self, i, y):
115 # self._num_sets += 1
116 # super(DictCacheStats, self).__setitem__(i, y)
118 # def get_stats(self):
120 # "sets": self._num_sets,
121 # "hits": self._num_hits,
122 # "misses": self._num_misses,
123 # "items": len(self),
127 class SetCache(set, Cache
):
129 super(SetCache
, self
).clear()
130 self
.set_not_populated()
133 class ListCache(list, Cache
):
135 del self
[:] # Clear the list in place
136 self
.set_not_populated()