##// END OF EJS Templates
pep8: cleanup code of lru_debug
marcink -
r2077:79954a19 default
parent child Browse files
Show More
@@ -1,96 +1,96 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2010-2017 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21 """
22 22 Custom LRU memory manager for debugging purposes. It allows to track the keys
23 23 and the state of LRU dict.
24 24
25 25 inrae.cache is licensed under LRUDict is licensed under ZPL license
26 26 This software is Copyright (c) Zope Corporation (tm) and
27 27 Contributors. All rights reserved.
28 28
29 29 TODO: marcink, we might think of replacing the LRUDict with lru-dict library
30 30 written in C.
31 31
32 32 eg difference in speed:
33 33
34 34 LRUDictC Time : 0.00025 s, Memory : 110592 Kb
35 35 LRUDict Time : 0.00369 s, Memory : 147456 Kb
36 36 """
37 37 import logging
38 38
39 39 from infrae.cache.beakerext.lru import LRUDict
40 40 from beaker.container import MemoryNamespaceManager, AbstractDictionaryNSManager
41 41 from rhodecode.lib.utils2 import safe_str
42 42
43 43 log = logging.getLogger(__name__)
44 44
45 45
46 46 class LRUDictDebug(LRUDict):
47 47 """
48 48 Wrapper to provide some debug options
49 49 """
50 50 def _report_keys(self):
51 51 elems_cnt = '%s/%s' % (len(self.keys()), self.size)
52 52 # trick for pformat print it more nicely
53 53 fmt = '\n'
54 54 for cnt, elem in enumerate(self.keys()):
55 55 fmt += '%s - %s\n' % (cnt+1, safe_str(elem))
56 56 log.debug('current LRU keys (%s):%s' % (elems_cnt, fmt))
57 57
58 58 def __getitem__(self, key):
59 59 self._report_keys()
60 60 return self.get(key)
61 61
62 62
63 63 class MemoryLRUNamespaceManagerBase(MemoryNamespaceManager):
64 64 default_max_items = 10000
65 65
66 66 def _get_factory(self, max_items):
67 67
68 68 def Factory():
69 69 return LRUDict(int(max_items))
70 70 return Factory
71 71
72 72 def __init__(self, namespace, **kwargs):
73 73 AbstractDictionaryNSManager.__init__(self, namespace)
74 if kwargs.has_key('max_items'):
74 if 'max_items' in kwargs:
75 75 max_items = kwargs['max_items']
76 76 else:
77 77 max_items = self.default_max_items
78 78
79 79 Factory = self._get_factory(max_items)
80 80
81 81 self.dictionary = MemoryNamespaceManager.namespaces.get(
82 82 self.namespace, Factory)
83 83
84 84
85 85 class MemoryLRUNamespaceManagerDebug(MemoryLRUNamespaceManagerBase):
86 86 """
87 87 A memory namespace manager that return with LRU dicts backend,
88 88 special debug for testing
89 89 """
90 90 default_max_items = 10000
91 91
92 92 def _get_factory(self, max_items):
93 93
94 94 def Factory():
95 95 return LRUDictDebug(int(max_items))
96 96 return Factory
General Comments 0
You need to be logged in to leave comments. Login now