Carlos Aguni

Highly motivated self-taught IT analyst. Always learning and ready to explore new skills. An eternal apprentice.


Python Redis

20 Apr 2020 » programming
from redis import Redis

redis = Redis(
    host="redishost",
    port=6379,
    password=""
)

def list_index():
    return [_.decode() for _ in redis.keys()]

def in_index(index):
    return index in list_index

def set_cache(index, body):
    if type(body) is str:
        redis.set(index, body)
    else:
        redis.set(index, json.dumps(body))

def get_cache(index):
    v = redis.get(index)
    try:
        return json.loads(v)
    except:
        return v.decode()

def delete_cache(index):
    redis.delete(index)