Titan/titanembeds/database/custom_redislite.py

33 lines
1.1 KiB
Python
Raw Normal View History

import urlparse
from limits.storage import Storage
from redislite import Redis
2017-04-22 10:28:31 +02:00
import time
class LimitsRedisLite(Storage): # For Python Limits
STORAGE_SCHEME = "redislite"
def __init__(self, uri, **options):
self.redis_instance = Redis(urlparse.urlparse(uri).netloc)
def check(self):
return True
def get_expiry(self, key):
return self.redis_instance.ttl(key)
def incr(self, key, expiry, elastic_expiry=False):
2017-04-22 10:28:31 +02:00
if not self.redis_instance.exists(key):
self.redis_instance.set(key, 1)
2017-04-22 10:28:31 +02:00
self.redis_instance.expireat(key, int(time.time() + expiry))
else:
oldexp = self.get_expiry(key)
self.redis_instance.set(key, int(self.redis_instance.get(key))+1)
self.redis_instance.expireat(key, int(time.time() + oldexp))
return
def get(self, key):
2017-04-22 10:28:31 +02:00
print key + " " + str(int(self.redis_instance.get(key))) + " " + str(self.get_expiry(key))
return int(self.redis_instance.get(key))
2017-04-22 10:28:31 +02:00
def reset(self):
return self.redis_instance.flushdb()