def _build_filename(cache_key: str, object_type: CacheObjectType, cache_type=None) -> Path: path = EnvironmentSettings.get_cache_path( cache_type) / object_type.name.lower() PathBuilder.build(path) return path / f"{cache_key}.pickle"
def add(params: tuple, caching_object, object_type: CacheObjectType = CacheObjectType.OTHER, cache_type=None): PathBuilder.build(EnvironmentSettings.get_cache_path(cache_type)) h = CacheHandler.generate_cache_key(params) filename = CacheHandler._build_filename(cache_key=h, object_type=object_type, cache_type=cache_type) with filename.open("wb") as file: dill.dump(caching_object, file, protocol=pickle.HIGHEST_PROTOCOL)
def test_memo_with_object_type(self): fn = lambda: "abc" cache_key = "a123" obj = CacheHandler.memo(cache_key, fn, CacheObjectType.ENCODING) self.assertEqual("abc", obj) self.assertTrue( os.path.isfile(EnvironmentSettings.get_cache_path() / f"encoding/{cache_key}.pickle")) os.remove( CacheHandler._build_filename(cache_key, CacheObjectType.ENCODING))
def test_get(self): params = (("k1", 1), ("k2", 2)) obj = "object_example" object_type = CacheObjectType.OTHER h = hashlib.sha256(str(params).encode('utf-8')).hexdigest() filename = EnvironmentSettings.get_cache_path( ) / "{}/{}.pickle".format(CacheObjectType.OTHER.name.lower(), h) with open(filename, "wb") as file: pickle.dump(obj, file) obj2 = CacheHandler.get(params, object_type) self.assertEqual(obj, obj2) os.remove(filename)
def add_by_key(cache_key: str, caching_object, object_type: CacheObjectType = CacheObjectType.OTHER, cache_type=None): PathBuilder.build(EnvironmentSettings.get_cache_path(cache_type)) filename = CacheHandler._build_filename(cache_key=cache_key, object_type=object_type, cache_type=cache_type) try: with filename.open("wb") as file: dill.dump(caching_object, file, protocol=pickle.HIGHEST_PROTOCOL) except AttributeError: os.remove(filename) logging.warning( f"CacheHandler: could not cache object of class {type(caching_object).__name__} with key {cache_key}. " f"Object: {caching_object}\n" f"Next time this object is needed, it will be recomputed which will take more time but should not influence results." )
def get_file_path(cache_type=None): file_path = EnvironmentSettings.get_cache_path(cache_type) / "files" PathBuilder.build(file_path) return file_path