def callback_write(self, query :Query, data): cache_filename = self.make_cache_filename(query) directory = os.path.dirname(cache_filename) mkdir(directory) check_writable_directory(directory) with open(cache_filename, self.write_mode) as f: self.callback_dump(data, f)
def install_cache(cache_filename: str = None): if not cache_filename: directory = DEFAULT_CACHE_STORAGE_BASE_DIR cache_filename = os.path.join(DEFAULT_CACHE_STORAGE_BASE_DIR, "requests_cache") else: directory = os.path.dirname(cache_filename) mkdir(directory) check_writable_directory(directory) requests_cache.install_cache(cache_filename, expire_after=DEFAULT_CACHE_STORAGE_LIFETIME)
def __init__( self, load_entries, cache_filename :str, load_cache, save_cache, read_mode, write_mode, with_cache :bool = True ): loaded_from_cache = False if with_cache: try: with open(cache_filename, read_mode) as f: Log.info("%s: Loading cache from [%s]" % (type(self), cache_filename)) entries = load_cache(f) Log.info("Loaded %d entries" % len(entries)) loaded_from_cache = True except FileNotFoundError: Log.debug("%s: Cache [%s] not found" % (type(self), cache_filename)) pass except Exception as e: Log.debug("%s: Cache [%s] corrupted" % (type(self), cache_filename)) Log.error(e) pass # Parse the input data (if needed) if not loaded_from_cache: entries = load_entries() Log.info("Loaded %d entries" % len(entries)) # Save into cache (if needed) if with_cache and not loaded_from_cache: Log.info("%s: Saving data into cache [%s]" % (type(self), cache_filename)) mkdir(os.path.dirname(cache_filename)) with open(cache_filename, write_mode) as f: save_cache(entries, f) super().__init__(entries)