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.
27 """Managing in-memory caches through the execution time of cmk"""
29 from cmk
.exceptions
import MKGeneralException
33 class CacheManager(object):
38 def exists(self
, name
):
39 return name
in self
._caches
42 def _get(self
, name
, cache_class
):
44 return self
._caches
[name
]
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
)
66 for cache
in self
._caches
.values():
72 for name
, cache
in self
._caches
.items():
73 sizes
[name
] = cmk_base
.utils
.total_size(cache
)
82 """Whether or not there is something in the collection at the moment"""
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
104 class DictCache(dict, Cache
):
105 def __init__(self
, *args
, **kwargs
):
106 super(DictCache
, self
).__init
__(*args
, **kwargs
)
112 def __getitem__(self
, y
):
114 result
= super(DictCache
, self
).__getitem
__( y
)
118 self
._num
_misses
+= 1
122 def __setitem__(self
, i
, y
):
124 super(DictCache
, self
).__setitem
__(i
, y
)
129 "sets" : self
._num
_sets
,
130 "hits" : self
._num
_hits
,
131 "misses" : self
._num
_misses
,
137 super(DictCache
, self
).clear()
138 self
.set_not_populated()
142 class SetCache(set, Cache
):
144 super(SetCache
, self
).clear()
145 self
.set_not_populated()
149 class ListCache(list, Cache
):
151 del self
[:] # Clear the list in place
152 self
.set_not_populated()