Skip to content

Commit

Permalink
object cache refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
wuan committed Sep 22, 2024
1 parent 2488608 commit 709efd4
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions blitzortung/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ def get(self, cached_object_creator, *args, **kwargs):

self.total_count += 1

cache_key = (cached_object_creator,) + args + (self.kwargs_separator,) \
+ tuple(sorted(kwargs.items()))
cache_key = self.generate_cache_key(cached_object_creator, args, kwargs)

current_time = int(time.time())

Expand Down Expand Up @@ -121,4 +120,16 @@ def get_ratio(self):
return self.total_hit_count / self.total_count

def get_size(self):
return len(self.cache)
return len(self.cache)


def generate_cache_key(self, cached_object_creator, args, kwargs):
"""
Generates a cache key based on the cached object creator function, args, and kwargs.
:param cached_object_creator: The function used to create the object to be cached.
:param args: Positional arguments to the object creator.
:param kwargs: Keyword arguments to the object creator.
:return: A tuple representing the cache key.
"""
return (cached_object_creator,) + args + (self.kwargs_separator,) + tuple(sorted(kwargs.items()))

0 comments on commit 709efd4

Please sign in to comment.