.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

@ -70,6 +70,7 @@ class handler(SentryMixin, requestHelper.asyncRequestHandler):
# Server's token string and request data # Server's token string and request data
responseTokenString = "ayy" responseTokenString = "ayy"
responseData = bytes()
if requestTokenString is None: if requestTokenString is None:
# No token, first request. Handle login. # No token, first request. Handle login.

View File

@ -8,21 +8,21 @@ from constants import privileges
def getID(username): 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 username -- user
return -- user id or False return -- user id or 0
""" """
# Get user ID from db # Add to cache if needed
userID = glob.db.fetch("SELECT id FROM users WHERE username = %s LIMIT 1", [username]) 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 # Get userID from cache
if userID is None: return glob.userIDCache[username]
return False
# Return user ID
return userID["id"]
def checkLogin(userID, password): def checkLogin(userID, password):
""" """

View File

@ -25,6 +25,7 @@ fLocks = fileLocks.fileLocks()
verifiedCache = {} verifiedCache = {}
cloudflare = False cloudflare = False
chatFilters = None chatFilters = None
userIDCache = {}
debug = False debug = False
outputRequestTime = False outputRequestTime = False
@ -33,5 +34,6 @@ discord = False
gzip = False gzip = False
localize = False localize = False
sentry = False sentry = False
irc = False
startTime = int(time.time()) startTime = int(time.time())

View File

@ -11,6 +11,7 @@ import threading
from helpers import chatHelper as chat from helpers import chatHelper as chat
class token: class token:
def __init__(self, userID, token_ = None, ip ="", irc = False, timeOffset = 0): def __init__(self, userID, token_ = None, ip ="", irc = False, timeOffset = 0):
""" """
Create a token object and set userID and token Create a token object and set userID and token

15
pep.py
View File

@ -1,14 +1,14 @@
"""Hello, pep.py here, ex-owner of ripple and prime minister of Ripwot.""" """Hello, pep.py here, ex-owner of ripple and prime minister of Ripwot."""
import sys
import os import os
import sys
import threading import threading
# Tornado import tornado.gen
import tornado.httpserver
import tornado.ioloop import tornado.ioloop
import tornado.web import tornado.web
import tornado.httpserver
import tornado.gen
from gevent import monkey as brit_monkey from gevent import monkey as brit_monkey
brit_monkey.patch_all() brit_monkey.patch_all()
# Raven # Raven
@ -25,6 +25,7 @@ from helpers import consoleHelper
from helpers import databaseHelperNew from helpers import databaseHelperNew
from helpers import generalFunctions from helpers import generalFunctions
from helpers import logHelper as log from helpers import logHelper as log
from helpers import userHelper
from handlers import mainHandler from handlers import mainHandler
from handlers import apiIsOnlineHandler from handlers import apiIsOnlineHandler
@ -136,9 +137,9 @@ if __name__ == "__main__":
consoleHelper.printDone() consoleHelper.printDone()
# Cache user ids # Cache user ids
#consoleHelper.printNoNl("> Caching user IDs... ") consoleHelper.printNoNl("> Caching user IDs... ")
#userHelper.cacheUserIDs() userHelper.cacheUserIDs()
#consoleHelper.printDone() consoleHelper.printDone()
# Localize warning # Localize warning
glob.localize = generalFunctions.stringToBool(glob.conf.config["localize"]["enable"]) glob.localize = generalFunctions.stringToBool(glob.conf.config["localize"]["enable"])