Skip to content

Instantly share code, notes, and snippets.

@Iftimie
Created September 13, 2020 20:26
Show Gist options
  • Save Iftimie/763d61bb3c0b917707dfd09202d37904 to your computer and use it in GitHub Desktop.
Save Iftimie/763d61bb3c0b917707dfd09202d37904 to your computer and use it in GitHub Desktop.
import redis
DISTRIBUTED_CACHE_TOP_PHRASES_KEY = 'top-phrases:'
class Frontend:
def __init__(self):
self._distributed_cache = redis.Redis(host=os.getenv("DISTRIBUTED_CACHE_HOST"), port=6379, db=0)
# Using Cache Aside Pattern
def top_phrases_for_prefix(self, prefix):
top_phrases_from_distributed_cache = self._top_phrases_for_prefix_distributed_cache(prefix)
if (top_phrases_from_distributed_cache is not None):
return top_phrases_from_distributed_cache
backend_hostname = self.backend_for_prefix(prefix)
r = requests.get(f'http://{backend_hostname}:6000/top-phrases', params = {'prefix': prefix})
top_phrases = r.json()["data"]["top_phrases"]
self._insert_top_phrases_distributed_cache(prefix, top_phrases)
return top_phrases
def _top_phrases_for_prefix_distributed_cache(self, prefix):
key = DISTRIBUTED_CACHE_TOP_PHRASES_KEY + prefix
self._logger.debug(f'Attempting to get top phrases from distributed cache with key {key}')
if (self._distributed_cache.exists(key)):
pickled_list = self._distributed_cache.get(key)
return pickle.loads(pickled_list)
return None
def _insert_top_phrases_distributed_cache(self, prefix, top_phrases):
key = DISTRIBUTED_CACHE_TOP_PHRASES_KEY + prefix
time_to_expire_s = 30 * 60 # Expire entry after 30 minutes
self._logger.debug(f'Inserting top phrases tokey {key}, with top phrases {top_phrases}')
self._distributed_cache.set(key, pickle.dumps(top_phrases), ex=time_to_expire_s)
distributor.distributed-cache:
container_name: distributor.distributed-cache
image: redis:6.0.6
ports:
- "6379:6379"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment