-
Notifications
You must be signed in to change notification settings - Fork 27
/
cache.py
236 lines (185 loc) · 6.87 KB
/
cache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# Copyright (c) 2015 Marin Atanasov Nikolov <dnaeon@gmail.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer
# in this position and unchanged.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
The vConnector caching module
"""
import logging
import threading
from time import time
from collections import OrderedDict, namedtuple
from vconnector.exceptions import CacheException
__all__ = ['CachedObject', 'CacheInventory']
_CachedObjectInfo = namedtuple('CachedObjectInfo', ['name', 'hits', 'ttl', 'timestamp'])
class CachedObject(object):
def __init__(self, name, obj, ttl):
"""
Initializes a new cached object
Args:
name (str): Human readable name for the cached entry
obj (type): Object to be cached
ttl (int): The TTL in seconds for the cached object
"""
self.hits = 0
self.name = name
self.obj = obj
self.ttl = ttl
self.timestamp = time()
class CacheInventory(object):
"""
Inventory for cached objects
"""
def __init__(self, maxsize=0, housekeeping=0):
"""
Initializes a new cache inventory
Args:
maxsize (int): Upperbound limit on the number of items
that will be stored in the cache inventory
housekeeping (int): Time in minutes to perform periodic
cache housekeeping
Raises:
CacheException
"""
if maxsize < 0:
raise CacheException('Cache inventory size cannot be negative')
if housekeeping < 0:
raise CacheException('Cache housekeeping period cannot be negative')
self._cache = OrderedDict()
self.maxsize = maxsize
self.housekeeping = housekeeping * 60.0
self.lock = threading.RLock()
self._schedule_housekeeper()
def __len__(self):
with self.lock:
return len(self._cache)
def __contains__(self, key):
with self.lock:
if key not in self._cache:
return False
item = self._cache[key]
return not self._has_expired(item)
def _has_expired(self, item):
"""
Checks if a cached item has expired and removes it if needed
Args:
item (CachedObject): A cached object to lookup
Returns:
bool: True if the item has expired, False otherwise
"""
with self.lock:
if time() > item.timestamp + item.ttl:
logging.debug(
'Object %s has expired and will be removed from cache',
self.info(item.name)
)
self._cache.pop(item.name)
return True
return False
def _schedule_housekeeper(self):
"""
Schedules the next run of the housekeeper
"""
if self.housekeeping > 0:
t = threading.Timer(
interval=self.housekeeping,
function=self._housekeeper
)
t.setDaemon(True)
t.start()
def _housekeeper(self):
"""
Remove expired entries from the cache on regular basis
"""
with self.lock:
expired = 0
logging.info(
'Starting cache housekeeper [%d item(s) in cache]',
len(self._cache)
)
items = list(self._cache.values())
for item in items:
if self._has_expired(item):
expired += 1
logging.info(
'Cache housekeeper completed [%d item(s) removed from cache]',
expired
)
self._schedule_housekeeper()
def add(self, obj):
"""
Add an item to the cache inventory
If the upperbound limit has been reached then the last item
is being removed from the inventory.
Args:
obj (CachedObject): A CachedObject instance to be added
Raises:
CacheException
"""
if not isinstance(obj, CachedObject):
raise CacheException('Need a CachedObject instance to add in the cache')
with self.lock:
if 0 < self.maxsize == len(self._cache):
popped = self._cache.popitem(last=False)
logging.debug(
'Cache maxsize reached, removing %s',
self.info(name=popped.name)
)
self._cache[obj.name] = obj
logging.debug('Adding object to cache %s', self.info(name=obj.name))
def get(self, name):
"""
Retrieve an object from the cache inventory
Args:
name (str): Name of the cache item to retrieve
Returns:
The cached object if found, None otherwise
"""
with self.lock:
if name not in self._cache:
return None
item = self._cache[name]
if self._has_expired(item):
return None
item.hits += 1
logging.debug(
'Returning object from cache %s',
self.info(name=item.name)
)
return item.obj
def clear(self):
"""
Remove all items from the cache
"""
with self.lock:
self._cache.clear()
def info(self, name):
"""
Get statistics about a cached object
Args:
name (str): Name of the cached object
"""
with self.lock:
if name not in self._cache:
return None
item = self._cache[name]
return _CachedObjectInfo(item.name, item.hits, item.ttl, item.timestamp)