.BANCHO. Add userID cache

This commit is contained in:
Nyo
2016-09-04 10:56:10 +02:00
parent cf60c167b6
commit 9ee2e5d7ff
5 changed files with 23 additions and 18 deletions

View File

@@ -8,21 +8,21 @@ from constants import privileges
def getID(username):
"""
Get username's user ID
Get username's user ID from userID cache (if cache hit)
or from db (and cache it for other requests) if cache miss
db -- database connection
username -- user
return -- user id or False
return -- user id or 0
"""
# Get user ID from db
userID = glob.db.fetch("SELECT id FROM users WHERE username = %s LIMIT 1", [username])
# Add to cache if needed
if username not in glob.userIDCache:
userID = glob.db.fetch("SELECT id FROM users WHERE username = %s LIMIT 1", [username])
if userID == None:
return 0
glob.userIDCache[username] = userID["id"]
# Make sure the query returned something
if userID is None:
return False
# Return user ID
return userID["id"]
# Get userID from cache
return glob.userIDCache[username]
def checkLogin(userID, password):
"""