# Collection of code snippets by Arne Vajhøj 
# (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import redis

FUNCTIONAL_CACHE = 0
cache = redis.Redis(host='localhost',port=6379,db=FUNCTIONAL_CACHE)

# basic put, get and remove
cache.set("v", 123)
print(int(cache.get("v")))
cache.delete("v")
print(cache.get("v"))
cache.set("v", 123)
print(int(cache.get("v")))

# persistence
counter = cache.get("counter")
if(counter == None):
    counter = 0;
else:
    counter = int(counter)
counter = counter + 1
print(counter)
cache.set("counter", counter)

# list all (simple)
for key in cache.keys("*"):
    print("%s = %d" % (key.decode('utf-8'), int(cache.get(key))))

#list all (batched)
for key in cache.scan_iter("*"):
    print("%s = %d" % (key.decode('utf-8'), int(cache.get(key))))

# listener not supported

cache.close()
