def get(self, url): f = LockedFile(self._file, 'r+', 'r') try: f.open_and_lock() if f.is_locked(): cache = _read_or_initialize_cache(f) if url in cache: content, t = cache.get(url, (None, 0)) if _to_timestamp(datetime.datetime.now()) < t + self._max_age: return content return None else: logger.debug('Could not obtain a lock for the cache file.') return None except Exception as e: logger.warning(e, exc_info=True) finally: f.unlock_and_close()
def __init__(self, max_age): """Constructor. Args: max_age: Cache expiration in seconds. """ self._max_age = max_age self._file = os.path.join(tempfile.gettempdir(), FILENAME) f = LockedFile(self._file, 'a+', 'r') try: f.open_and_lock() if f.is_locked(): _read_or_initialize_cache(f) # If we can not obtain the lock, other process or thread must # have initialized the file. except Exception as e: logging.warning(e, exc_info=True) finally: f.unlock_and_close()
def __init__(self, filename, warn_on_readonly=True): """Initialize the class. This will create the file if necessary. """ self._file = LockedFile(filename, 'r+', 'r') self._thread_lock = threading.Lock() self._read_only = False self._warn_on_readonly = warn_on_readonly self._create_file_if_needed() # Cache of deserialized store. This is only valid after the # _MultiStore is locked or _refresh_data_cache is called. This is # of the form of: # # ((key, value), (key, value)...) -> OAuth2Credential # # If this is None, then the store hasn't been read yet. self._data = None
def set(self, url, content): f = LockedFile(self._file, 'r+', 'r') try: f.open_and_lock() if f.is_locked(): cache = _read_or_initialize_cache(f) cache[url] = (content, _to_timestamp(datetime.datetime.now())) # Remove stale cache. for k, (_, timestamp) in list(cache.items()): if _to_timestamp(datetime.datetime.now()) >= timestamp + self._max_age: del cache[k] f.file_handle().truncate(0) f.file_handle().seek(0) json.dump(cache, f.file_handle()) else: logger.debug('Could not obtain a lock for the cache file.') except Exception as e: logger.warning(e, exc_info=True) finally: f.unlock_and_close()