def write(self, query: Query, data) -> bool: success = True try: self.callback_write(query, data) except: Log.error("CacheConnector.write(%s, %s): Cannot write cache:\n%s" % (traceback.format_exc(), query, data)) success = False return success
def read(self, query: Query): (data, success) = (None, False) try: data = self.callback_read(query) success = (data != None) except: Log.error("CacheConnector.read(%s): Cannot read cache:\n%s" % (query, traceback.format_exc())) return (data, success)
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)
def error(self, message): Log.error(message)