##// END OF EJS Templates
lru-cache: fixed iterators on python3
super-admin -
r5007:79d8fb28 default
parent child Browse files
Show More
@@ -1,110 +1,111 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2010-2020 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
30 30 import logging
31 31
32 32 from repoze.lru import LRUCache
33 33 from beaker.container import MemoryNamespaceManager, AbstractDictionaryNSManager
34 34 from rhodecode.lib.utils2 import safe_str
35 35
36 36 log = logging.getLogger(__name__)
37 37
38 38
39 39 class LRUDict(LRUCache):
40 40 """
41 41 Wrapper to provide partial dict access
42 42 """
43 43
44 44 def __setitem__(self, key, value):
45 45 return self.put(key, value)
46 46
47 47 def __getitem__(self, key):
48 48 return self.get(key)
49 49
50 50 def __contains__(self, key):
51 51 return bool(self.get(key))
52 52
53 53 def __delitem__(self, key):
54 54 del self.data[key]
55 55
56 56 def keys(self):
57 return self.data.keys()
57 return list(self.data.keys())
58 58
59 59
60 60 class LRUDictDebug(LRUDict):
61 61 """
62 62 Wrapper to provide some debug options
63 63 """
64 64 def _report_keys(self):
65 elems_cnt = '%s/%s' % (len(self.keys()), self.size)
65
66 66 # trick for pformat print it more nicely
67 67 fmt = '\n'
68 68 for cnt, elem in enumerate(self.keys()):
69 fmt += '%s - %s\n' % (cnt+1, safe_str(elem))
70 log.debug('current LRU keys (%s):%s', elems_cnt, fmt)
69 fmt += f'{cnt+1} - {safe_str(elem)}\n'
70
71 log.debug('current LRU keys (%s/%s):%s', len(self.keys()), self.size, fmt)
71 72
72 73 def __getitem__(self, key):
73 74 self._report_keys()
74 75 return self.get(key)
75 76
76 77
77 78 class MemoryLRUNamespaceManagerBase(MemoryNamespaceManager):
78 79 default_max_items = 10000
79 80
80 81 def _get_factory(self, max_items):
81 82
82 83 def Factory():
83 84 return LRUDict(int(max_items))
84 85 return Factory
85 86
86 87 def __init__(self, namespace, **kwargs):
87 88 AbstractDictionaryNSManager.__init__(self, namespace)
88 89 if 'max_items' in kwargs:
89 90 max_items = kwargs['max_items']
90 91 else:
91 92 max_items = self.default_max_items
92 93
93 94 Factory = self._get_factory(max_items)
94 95
95 96 self.dictionary = MemoryNamespaceManager.namespaces.get(
96 97 self.namespace, Factory)
97 98
98 99
99 100 class MemoryLRUNamespaceManagerDebug(MemoryLRUNamespaceManagerBase):
100 101 """
101 102 A memory namespace manager that return with LRU dicts backend,
102 103 special debug for testing
103 104 """
104 105 default_max_items = 10000
105 106
106 107 def _get_factory(self, max_items):
107 108
108 109 def Factory():
109 110 return LRUDictDebug(int(max_items))
110 111 return Factory
General Comments 0
You need to be logged in to leave comments. Login now