diff --git a/files/__main__.py b/files/__main__.py index 743c15e67..d6d4e32c7 100644 --- a/files/__main__.py +++ b/files/__main__.py @@ -16,6 +16,7 @@ import time from sys import stdout import faulthandler from json import loads +import atexit app = Flask(__name__, template_folder='templates') app.url_map.strict_slashes = False @@ -50,8 +51,8 @@ app.config["COMMENT_SPAM_SIMILAR_THRESHOLD"] = float(environ.get("COMMENT_SPAM_S app.config["COMMENT_SPAM_COUNT_THRESHOLD"] = int(environ.get("COMMENT_SPAM_COUNT_THRESHOLD", 10)) app.config["READ_ONLY"]=bool(int(environ.get("READ_ONLY", "0"))) app.config["BOT_DISABLE"]=bool(int(environ.get("BOT_DISABLE", False))) -app.config["CACHE_TYPE"] = "filesystem" -app.config["CACHE_DIR"] = "cache" +app.config["CACHE_TYPE"] = "RedisCache" +app.config["CACHE_REDIS_URL"] = environ.get("REDIS_URL", "redis://localhost") app.config['MAIL_SERVER'] = 'smtp.gmail.com' app.config['MAIL_PORT'] = 587 app.config['MAIL_USE_TLS'] = True @@ -115,4 +116,12 @@ def after_request(response): response.headers.add("X-Frame-Options", "deny") return response -from files.routes import * \ No newline at end of file +if not cache.get("marseys"): + with open("marseys.json", 'r') as f: cache.set("marseys", loads(f.read())) + +from files.routes import * + +def close_running_threads(): + with open('marsey_count.json', 'w') as f: dump(cache.get("marseys"), f) + stdout.flush() +atexit.register(close_running_threads) \ No newline at end of file diff --git a/files/helpers/sanitize.py b/files/helpers/sanitize.py index fffaf4432..8d4fe8776 100644 --- a/files/helpers/sanitize.py +++ b/files/helpers/sanitize.py @@ -178,7 +178,7 @@ def sanitize(sanitized, noimages=False, alert=False, comment=False, edit=False): sanitized = re.sub('\|\|(.*?)\|\|', r'\1', sanitized) if comment: - with open("marseys.json", 'r') as f: marsey_count = loads(f.read().replace("'",'"')) + marsey_count = cache.get("marseys") marseys_used = set() emojis = list(re.finditer("[^a]>\s*(:[!#]{0,2}\w+:\s*)+<\/", sanitized)) @@ -263,7 +263,7 @@ def sanitize(sanitized, noimages=False, alert=False, comment=False, edit=False): if comment: for emoji in marseys_used: if emoji in marsey_count: marsey_count[emoji]["count"] += 1 - with open('marseys.json', 'w') as f: dump(marsey_count, f) + marsey_count = cache.set("marseys", marsey_count) return sanitized diff --git a/files/routes/admin.py b/files/routes/admin.py index 4cd7b0044..e60fc3156 100644 --- a/files/routes/admin.py +++ b/files/routes/admin.py @@ -1217,7 +1217,7 @@ def admin_distinguish_comment(c_id, v): @app.get("/admin/dump_cache") @admin_level_required(2) def admin_dump_cache(v): - cache.clear() + # cache.clear() return {"message": "Internal cache cleared."} diff --git a/files/routes/comments.py b/files/routes/comments.py index c1f3c813f..8ca7ba9e8 100644 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -172,7 +172,7 @@ def api_comment(v): marsey_body = marsey_dict[0][1] marsey_body["count"] = 0 except: return {"error": "You didn't follow the format retard"}, 400 - with open("marseys.json", 'r') as f: marseys = loads(f.read().replace("'",'"')) + marseys = cache.get("marseys") marseys[marsey_key] = marsey_body if v.marseyawarded: @@ -212,7 +212,7 @@ def api_comment(v): elif v.id in (CARP_ID,AEVANN_ID) and parent_post.id == 37838: filename = f'files/assets/images/emojis/{marsey_key}.webp' process_image(file, filename, 200) - with open('marseys.json', 'w') as f: dump(marseys, f) + cache.set("marseys", marseys) elif file.content_type.startswith('video/'): file.save("video.mp4") with open("video.mp4", 'rb') as f: @@ -860,4 +860,4 @@ def unsave_comment(cid, v): g.db.delete(save) g.db.commit() - return {"message": "Comment unsaved!"} + return {"message": "Comment unsaved!"} \ No newline at end of file diff --git a/files/routes/posts.py b/files/routes/posts.py index 370528992..316644358 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -1008,7 +1008,7 @@ def submit_post(v): with open(f'snappy_{SITE_NAME}.txt', "r") as f: snappyquotes = f.read().split("{[para]}") if request.host != 'pcmemes.net': - with open("marseys.json", 'r') as f: marseys = loads(f.read().replace("'",'"')).keys() + marseys = cache.get("marseys").keys() snappyquotes += [f':#{x}:' for x in marseys] body = random.choice(snappyquotes) body += "\n\n" diff --git a/files/routes/static.py b/files/routes/static.py index 36d0a6e13..7daa118b9 100644 --- a/files/routes/static.py +++ b/files/routes/static.py @@ -17,7 +17,7 @@ def privacy(v): @app.get("/marseys") @auth_required def marseys(v): - with open("marseys.json", 'r') as f: marsey_count = list(loads(f.read().replace("'",'"')).items()) + marsey_count = cache.get("marseys").items() marsey_count = sorted(marsey_count, key=lambda x: list(x[1].values())[2], reverse=True) return render_template("marseys.html", v=v, marseys=marsey_count) @@ -48,7 +48,7 @@ def participation_stats(v): day = now - 86400 - with open("marseys.json", 'r') as f: marseys = loads(f.read().replace("'",'"')) + marseys = cache.get("marseys") data = {"marseys": len(marseys), "users": g.db.query(User.id).count(), @@ -373,7 +373,7 @@ def badges(v): @app.get("/marsey_list") @auth_required def marsey_list(v): - with open("marseys.json", 'r') as f: return loads(f.read().replace("'",'"')) + return cache.get("marseys") @app.get("/blocks") @auth_required diff --git a/files/routes/users.py b/files/routes/users.py index 037afdf78..a92ed32b9 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -31,7 +31,7 @@ def leaderboard_thread(): if SITE_NAME == 'Drama': users13 = {} - with open("marseys.json", 'r') as f: authors = (x for x in loads(f.read().replace("'",'"')).values()) + authors = (x for x in cache.get("marseys").values()) for x in authors: if x["author"] in users13: users13[x["author"]] += 1 else: users13[x["author"]] = 1