mirror of
https://github.com/idanoo/discord-keyword-bot.git
synced 2024-11-22 08:25:11 +00:00
Fix duplicate cache
This commit is contained in:
parent
d5a03d0787
commit
e3a04222e5
26
bot.py
26
bot.py
@ -9,7 +9,7 @@ from dotenv import load_dotenv
|
|||||||
class DiscordKeywordBot(discord.Client):
|
class DiscordKeywordBot(discord.Client):
|
||||||
CACHE_TTL = 900
|
CACHE_TTL = 900
|
||||||
CACHE_DICT = {}
|
CACHE_DICT = {}
|
||||||
CACHE_LAST_PURGE = 0
|
CACHE_NEXT_PURGE = int(time.time()) + (DiscordKeywordBot.CACHE_TTL * 2)
|
||||||
|
|
||||||
async def on_ready(self):
|
async def on_ready(self):
|
||||||
print('Logged on as', self.user)
|
print('Logged on as', self.user)
|
||||||
@ -22,31 +22,35 @@ class DiscordKeywordBot(discord.Client):
|
|||||||
chan_list = [x.strip() for x in os.getenv("DISCORD_CHANNELS").split(',')]
|
chan_list = [x.strip() for x in os.getenv("DISCORD_CHANNELS").split(',')]
|
||||||
for channel_id in chan_list:
|
for channel_id in chan_list:
|
||||||
channel = client.get_channel(int(channel_id))
|
channel = client.get_channel(int(channel_id))
|
||||||
await channel.send(message.content)
|
await channel.send('test: ' + message.content)
|
||||||
|
|
||||||
def cache_check(msg):
|
def cache_check(msg):
|
||||||
|
print('Checking cache for: ' + msg)
|
||||||
if msg in DiscordKeywordBot.CACHE_DICT:
|
if msg in DiscordKeywordBot.CACHE_DICT:
|
||||||
""" Has been sent before.. Check TTL """
|
""" Has been sent before.. Check TTL """
|
||||||
timestamp = DiscordKeywordBot.CACHE_DICT.get(msg)
|
print('Exists in cache: ' + msg)
|
||||||
if timestamp <=(int(time.time()) + DiscordKeywordBot.CACHE_TTL):
|
if DiscordKeywordBot.CACHE_DICT.get(msg) <= int(time.time()):
|
||||||
""" TTL Expired. """
|
""" TTL Expired. """
|
||||||
DiscordKeywordBot.CACHE_DICT[msg] = int(time.time())
|
print('TTL Expired for:' + msg)
|
||||||
|
DiscordKeywordBot.CACHE_DICT[msg] = (int(time.time()) + DiscordKeywordBot.CACHE_TTL)
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
""" TTL Not Expired """
|
""" TTL Not Expired """
|
||||||
|
print('TTL Not expired: ' + msg)
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
DiscordKeywordBot.CACHE_DICT[msg] = int(time.time())
|
print('Not in cache: ' + msg)
|
||||||
|
DiscordKeywordBot.CACHE_DICT[msg] = (int(time.time()) + DiscordKeywordBot.CACHE_TTL)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def cache_purge():
|
def cache_purge():
|
||||||
purge_timer = int(time.time()) - (DiscordKeywordBot.CACHE_TTL * 2)
|
if DiscordKeywordBot.CACHE_NEXT_PURGE <= int(time.time()) :
|
||||||
if DiscordKeywordBot.CACHE_LAST_PURGE <= purge_timer:
|
|
||||||
print("Purging cache!!!")
|
|
||||||
DiscordKeywordBot.CACHE_LAST_PURGE = int(time.time())
|
|
||||||
""" Lets Purge!!! """
|
""" Lets Purge!!! """
|
||||||
|
print("Total cache older than TTL*2. Purging old records.")
|
||||||
|
DiscordKeywordBot.CACHE_NEXT_PURGE = (int(time.time()) + (DiscordKeywordBot.CACHE_TTL * 2))
|
||||||
for uniq_msg in DiscordKeywordBot.CACHE_DICT:
|
for uniq_msg in DiscordKeywordBot.CACHE_DICT:
|
||||||
if DiscordKeywordBot.CACHE_DICT.get(uniq_msg) <= purge_timer:
|
if DiscordKeywordBot.CACHE_DICT.get(uniq_msg) <= int(time.time()):
|
||||||
|
""" If TTL <= now, remove from cache """
|
||||||
DiscordKeywordBot.CACHE_DICT.pop(msg, None)
|
DiscordKeywordBot.CACHE_DICT.pop(msg, None)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user