Created
October 12, 2016 05:54
-
-
Save pythonhacker/97726b15c749ba8f8d3b1cb282be62ae to your computer and use it in GitHub Desktop.
A Python dictionary type with keys having specific time to live (TTL)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
import time | |
class TTLDict(dict): | |
""" A dictionary with keys having specific time to live """ | |
def __init__(self, ttl=5): | |
self._ttl = ttl | |
# Internal key mapping keys of _d | |
# to times of access | |
self.__map = {} | |
self._flag = True | |
self._t = threading.Thread(target=self._collect) | |
self._t.setDaemon(True) | |
self._t.start() | |
def _collect(self): | |
""" Collect expired keys in a loop """ | |
while self._flag: | |
now = time.time() | |
keys = self.__map.keys() | |
# Pop the top first 'sz' keys | |
for key in keys: | |
val = self.__map[key] | |
diff = now - val | |
if diff>self._ttl: | |
# print 'Dropping key',key | |
del self[key] | |
# Drop it from this also | |
del self.__map[key] | |
def _set(self, key): | |
self.__map[key] = time.time() | |
def __setitem__(self, key, value): | |
# Set internal map | |
self._set(key) | |
# Set it | |
return dict.__setitem__(self, key, value) | |
def __getitem__(self, key): | |
# Return | |
val = dict.__getitem__(self, key) | |
return val | |
def __del__(self): | |
self._flag = False | |
self._t.join() |
RuntimeError: dictionary changed size during iteration in Python 3.6
Use
for key in list(self):
Is it thread safe?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uses 1 thread per dictionary.