Skip to content

Instantly share code, notes, and snippets.

@pythonhacker
Created October 12, 2016 05:54
Show Gist options
  • Save pythonhacker/97726b15c749ba8f8d3b1cb282be62ae to your computer and use it in GitHub Desktop.
Save pythonhacker/97726b15c749ba8f8d3b1cb282be62ae to your computer and use it in GitHub Desktop.
A Python dictionary type with keys having specific time to live (TTL)
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()
@bmwcmw
Copy link

bmwcmw commented Jul 26, 2018

RuntimeError: dictionary changed size during iteration in Python 3.6

Use
for key in list(self):

@jeffry01
Copy link

Is it thread safe?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment