##// END OF EJS Templates
caches: allow debug of max_size reached by the MemoryLRU cache.
marcink -
r2887:2a9499df default
parent child Browse files
Show More
@@ -1,174 +1,181 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2015-2018 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 import time
21 21 import errno
22 22 import logging
23 23
24 24 import gevent
25 25
26 26 from dogpile.cache.backends import memory as memory_backend
27 27 from dogpile.cache.backends import file as file_backend
28 28 from dogpile.cache.backends import redis as redis_backend
29 29 from dogpile.cache.backends.file import NO_VALUE, compat, FileLock
30 30 from dogpile.cache.util import memoized_property
31 31 from lru import LRU as LRUDict
32 32
33 33
34 34 _default_max_size = 1024
35 35
36 36 log = logging.getLogger(__name__)
37 37
38 38
39 39 class LRUMemoryBackend(memory_backend.MemoryBackend):
40 40 pickle_values = False
41 41
42 42 def __init__(self, arguments):
43 43 max_size = arguments.pop('max_size', _default_max_size)
44 arguments['cache_dict'] = LRUDict(max_size)
44 callback = None
45 if arguments.pop('log_max_size_reached', None):
46 def evicted(key, value):
47 log.debug(
48 'LRU: evicting key `%s` due to max size %s reach', key, max_size)
49 callback = evicted
50
51 arguments['cache_dict'] = LRUDict(max_size, callback=callback)
45 52 super(LRUMemoryBackend, self).__init__(arguments)
46 53
47 54 def delete(self, key):
48 55 if self._cache.has_key(key):
49 56 del self._cache[key]
50 57
51 58 def delete_multi(self, keys):
52 59 for key in keys:
53 60 if self._cache.has_key(key):
54 61 del self._cache[key]
55 62
56 63
57 64 class Serializer(object):
58 65 def _dumps(self, value):
59 66 return compat.pickle.dumps(value)
60 67
61 68 def _loads(self, value):
62 69 return compat.pickle.loads(value)
63 70
64 71
65 72 class CustomLockFactory(FileLock):
66 73
67 74 @memoized_property
68 75 def _module(self):
69 76 import fcntl
70 77 flock_org = fcntl.flock
71 78
72 79 def gevent_flock(fd, operation):
73 80 """
74 81 Gevent compatible flock
75 82 """
76 83 # set non-blocking, this will cause an exception if we cannot acquire a lock
77 84 operation |= fcntl.LOCK_NB
78 85 start_lock_time = time.time()
79 86 timeout = 60 * 5 # 5min
80 87 while True:
81 88 try:
82 89 flock_org(fd, operation)
83 90 # lock has been acquired
84 91 break
85 92 except (OSError, IOError) as e:
86 93 # raise on other errors than Resource temporarily unavailable
87 94 if e.errno != errno.EAGAIN:
88 95 raise
89 96 elif (time.time() - start_lock_time) > timeout:
90 97 # waited to much time on a lock, better fail than loop for ever
91 98 raise
92 99
93 100 log.debug('Failed to acquire lock, retry in 0.1')
94 101 gevent.sleep(0.1)
95 102
96 103 fcntl.flock = gevent_flock
97 104 return fcntl
98 105
99 106
100 107 class FileNamespaceBackend(Serializer, file_backend.DBMBackend):
101 108
102 109 def __init__(self, arguments):
103 110 arguments['lock_factory'] = CustomLockFactory
104 111 super(FileNamespaceBackend, self).__init__(arguments)
105 112
106 113 def list_keys(self, prefix=''):
107 114 def cond(v):
108 115 if not prefix:
109 116 return True
110 117
111 118 if v.startswith(prefix):
112 119 return True
113 120 return False
114 121
115 122 with self._dbm_file(True) as dbm:
116 123
117 124 return filter(cond, dbm.keys())
118 125
119 126 def get_store(self):
120 127 return self.filename
121 128
122 129 def get(self, key):
123 130 with self._dbm_file(False) as dbm:
124 131 if hasattr(dbm, 'get'):
125 132 value = dbm.get(key, NO_VALUE)
126 133 else:
127 134 # gdbm objects lack a .get method
128 135 try:
129 136 value = dbm[key]
130 137 except KeyError:
131 138 value = NO_VALUE
132 139 if value is not NO_VALUE:
133 140 value = self._loads(value)
134 141 return value
135 142
136 143 def set(self, key, value):
137 144 with self._dbm_file(True) as dbm:
138 145 dbm[key] = self._dumps(value)
139 146
140 147 def set_multi(self, mapping):
141 148 with self._dbm_file(True) as dbm:
142 149 for key, value in mapping.items():
143 150 dbm[key] = self._dumps(value)
144 151
145 152
146 153 class RedisPickleBackend(Serializer, redis_backend.RedisBackend):
147 154 def list_keys(self, prefix=''):
148 155 if prefix:
149 156 prefix = prefix + '*'
150 157 return self.client.keys(prefix)
151 158
152 159 def get_store(self):
153 160 return self.client.connection_pool
154 161
155 162 def set(self, key, value):
156 163 if self.redis_expiration_time:
157 164 self.client.setex(key, self.redis_expiration_time,
158 165 self._dumps(value))
159 166 else:
160 167 self.client.set(key, self._dumps(value))
161 168
162 169 def set_multi(self, mapping):
163 170 mapping = dict(
164 171 (k, self._dumps(v))
165 172 for k, v in mapping.items()
166 173 )
167 174
168 175 if not self.redis_expiration_time:
169 176 self.client.mset(mapping)
170 177 else:
171 178 pipe = self.client.pipeline()
172 179 for key, value in mapping.items():
173 180 pipe.setex(key, self.redis_expiration_time, value)
174 181 pipe.execute()
General Comments 0
You need to be logged in to leave comments. Login now