Module: check_mk
Branch: master
Commit: c0f9e48505f3a9730f74aab08d9673c297f26205
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=c0f9e48505f3a9…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Thu May 18 12:05:55 2017 +0200
Added get_stats() method to perform some basic cache usage tests
Change-Id: Ia584240e2f4a8abe7e780d6462aab45ec7233c11
---
cmk_base/caching.py | 30 +++++++++++++++++++++++++++++-
1 file changed, 29 insertions(+), 1 deletion(-)
diff --git a/cmk_base/caching.py b/cmk_base/caching.py
index de679e0..0b95948 100644
--- a/cmk_base/caching.py
+++ b/cmk_base/caching.py
@@ -83,7 +83,35 @@ class Cache(object):
# Just a small wrapper round a dict to get some caching specific functionality
# for analysis etc.
class DictCache(dict, Cache):
- pass
+ def __init__(self, *args, **kwargs):
+ super(Cache, self).__init__(*args, **kwargs)
+ self._num_hits = 0
+ self._num_misses = 0
+ self._num_sets = 0
+
+
+ def __getitem__(self, y):
+ try:
+ result = super(DictCache, self).__getitem__( y)
+ self._num_hits += 1
+ return result
+ except KeyError:
+ self._num_misses += 1
+ raise
+
+
+ def __setitem__(self, i, y):
+ self._num_sets += 1
+ super(DictCache, self).__setitem__(i, y)
+
+
+ def get_stats(self):
+ return {
+ "sets" : self._num_sets,
+ "hits" : self._num_hits,
+ "misses" : self._num_misses,
+ "items" : len(self),
+ }
class SetCache(set, Cache):