use SITE as a prefix for flask_cache

pull/142/head
Aevann 2023-03-25 22:57:27 +02:00
parent fff0b9c0c5
commit 96330210e8
9 changed files with 15 additions and 17 deletions

View File

@ -49,6 +49,7 @@ app.config['SESSION_REFRESH_EACH_REQUEST'] = False
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
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

View File

@ -77,9 +77,9 @@ else:
if SITE == 'staging.rdrama.net':
SITE_FULL_IMAGES = 'https://i.rdrama.net'
LOGGED_IN_CACHE_KEY = f"{SITE}_loggedin"
LOGGED_OUT_CACHE_KEY = f"{SITE}_loggedout"
CHAT_ONLINE_CACHE_KEY = f"{SITE}_online"
LOGGED_IN_CACHE_KEY = "loggedin"
LOGGED_OUT_CACHE_KEY = "loggedout"
CHAT_ONLINE_CACHE_KEY = "online"
REDDIT_NOTIFS_CACHE_KEY = "reddit_notifications"
SESSION_LIFETIME = 60 * 60 * 24 * 365

View File

@ -44,8 +44,7 @@ def cron(every_5m, every_1h, every_1d, every_1mo):
if every_1d:
stats.generate_charts_task(SITE)
_sub_inactive_purge_task()
site_stats = stats.stats(SITE_NAME)
cache.set(f'{SITE}_stats', site_stats)
cache.set('stats', stats.stats())
g.db.commit()
g.db.close()

View File

@ -93,7 +93,7 @@ def chart(kind, site):
def chart_path(kind, site):
return f'/{site}_{kind}.png'
def stats(site=None):
def stats():
now = time.time()
day = int(now) - 86400
week = int(now) - 604800

View File

@ -29,7 +29,7 @@ from .front import frontlist, comment_idlist
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
@admin_level_required(PERMS['VIEW_ACTIVE_USERS'])
def loggedin_list(v):
ids = [x for x,val in cache.get(f'{SITE}_loggedin').items() if time.time()-val < LOGGEDIN_ACTIVE_TIME]
ids = [x for x,val in cache.get('loggedin').items() if time.time()-val < LOGGEDIN_ACTIVE_TIME]
users = g.db.query(User).filter(User.id.in_(ids)).order_by(User.admin_level.desc(), User.truescore.desc()).all()
return render_template("admin/loggedin.html", v=v, users=users)
@ -38,7 +38,7 @@ def loggedin_list(v):
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
@admin_level_required(PERMS['VIEW_ACTIVE_USERS'])
def loggedout_list(v):
users = sorted([val[1] for x,val in cache.get(f'{SITE}_loggedout').items() if time.time()-val[0] < LOGGEDIN_ACTIVE_TIME])
users = sorted([val[1] for x,val in cache.get('loggedout').items() if time.time()-val[0] < LOGGEDIN_ACTIVE_TIME])
return render_template("admin/loggedout.html", v=v, users=users)
@app.get('/admin/dm_images')

View File

@ -58,7 +58,6 @@ def front_all(v, sub=None, subdomain=None):
gt=gt,
lt=lt,
sub=sub,
site=SITE,
pins=pins,
holes=holes
)
@ -76,7 +75,7 @@ def front_all(v, sub=None, subdomain=None):
LIMITED_WPD_HOLES = ('gore', 'aftermath', 'selfharm', 'meta', 'discussion', 'social', 'music', 'request')
@cache.memoize()
def frontlist(v=None, sort="hot", page=1, t="all", ids_only=True, filter_words='', gt=0, lt=0, sub=None, site=None, pins=True, holes=True):
def frontlist(v=None, sort="hot", page=1, t="all", ids_only=True, filter_words='', gt=0, lt=0, sub=None, pins=True, holes=True):
posts = g.db.query(Submission)
if v and v.hidevotedon:
@ -185,7 +184,7 @@ def random_user(v:User):
return redirect(f"/@{u}")
@cache.memoize()
def comment_idlist(v=None, page=1, sort="new", t="day", gt=0, lt=0, site=None):
def comment_idlist(v=None, page=1, sort="new", t="day", gt=0, lt=0):
comments = g.db.query(Comment.id) \
.outerjoin(Comment.post) \
.filter(
@ -233,7 +232,6 @@ def all_comments(v:User):
t=t,
gt=gt,
lt=lt,
site=SITE
)
comments = get_comments(idlist, v=v)

View File

@ -144,9 +144,9 @@ def me(v:User):
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
@auth_required
def logout(v):
loggedin = cache.get(f'{SITE}_loggedin') or {}
loggedin = cache.get('loggedin') or {}
if session.get("lo_user") in loggedin: del loggedin[session["lo_user"]]
cache.set(f'{SITE}_loggedin', loggedin)
cache.set('loggedin', loggedin)
session.pop("lo_user", None)
return {"message": "Logout successful!"}

View File

@ -101,7 +101,7 @@ def sidebar(v:Optional[User]):
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
@auth_required
def participation_stats(v:User):
stats = cache.get(f'{SITE}_stats') or {}
stats = cache.get('stats') or {}
if v.client: return stats
return render_template("stats.html", v=v, title="Content Statistics", data=stats)

View File

@ -787,7 +787,7 @@ def visitors(v:User, username:str):
return render_template("userpage/views.html", v=v, u=u, views=views, next_exists=next_exists, page=page)
@cache.memoize()
def userpagelisting(user:User, site=None, v=None, page:int=1, sort="new", t="all"):
def userpagelisting(user:User, v=None, page:int=1, sort="new", t="all"):
posts = g.db.query(Submission.id).filter_by(author_id=user.id, is_pinned=False)
if not (v and (v.admin_level >= PERMS['POST_COMMENT_MODERATION'] or v.id == user.id)):
posts = posts.filter_by(is_banned=False, private=False, ghost=False, deleted_utc=0)
@ -928,7 +928,7 @@ def u_username(v:Optional[User], username:str):
try: page = max(int(request.values.get("page", 1)), 1)
except: page = 1
ids = userpagelisting(u, site=SITE, v=v, page=page, sort=sort, t=t)
ids = userpagelisting(u, v=v, page=page, sort=sort, t=t)
next_exists = (len(ids) > PAGE_SIZE)
ids = ids[:PAGE_SIZE]