From 6dacba53b685dd94f533ed4f8dd71eef6b4e5386 Mon Sep 17 00:00:00 2001 From: Aevann Date: Fri, 7 Jul 2023 22:08:23 +0300 Subject: [PATCH] tinker with cache to stop insane cpu usage by redis-server --- files/__main__.py | 2 +- files/helpers/config/const.py | 2 +- files/helpers/cron.py | 22 ++++++++++++---------- files/routes/front.py | 2 +- files/routes/posts.py | 2 +- files/routes/static.py | 2 +- 6 files changed, 17 insertions(+), 15 deletions(-) diff --git a/files/__main__.py b/files/__main__.py index ef6d806a9..41f8c8ed2 100644 --- a/files/__main__.py +++ b/files/__main__.py @@ -53,7 +53,7 @@ app.config['SQLALCHEMY_DATABASE_URL'] = environ.get("DATABASE_URL").strip() app.config["CACHE_KEY_PREFIX"] = f"{SITE}_flask_cache_" app.config["CACHE_TYPE"] = "RedisCache" app.config["CACHE_REDIS_URL"] = environ.get("REDIS_URL").strip() -app.config["CACHE_DEFAULT_TIMEOUT"] = 604800 +app.config["CACHE_DEFAULT_TIMEOUT"] = 86400 app.config['SERVICE'] = Service.RDRAMA if "load_chat" in argv: diff --git a/files/helpers/config/const.py b/files/helpers/config/const.py index 08a62185c..757639055 100644 --- a/files/helpers/config/const.py +++ b/files/helpers/config/const.py @@ -672,7 +672,7 @@ TRUESCORE_GHOST_MINIMUM = 0 TRUESCORE_DONATE_MINIMUM = 1 TRUESCORE_RESTRICTED_HOLES_MINIMUM = 100 -LOGGEDIN_ACTIVE_TIME = 15 * 60 +LOGGEDIN_ACTIVE_TIME = 60 PFP_DEFAULT_MARSEY = True NEW_USER_AGE = 7 * 86400 NOTIFICATION_SPAM_AGE_THRESHOLD = 0 diff --git a/files/helpers/cron.py b/files/helpers/cron.py index ed14ca3bc..acb7e0175 100644 --- a/files/helpers/cron.py +++ b/files/helpers/cron.py @@ -24,6 +24,8 @@ from files.helpers.roulette import spin_roulette_wheel from files.helpers.useractions import * from files.cli import app, db_session, g +CRON_CACHE_TIMEOUT = 172800 + @app.cli.command('cron', help='Run scheduled tasks.') @click.option('--every-5m', is_flag=True, help='Call every 5 minutes.') @click.option('--every-1h', is_flag=True, help='Call every 1 hour.') @@ -48,7 +50,7 @@ def cron(every_5m, every_1h, every_1d, every_1mo): _sub_inactive_purge_task() - cache.set('stats', stats.stats()) + cache.set('stats', stats.stats(), timeout=CRON_CACHE_TIMEOUT) _generate_emojis_zip() @@ -130,13 +132,13 @@ def _generate_emojis_zip(): data = f.read() m.update(data) - cache.set('emojis_hash', m.hexdigest()) + cache.set('emojis_hash', m.hexdigest(), timeout=CRON_CACHE_TIMEOUT) count = str(len(os.listdir('files/assets/images/emojis'))) - cache.set('emojis_count', count) + cache.set('emojis_count', count, timeout=CRON_CACHE_TIMEOUT) size = str(int(os.stat('files/assets/emojis.zip').st_size/1024/1024)) + ' MB' - cache.set('emojis_size', size) + cache.set('emojis_size', size, timeout=CRON_CACHE_TIMEOUT) def _leaderboard_task(): @@ -151,9 +153,9 @@ def _leaderboard_task(): users13 = sorted(users13, key=lambda x: x[1], reverse=True) users13_1, users13_2 = zip(*users13[:25]) - cache.set("users13", list(users13)) - cache.set("users13_1", list(users13_1)) - cache.set("users13_2", list(users13_2)) + cache.set("users13", list(users13), timeout=CRON_CACHE_TIMEOUT) + cache.set("users13_1", list(users13_1), timeout=CRON_CACHE_TIMEOUT) + cache.set("users13_2", list(users13_2), timeout=CRON_CACHE_TIMEOUT) votes1 = g.db.query(Post.author_id, func.count(Post.author_id)).join(Vote).filter(Vote.vote_type==-1).group_by(Post.author_id).order_by(func.count(Post.author_id).desc()).all() votes2 = g.db.query(Comment.author_id, func.count(Comment.author_id)).join(CommentVote).filter(CommentVote.vote_type==-1).group_by(Comment.author_id).order_by(func.count(Comment.author_id).desc()).all() @@ -166,9 +168,9 @@ def _leaderboard_task(): users9 = sorted(users9, key=lambda x: x[1], reverse=True) users9_1, users9_2 = zip(*users9[:25]) - cache.set("users9", list(users9)) - cache.set("users9_1", list(users9_1)) - cache.set("users9_2", list(users9_2)) + cache.set("users9", list(users9), timeout=CRON_CACHE_TIMEOUT) + cache.set("users9_1", list(users9_1), timeout=CRON_CACHE_TIMEOUT) + cache.set("users9_2", list(users9_2), timeout=CRON_CACHE_TIMEOUT) def _process_timer(attr, badge_ids, text, extra_attrs={}): diff --git a/files/routes/front.py b/files/routes/front.py index 8dc9718db..0c59152b1 100644 --- a/files/routes/front.py +++ b/files/routes/front.py @@ -73,7 +73,7 @@ def front_all(v, sub=None): result = render_template("home.html", v=v, listing=posts, total=total, sort=sort, t=t, page=page, sub=sub, home=True, pins=pins, size=size) if not v: - cache.set(f'frontpage_{sort}_{t}_{page}_{sub}_{pins}', result) + cache.set(f'frontpage_{sort}_{t}_{page}_{sub}_{pins}', result, timeout=3600) return result diff --git a/files/routes/posts.py b/files/routes/posts.py index 89c8acc62..9c6aed3c7 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -185,7 +185,7 @@ def post_id(pid, anything=None, v=None, sub=None): fart=get_setting('fart_mode')) if not v: - cache.set(f'post_{p.id}_{sort}', result) + cache.set(f'post_{p.id}_{sort}', result, timeout=3600) return result diff --git a/files/routes/static.py b/files/routes/static.py index cf22fba5e..f217b0c27 100644 --- a/files/routes/static.py +++ b/files/routes/static.py @@ -77,7 +77,7 @@ def emoji_list(v:User): -@cache.cached(key_prefix="emojis", timeout=86400) +@cache.cached(key_prefix="emojis") def get_emojis(): emojis = g.db.query(Emoji, User).join(User, Emoji.author_id == User.id).filter(Emoji.submitter_id == None) emojis1 = emojis.filter(Emoji.kind != 'Marsey Alphabet').order_by(Emoji.count.desc()).all()