Last active
June 1, 2022 20:04
-
-
Save nedludd0/c39cc039c2e3e2f815c0dff425962aa6 to your computer and use it in GitHub Desktop.
borg_redis.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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