From 3df40af71a327c43c56e4ca2b2a80f739d5fe2f8 Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Sat, 22 Apr 2017 01:28:31 -0700 Subject: [PATCH] Attempt to fix limiting --- titanembeds/blueprints/api/api.py | 2 +- titanembeds/database/custom_redislite.py | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/titanembeds/blueprints/api/api.py b/titanembeds/blueprints/api/api.py index 698a401..db5091b 100644 --- a/titanembeds/blueprints/api/api.py +++ b/titanembeds/blueprints/api/api.py @@ -114,7 +114,7 @@ def format_post_content(message): message = "**<{}#{}>** {}".format(session['username'], session['discriminator'], message) # I would like to do a @ mention, but i am worried about notif spam return message -@cache.cache(make_guildchannels_cache_key, expires=60) +@cache.cache(make_guildchannels_cache_key, expire=60) def get_guild_channels(guild_id): if user_unauthenticated(): member_roles = [guild_id] #equivilant to @everyone role diff --git a/titanembeds/database/custom_redislite.py b/titanembeds/database/custom_redislite.py index e79849c..dbd5aa3 100644 --- a/titanembeds/database/custom_redislite.py +++ b/titanembeds/database/custom_redislite.py @@ -1,6 +1,7 @@ import urlparse from limits.storage import Storage from redislite import Redis +import time class LimitsRedisLite(Storage): # For Python Limits STORAGE_SCHEME = "redislite" @@ -14,12 +15,18 @@ class LimitsRedisLite(Storage): # For Python Limits return self.redis_instance.ttl(key) def incr(self, key, expiry, elastic_expiry=False): - if self.redis_instance.exists(key): - self.redis_instance.set(key, int(self.redis_instance.get(key))+1) - else: + if not self.redis_instance.exists(key): self.redis_instance.set(key, 1) - self.redis_instance.expire(key, expiry) + 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): + print key + " " + str(int(self.redis_instance.get(key))) + " " + str(self.get_expiry(key)) return int(self.redis_instance.get(key)) + + def reset(self): + return self.redis_instance.flushdb()