-
Notifications
You must be signed in to change notification settings - Fork 0
/
expiringdict.py
164 lines (134 loc) · 4.68 KB
/
expiringdict.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
'''
Dictionary with auto-expiring values for caching purposes.
Expiration happens on any access, object is locked during cleanup from expired
values. Can not store more than max_len elements - the oldest will be deleted.
>>> ExpiringDict(max_len=100, max_age_seconds=10)
The values stored in the following way:
{
key1: (value1, created_time1),
key2: (value2, created_time2)
}
NOTE: iteration over dict and also keys() do not remove expired values!
Usage:
Create a dictionary with capacity for 100 elements and elements expiring in 10 seconds:
from expiringdict import ExpiringDict
cache = ExpiringDict(max_len=100, max_age_seconds=10)
put and get a value there:
cache["key"] = "value"
cache.get("key")
'''
import time
from threading import RLock
try:
from collections import OrderedDict
except ImportError:
# Python < 2.7
from ordereddict import OrderedDict
class ExpiringDict(OrderedDict):
def __init__(self, max_len, max_age_seconds):
assert max_age_seconds >= 0
assert max_len >= 1
OrderedDict.__init__(self)
self.max_len = max_len
self.max_age = max_age_seconds
self.lock = RLock()
def __contains__(self, key):
""" Return True if the dict has a key, else return False. """
try:
with self.lock:
item = OrderedDict.__getitem__(self, key)
if time.time() - item[1] < self.max_age:
return True
else:
del self[key]
except KeyError:
pass
return False
def __getitem__(self, key, with_age=False):
""" Return the item of the dict.
Raises a KeyError if key is not in the map.
"""
with self.lock:
item = OrderedDict.__getitem__(self, key)
item_age = time.time() - item[1]
if item_age < self.max_age:
if with_age:
return item[0], item_age
else:
return item[0]
else:
del self[key]
raise KeyError(key)
def __setitem__(self, key, value):
""" Set d[key] to value. """
with self.lock:
if len(self) == self.max_len:
self.popitem(last=False)
OrderedDict.__setitem__(self, key, (value, time.time()))
def pop(self, key, default=None):
""" Get item from the dict and remove it.
Return default if expired or does not exist. Never raise KeyError.
"""
with self.lock:
try:
item = OrderedDict.__getitem__(self, key)
del self[key]
return item[0]
except KeyError:
return default
def ttl(self, key):
""" Return TTL of the `key` (in seconds).
Returns None for non-existent or expired keys.
"""
key_value, key_age = self.get(key, with_age=True)
if key_age:
key_ttl = self.max_age - key_age
if key_ttl > 0:
return key_ttl
return None
def get(self, key, default=None, with_age=False):
" Return the value for key if key is in the dictionary, else default. "
try:
return self.__getitem__(key, with_age)
except KeyError:
if with_age:
return default, None
else:
return default
def items(self):
""" Return a copy of the dictionary's list of (key, value) pairs. """
r = []
for key in self:
try:
r.append((key, self[key]))
except KeyError:
pass
return r
def values(self):
""" Return a copy of the dictionary's list of values.
See the note for dict.items(). """
r = []
for key in self:
try:
r.append(self[key])
except KeyError:
pass
return r
def fromkeys(self):
" Create a new dictionary with keys from seq and values set to value. "
raise NotImplementedError()
def iteritems(self):
""" Return an iterator over the dictionary's (key, value) pairs. """
raise NotImplementedError()
def itervalues(self):
""" Return an iterator over the dictionary's values. """
raise NotImplementedError()
def viewitems(self):
" Return a new view of the dictionary's items ((key, value) pairs). "
raise NotImplementedError()
def viewkeys(self):
""" Return a new view of the dictionary's keys. """
raise NotImplementedError()
def viewvalues(self):
""" Return a new view of the dictionary's values. """
raise NotImplementedError()