NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

ttl_cache.py (2916B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\caching\client\ttl_cache.py
      2 from collections import MutableMapping
      3 from datetime import datetime, timedelta
      4 NO_CACHED_DATA = 'ttl_cache_no_cached'
      5 
      6 class TTLCacheEntry(object):
      7 
      8     def __init__(self, data, expiration):
      9         self.data = data
     10         self.expiration = expiration
     11 
     12     def __repr__(self):
     13         return '<TTLCacheEntry>' + self.__dict__.__repr__()
     14 
     15 
     16 class TTLCache(MutableMapping):
     17 
     18     def __init__(self, ttl):
     19         if not isinstance(ttl, timedelta):
     20             raise TypeError('ttl must be a datetime.timedelta')
     21         self._ttl = ttl
     22         self._cache = {}
     23 
     24     def __setitem__(self, key, value):
     25         self.store(key, value)
     26 
     27     def __delitem__(self, key):
     28         self.remove(key)
     29 
     30     def __getitem__(self, key):
     31         return self.get(key)
     32 
     33     def __iter__(self):
     34         for key in self._cache.keys():
     35             yield key
     36 
     37     def __len__(self):
     38         return len(self._cache)
     39 
     40     def __contains__(self, key):
     41         return key in self._cache
     42 
     43     def __repr__(self):
     44         return '<TTLCache>' + self.__dict__.__repr__()
     45 
     46     def store(self, key, data):
     47         now = datetime.utcnow()
     48         self._cache[key] = TTLCacheEntry(data, now + self._ttl)
     49 
     50     def reset_expiration(self, key):
     51         if key not in self._cache:
     52             raise KeyError('Tried to get expired data for a key that is not in the cache', key)
     53         entry = self._cache[key]
     54         entry.expiration = datetime.utcnow() + self._ttl
     55 
     56     def remove(self, key):
     57         self._cache.pop(key, None)
     58 
     59     def get(self, key, default_value = NO_CACHED_DATA):
     60         if key not in self._cache:
     61             return default_value
     62         entry = self._cache[key]
     63         now = datetime.utcnow()
     64         if entry.expiration <= now:
     65             return default_value
     66         return entry.data
     67 
     68     def itervalues_ignore_expiry(self):
     69         for key in self:
     70             yield self.get_ignore_expiry(key)
     71 
     72     def get_ignore_expiry(self, key, default_value = NO_CACHED_DATA):
     73         if key not in self._cache:
     74             return default_value
     75         entry = self._cache[key]
     76         return entry.data
     77 
     78     def get_expiration(self, key):
     79         if key not in self._cache:
     80             raise KeyError('key not found in cache', key)
     81         entry = self._cache[key]
     82         return entry.expiration
     83 
     84     def clear_cache(self):
     85         self._cache.clear()
     86 
     87     def set_ttl(self, ttl):
     88         if not isinstance(ttl, timedelta):
     89             raise TypeError('ttl must be a datetime.timedelta')
     90         self._ttl = ttl
     91 
     92     def expire(self, key):
     93         if key not in self._cache:
     94             raise KeyError('key not found in cache', key)
     95         entry = self._cache[key]
     96         entry.expiration = datetime.utcnow()
     97 
     98     def has_cached_data_for_key(self, key):
     99         return self.get(key, NO_CACHED_DATA) != NO_CACHED_DATA