Skip to content

Instantly share code, notes, and snippets.

@Sanix-Darker
Last active January 15, 2020 12:05
Show Gist options
  • Save Sanix-Darker/8f86bf0e30fb38e380764dfdd6aa9c45 to your computer and use it in GitHub Desktop.
Save Sanix-Darker/8f86bf0e30fb38e380764dfdd6aa9c45 to your computer and use it in GitHub Desktop.
redisIT
{
"host": "127.0.0.1",
"port": 6379,
"db": 0,
"password": "",
"decode_responses": true
}
#
# redisIT.py
# By Sanix darker
# This file will assume the role of save data directly in redis database
#
import redis
import json
import configparser as ConfigParser
# Configs parameters
configParser = ConfigParser.RawConfigParser()
configFilePath = r'config.txt'
configParser.read(configFilePath)
REDIS_CONF = configParser.get('fax-config', 'REDIS_CONF')
class RedisIt:
def __init__(self):
self.r = None
self.init_redis()
def init_redis(self):
with open(REDIS_CONF) as raw:
config = json.load(raw)
self.r = redis.Redis(**config)
# Key - Values ------------------------
# Get a value from key
def _get(self, key):
return self.r.get(str(key))
# Set a value from a key
def _set(self, key, value):
self.r.set(str(key), str(value))
# Delete an element
def _delete_key(self, key):
self.r.delete(key)
# Key - Values ------------------------
# Sets - Values ------------------------
# Adding a value contain in a SET
def add_to_set(self, key, value):
self.r.sadd(str(key), str(value))
# Remove a value contain in a SET
def remove_to_set(self, key, value):
self.r.srem(str(key), str(value))
# Return all the elements in the SET
def _get_set(self, key):
return self.r.smembers(key)
# Sets - Values ------------------------
# Lists - Values ------------------------
# Adding a value contain in a LIST
def add_to_list(self, key, value):
self.r.rpush(str(key), str(value))
# Remove a value contain in a LIST
def remove_to_list(self, key, value):
self.r.rpop(str(key))
# Lists - Values ------------------------
def erase_all(self):
# flushdb
try:
self.r.flushdb()
except Exception as es:
print(es)
# flushall
try:
self.r.flushall()
except Exception as es:
print(es)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment