- 
      
- 
        Save schollz/e6f95a7b0fb2e57d6218c0c1181ee22d 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() | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment