2017-04-24 05:02:04 +02:00
|
|
|
from titanembeds.database import db
|
2017-04-24 08:02:03 +02:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from limits.storage import Storage
|
|
|
|
import time
|
2017-04-24 05:02:04 +02:00
|
|
|
|
|
|
|
def set_keyvalproperty(key, value, expiration=None):
|
|
|
|
q = db.session.query(KeyValueProperties).filter(KeyValueProperties.key == key)
|
|
|
|
if q.count() == 0:
|
|
|
|
db.session.add(KeyValueProperties(key=key, value=value, expiration=expiration))
|
|
|
|
else:
|
|
|
|
firstobj = q.first()
|
|
|
|
firstobj.value = value
|
|
|
|
firstobj.expiration = expiration
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
def get_keyvalproperty(key):
|
|
|
|
q = db.session.query(KeyValueProperties).filter(KeyValueProperties.key == key)
|
|
|
|
now = datetime.now()
|
|
|
|
if q.count() > 0 and (q.first().expiration is None or q.first().expiration > now):
|
|
|
|
return q.first().value
|
|
|
|
return None
|
|
|
|
|
|
|
|
def getexpir_keyvalproperty(key):
|
|
|
|
q = db.session.query(KeyValueProperties).filter(KeyValueProperties.key == key)
|
|
|
|
now = datetime.now()
|
|
|
|
if q.count() > 0 and (q.first().expiration is not None and q.first().expiration > now):
|
2017-04-24 08:02:03 +02:00
|
|
|
print q.first().expiration
|
|
|
|
print datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
return int(q.first().expiration.strftime('%s'))
|
|
|
|
return 0
|
2017-04-24 05:02:04 +02:00
|
|
|
|
|
|
|
def setexpir_keyvalproperty(key, expiration=None):
|
|
|
|
q = db.session.query(KeyValueProperties).filter(KeyValueProperties.key == key)
|
|
|
|
if q.count() > 0:
|
|
|
|
if expiration:
|
|
|
|
q.first().expiration = datetime.now()
|
|
|
|
else:
|
|
|
|
q.first().expiration = None
|
|
|
|
db.session.commit()
|
|
|
|
|
2017-04-24 08:02:03 +02:00
|
|
|
def ifexists_keyvalproperty(key):
|
|
|
|
q = db.session.query(KeyValueProperties).filter(KeyValueProperties.key == key)
|
|
|
|
return q.count() > 0
|
|
|
|
|
|
|
|
def delete_keyvalproperty(key):
|
|
|
|
q = db.session.query(KeyValueProperties).filter(KeyValueProperties.key == key).first()
|
|
|
|
if q:
|
|
|
|
db.session.delete(q)
|
|
|
|
db.session.commit()
|
|
|
|
|
2017-04-24 05:02:04 +02:00
|
|
|
class KeyValueProperties(db.Model):
|
|
|
|
__tablename__ = "keyvalue_properties"
|
|
|
|
id = db.Column(db.Integer, primary_key=True) # Auto incremented id
|
2017-04-24 08:02:03 +02:00
|
|
|
key = db.Column(db.String(255)) # Property Key
|
2017-04-24 05:02:04 +02:00
|
|
|
value = db.Column(db.Text()) # Property value
|
|
|
|
expiration = db.Column(db.TIMESTAMP) # Suggested Expiration for value (None = no expire) in secs
|
|
|
|
|
|
|
|
def __init__(self, key, value, expiration=None):
|
|
|
|
self.key = key
|
|
|
|
self.value = value
|
|
|
|
if expiration:
|
2017-04-24 08:02:03 +02:00
|
|
|
self.expiration = datetime.now() + timedelta(seconds = expiration)
|
2017-04-24 05:02:04 +02:00
|
|
|
else:
|
|
|
|
self.expiration = None
|
2017-04-24 08:02:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
class LimitsKeyValueProperties(Storage): # For Python Limits
|
|
|
|
STORAGE_SCHEME = "keyvalprops"
|
|
|
|
def __init__(self, uri, **options):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def check(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def get_expiry(self, key):
|
|
|
|
return getexpir_keyvalproperty(key) + time.time()
|
|
|
|
|
|
|
|
def incr(self, key, expiry, elastic_expiry=False):
|
|
|
|
if not ifexists_keyvalproperty(key):
|
|
|
|
set_keyvalproperty(key, 1, expiration=expiry)
|
|
|
|
else:
|
|
|
|
oldexp = getexpir_keyvalproperty(key) - time.time()
|
|
|
|
if oldexp <= 0:
|
|
|
|
delete_keyvalproperty(key)
|
|
|
|
return self.incr(key, expiry, elastic_expiry)
|
|
|
|
set_keyvalproperty(key, int(get_keyvalproperty(key))+1, expiration=int(round(oldexp)))
|
|
|
|
return int(self.get(key))
|
|
|
|
|
|
|
|
def get(self, key):
|
|
|
|
value = get_keyvalproperty(key)
|
|
|
|
if value:
|
|
|
|
return int(value)
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
return False
|