Skip to content

Instantly share code, notes, and snippets.

@nedludd0
Last active June 1, 2022 20:04
Show Gist options
  • Select an option

  • Save nedludd0/c39cc039c2e3e2f815c0dff425962aa6 to your computer and use it in GitHub Desktop.

Select an option

Save nedludd0/c39cc039c2e3e2f815c0dff425962aa6 to your computer and use it in GitHub Desktop.
borg_redis.py
"""
SINGLETON CLASS ENCAPSULATING A REDIS CLIENT
Credit: https://gist.github.com/skeeved/79dfe5652b20182586bb55c3cbc94250
"""
""" Config """
from os import environ
REDIS_URL = environ.get('REDIS_URL')
""" Import """
import redis
"""Borg pattern singleton"""
class RedisClient:
__state = {}
def __init__(self):
self.__dict__ = self.__state
if not hasattr(self, 'client'):
self.client = redis.from_url(config.REDIS_URL)
if __name__ == '__main__':
redis_client_obj = RedisClient()
redis_key = 'REDIS_KEY1'
""" HSet 1"""
dict_key1 = 'REDIS_DICT_KEY1'
dict_value2 = 'REDIS_DICT_VALUE1'
response = redis_client_obj.hset(redis_key, dict_key1, dict_value2)
print(response)
""" HSet 2"""
dict_key2 = 'REDIS_DICT_KEY2'
dict_value2 = 'REDIS_DICT_VALUE2'
response = redis_client_obj.hset(redis_key, dict_key2, dict_value2)
print(response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment