Implement cache TTL

This commit is contained in:
AB
2020-03-30 10:02:19 +00:00
parent a5ac105177
commit decd96fe5c

View File

@@ -14,6 +14,12 @@ from telegram.ext import Updater, CommandHandler, CallbackQueryHandler
from telegram.error import NetworkError, Unauthorized from telegram.error import NetworkError, Unauthorized
from time import sleep from time import sleep
from functools import wraps from functools import wraps
import datetime
import logging
logging.basicConfig(level=logging.INFO)
now = datetime.datetime.now()
from pprint import pprint from pprint import pprint
try: try:
@@ -116,13 +122,19 @@ def perdoliq(username, password, subj, test, acc, submit=True, is_delayed=False)
return "Exception: " + str(e) return "Exception: " + str(e)
def list_test(username, password): def list_test(username, password):
if username in cached_tests: now = datetime.datetime.now()
return cached_tests[username] cache_name = f"{now.month}_{now.day}_{now.hour}"
if cache_name in cached_tests:
cache_username = cached_tests[cache_name].get(username, None)
if cache_username:
return cache_username
try: try:
app = Perdoliq(username, password) app = Perdoliq(username, password)
app.auth() app.auth()
tests = app.get_tests() tests = app.get_tests()
cached_tests[username] = tests if not cached_tests.get(cache_name, None):
cached_tests[cache_name] = dict()
cached_tests[cache_name][username] = tests
return tests return tests
except Exception as e: except Exception as e:
return "Exception: " + str(e) return "Exception: " + str(e)
@@ -132,7 +144,6 @@ def list_(update, context):
query = update.callback_query query = update.callback_query
user_id = update.effective_user.id user_id = update.effective_user.id
tests = list_test(auth[user_id]["login"], auth[user_id]["passwd"]) tests = list_test(auth[user_id]["login"], auth[user_id]["passwd"])
print(tests)
keyboard = list() keyboard = list()
i = 1 i = 1
for subj in tests: for subj in tests:
@@ -140,7 +151,7 @@ def list_(update, context):
#msg = msg + (" [%s] %s (%s tests)\n" % (i, subj, tests_count)) #msg = msg + (" [%s] %s (%s tests)\n" % (i, subj, tests_count))
keyboard.append([InlineKeyboardButton(f"{i}. {subj}", callback_data=f"s_{i}")]) keyboard.append([InlineKeyboardButton(f"{i}. {subj}", callback_data=f"s_{i}")])
i += 1 i += 1
keyboard.append([InlineKeyboardButton("Close", callback_data="subj")]) keyboard.append([InlineKeyboardButton("Закрыть", callback_data=f"close")])
reply_markup = InlineKeyboardMarkup(keyboard) reply_markup = InlineKeyboardMarkup(keyboard)
update.message.reply_text('Чо будем хакать?', reply_markup=reply_markup) update.message.reply_text('Чо будем хакать?', reply_markup=reply_markup)