Skip to content

Commit

Permalink
Implemented cache helper file
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertSuarez committed Jan 30, 2020
1 parent c8ce719 commit c3eccce
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/searchly/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,8 @@
CONTEXT_SIZE = 7
DOWN_SAMPLING = 1e-1
NEIGHBOURHOOD_AMOUNT = API_SONG_SIMILARITY_LIMIT * 10

# Cache settings
CACHE_MAX_LENGTH = 256
CACHE_MAX_AGE = 5 * 60 # 5 minutes
CACHE_DICT_FORMAT = '{}:{}'
27 changes: 27 additions & 0 deletions src/searchly/helper/cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from expiringdict import ExpiringDict

from src.searchly import CACHE_MAX_LENGTH, CACHE_MAX_AGE, CACHE_DICT_FORMAT


__cache = ExpiringDict(max_len=CACHE_MAX_LENGTH, max_age_seconds=CACHE_MAX_AGE) # 5 minutes expiration


def get(method, key):
"""
Get object from the cache given a method and key.
:param method: Method.
:param key: Key.
:return: Cached object if found, None otherwise.
"""
return __cache.get(CACHE_DICT_FORMAT.format(method, key))


def refresh(method, key, refreshed_element):
"""
Refresh object in cache.
:param method: Method.
:param key: Key.
:param refreshed_element: Object to refresh.
:return: Object refreshed.
"""
__cache[CACHE_DICT_FORMAT.format(method, key)] = refreshed_element

0 comments on commit c3eccce

Please sign in to comment.