You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I'm trying to use beaker as a base for internal caching, but getting extremely slow performance on file caching.
Here is my basic implementation:
classCache:
""" General cache super().__init__() must be called after all attributes are declared to work properly """def__init__(self, disable=False):
self._disable=disableself._cache_path=CACHE_DIR/f"{self.__class__.__name__}:{hash(self)}.pkl"self._cache=self._load_cache()
def__hash__(self):
""" Must return unique identifier of instance to be cached Identifier must be the same across runs """self_vars=dict(vars(self))
self_vars.pop("_cache", None)
returnconsistent_hash(self_vars)
def_get_data(self, key: Any) ->Any:
""" Return data for key """raiseNotImplementedErrordef__getitem__(self, key: Any) ->Any:
ifself._disable:
returnself._get_data(key)
try:
value=self._cache[key]
exceptKeyError:
value=self._get_data(key)
self._cache[key] =keyreturnvaluedefclear(self):
self._cache= {}
self._save_cache()
def_save_cache(self):
withself._cache_path.open("wb") asf:
pickle.dump(self._cache, f)
def_load_cache(self):
try:
withself._cache_path.open("rb") asf:
returnpickle.load(f)
except (FileNotFoundError, EOFError):
return {}
def__del__(self):
self._save_cache()
and my implementation using beaker:
classCache:
""" General cache super().__init__() must be called after all attributes are declared to work properly """def__init__(self, expire_time=60*60*24*7, disable=False, cache_type="file"):
self._disable=disableself._cache=cache.get_cache(f"{self.__class__.__name__}:{hash(self)}", expire=expire_time, type=cache_type)
def__hash__(self):
""" Must return unique identifier of instance to be cached Identifier must be the same across runs """self_vars=dict(vars(self))
self_vars.pop("_cache", None)
returnconsistent_hash(self_vars)
def_get_data(self, key: Any) ->Any:
""" Return data for key """raiseNotImplementedErrordef__getitem__(self, key: Any) ->Any:
ifself._disable:
returnself._get_data(key)
try:
value=self._cache[key]
exceptKeyError:
value=self._get_data(key)
self._cache[key] =keyreturnvaluedefclear(self):
self._cache.clear()
Beaker caching appears to work, but I'm getting very bad performance:
My own: 50k+/s vs beakers 100/s, nearly 3 orders of magnitude.
I understand that there is some overhead on checking time and some splitting between different files, but this seems too much.
What am I doing wrong?
The text was updated successfully, but these errors were encountered:
Rizhiy
changed the title
File cache is very slow
File cache is extremely slow
Sep 2, 2020
Hi, I'm trying to use
beaker
as a base for internal caching, but getting extremely slow performance on file caching.Here is my basic implementation:
and my implementation using
beaker
:Beaker caching appears to work, but I'm getting very bad performance:
My own: 50k+/s vs beakers 100/s, nearly 3 orders of magnitude.
I understand that there is some overhead on checking time and some splitting between different files, but this seems too much.
What am I doing wrong?
The text was updated successfully, but these errors were encountered: