Exemplo n.º 1
0
    def get(self, key):
        c = self.connection.cursor()
        c.execute(
            f"""
        SELECT
          metadata,
          state_data
        FROM {self.table}
        WHERE query=?
        """,
            [key],
        )

        try:
            metadata, data = c.fetchone()
            metadata = json.loads(metadata)
            if metadata.get("status") != "ready":
                return None
        except:
            return None
        try:
            state = State()
            state = state.from_dict(metadata)

            t = state_types_registry().get(state.type_identifier)
            state.data = t.from_bytes(self.decode(data))
            return state
        except:
            logging.exception(f"Cache failed to recover {key}")
            return None
Exemplo n.º 2
0
 def contains(self, key):
     state_path = self.to_path(key)
     if os.path.exists(state_path):
         state = State()
         state = state.from_dict(json.loads(open(state_path).read()))
     else:
         return False
     t = state_types_registry().get(state.type_identifier)
     path = self.to_path(key,
                         prefix="data_",
                         extension=t.default_extension())
     if os.path.exists(path):
         return True
     else:
         return False
Exemplo n.º 3
0
 def get(self, key):
     state_path = self.to_path(key)
     if os.path.exists(state_path):
         state = State()
         state = state.from_dict(json.loads(open(state_path).read()))
     else:
         return None
     t = state_types_registry().get(state.type_identifier)
     path = self.to_path(key,
                         prefix="data_",
                         extension=t.default_extension())
     if os.path.exists(path):
         try:
             state.data = t.from_bytes(open(path, "rb").read())
             return state
         except:
             logging.exception(f"Cache failed to recover {key}")
             return None