Moving to cache system only using one dict

pull/8/head
sgoudham 4 years ago
parent 753e27977d
commit 5d86190927

@ -13,7 +13,7 @@ import settings
from cogs.help import HelpPaginator from cogs.help import HelpPaginator
from db import connection from db import connection
from settings import blank_space, enso_embedmod_colours, enso_guild_ID, enso_newpeople_ID, get_prefix_for_guild, \ from settings import blank_space, enso_embedmod_colours, enso_guild_ID, enso_newpeople_ID, get_prefix_for_guild, \
storage_prefix_for_guild, cache_prefix, del_cache_prefix, del_modlog_channel, cache_modlogs storage_prefix_for_guild, cache, del_cache
counter = 0 counter = 0
@ -205,10 +205,8 @@ async def on_guild_join(guild):
Store prefix/modlogs in the cache Store prefix/modlogs in the cache
""" """
# Store default prefix within cache # Store guildID, modlogs channel and prefix to cache
cache_prefix(str(guild.id), prefix="~") cache(str(guild.id), channel=None, prefix="~")
# Store default modlogs channel within cache
cache_modlogs(str(guild.id), channel=None)
# Setup pool # Setup pool
pool = await connection(db.loop) pool = await connection(db.loop)
@ -244,10 +242,8 @@ async def on_guild_remove(guild):
Remove users in the database for the guild Remove users in the database for the guild
Remove the modlogs/guild from the cache Remove the modlogs/guild from the cache
""" """
# Delete the key - value pairs for the guild # Delete the key - value pairs for the guild
del_cache_prefix(str(guild.id)) del_cache(str(guild.id))
del_modlog_channel(str(guild.id))
# Setup pool # Setup pool
pool = await connection(db.loop) pool = await connection(db.loop)

@ -56,6 +56,10 @@ def rndColour():
return Colour(random.randint(0, 0xFFFFFF)) return Colour(random.randint(0, 0xFFFFFF))
# Setup Dict To Store Values
enso_cache = {}
async def startup_cache_log(): async def startup_cache_log():
"""Store the modlogs/prefixes in cache from the database on startup""" """Store the modlogs/prefixes in cache from the database on startup"""
@ -72,21 +76,32 @@ async def startup_cache_log():
await cur.execute(select_query) await cur.execute(select_query)
results = await cur.fetchall() results = await cur.fetchall()
# Store the guildids and modlog channels # Store the guildID's, modlog channels and prefixes within cache
for row in results: for row in results:
cache_modlogs(row[0], row[2]) cache(guildid=row[0], channel=row[2], prefix=row[1])
cache_prefix(row[0], row[1])
# --------------------------------------------!ModLogs Section!--------------------------------------------------------- # Method to store the guildID, channel
def cache(guildid, channel, prefix):
"""Storing GuildID, Modlogs Channel and Prefix in Cache"""
enso_cache[guildid] = {"Modlogs": channel, "Prefix": prefix}
def get_cache(guildid):
"""Returning the cache"""
return enso_cache[guildid]
# Store guildID's and modlog channel within a cached dictionary
modlogs = {}
def del_cache(guildid):
"""Deleting the entry of the guild within the cache"""
del enso_cache[guildid]
# --------------------------------------------!ModLogs Section!---------------------------------------------------------
# Updating the modlog within the dict and database when the method is called # Updating the modlog within the dict and database when the method is called
async def storage_modlog_for_guild(ctx, channelID, setup): async def storage_modlog_for_guild(ctx, channelID, setup):
modlogs[str(ctx.guild.id)] = channelID enso_cache[str(ctx.guild.id)]["Modlogs"] = channelID
# Setup pool # Setup pool
pool = await db.connection(db.loop) pool = await db.connection(db.loop)
@ -120,25 +135,25 @@ async def storage_modlog_for_guild(ctx, channelID, setup):
# Method to store the cached modlog channels # Method to store the cached modlog channels
def cache_modlogs(guildid, channel): def cache_modlogs(guildid, channel):
modlogs[guildid] = channel enso_cache[guildid]["Modlogs"] = channel
# Deleting the key - value pair for guild/modlogs # Deleting the key - value pair for guild/modlogs
def del_modlog_channel(guildid): def del_modlog_channel(guildid):
if modlogs[guildid] is not None: if enso_cache[guildid]["Modlogs"] is not None:
del modlogs[guildid] del enso_cache[guildid]["Modlogs"]
else: else:
pass pass
# Remove the value of modlog for the guild specified # Remove the value of modlog for the guild specified
def remove_modlog_channel(guildid): def remove_modlog_channel(guildid):
modlogs[guildid] = None enso_cache[guildid]["Modlogs"] = None
# Get the modlog channel of the guild that the user is in # Get the modlog channel of the guild that the user is in
def get_modlog_for_guild(guildid): def get_modlog_for_guild(guildid):
channel = modlogs[guildid] channel = enso_cache[guildid]["Modlogs"]
return channel return channel
@ -146,13 +161,9 @@ def get_modlog_for_guild(guildid):
# --------------------------------------------!Prefixes Section!-------------------------------------------------------- # --------------------------------------------!Prefixes Section!--------------------------------------------------------
# Storing the prefixes and guildID's in the cache
cached_prefixes = {}
# Updating the prefix within the dict and database when the method is called # Updating the prefix within the dict and database when the method is called
async def storage_prefix_for_guild(ctx, prefix): async def storage_prefix_for_guild(ctx, prefix):
cached_prefixes[str(ctx.guild.id)] = prefix enso_cache[str(ctx.guild.id)]["Prefix"] = prefix
# Setup pool # Setup pool
pool = await db.connection(db.loop) pool = await db.connection(db.loop)
@ -175,17 +186,17 @@ async def storage_prefix_for_guild(ctx, prefix):
# Method to store the cached prefixes # Method to store the cached prefixes
def cache_prefix(guildid, prefix): def cache_prefix(guildid, prefix):
cached_prefixes[guildid] = prefix enso_cache[guildid]["Prefix"] = prefix
# Deleting the key - value pair for guild # Deleting the key - value pair for guild
def del_cache_prefix(guildid): def del_cache_prefix(guildid):
del cached_prefixes[guildid] del enso_cache[guildid]["Prefix"]
# Get the prefix of the guild that the user is in # Get the prefix of the guild that the user is in
def get_prefix_for_guild(guildid): def get_prefix_for_guild(guildid):
prefix = cached_prefixes[guildid] prefix = enso_cache[guildid]["Prefix"]
if prefix is not None: if prefix is not None:
return prefix return prefix
return "defaultPrefix" return "defaultPrefix"

Loading…
Cancel
Save