Skip to content

Commit

Permalink
redis 连接池
Browse files Browse the repository at this point in the history
  • Loading branch information
BennyThink committed Aug 26, 2023
1 parent 1fcfdfc commit 3353f36
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 42 deletions.
17 changes: 15 additions & 2 deletions yyetsweb/databases/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import pathlib
import sys

import fakeredis
import pymongo
import redis

DOUBAN_SEARCH = "https://www.douban.com/search?cat=1002&q={}"
DOUBAN_DETAIL = "https://movie.douban.com/subject/{}/"
Expand All @@ -24,7 +26,7 @@
(BD2020, XL720, NewzmzOnline, ZhuixinfanOnline, ZimuxiaOnline),
)

client = pymongo.MongoClient(
mongo_client = pymongo.MongoClient(
host=os.getenv("MONGO", "localhost"),
connect=True,
connectTimeoutMS=5000,
Expand All @@ -33,4 +35,15 @@
minPoolSize=50,
maxIdleTimeMS=600000,
)
db = client["zimuzu"]
db = mongo_client["zimuzu"]

try:
redis_client = redis.StrictRedis(
host=os.getenv("REDIS", "localhost"),
decode_responses=True,
max_connections=100,
)
redis_client.ping()
except redis.exceptions.ConnectionError:
logging.warning("%s Using fakeredis now... %s", "#" * 10, "#" * 10)
redis_client = fakeredis.FakeStrictRedis()
17 changes: 3 additions & 14 deletions yyetsweb/databases/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@
import os
import time

import fakeredis
import meilisearch
import redis

from databases import db

faker_redis = fakeredis.FakeStrictRedis()
from databases import db, redis_client


class Mongo:
def __init__(self):
self.db = db
super().__init__()

def is_admin(self, username: str) -> bool:
data = self.db["users"].find_one({"username": username, "group": {"$in": ["admin"]}})
Expand All @@ -34,17 +31,9 @@ def is_old_user(self, username: str) -> bool:

class Redis:
def __init__(self):
try:
self.r = redis.StrictRedis(host=os.getenv("REDIS", "localhost"), decode_responses=True)
self.r.ping()
except redis.exceptions.ConnectionError:
logging.warning("%s Using fakeredis now... %s", "#" * 10, "#" * 10)
self.r = faker_redis
self.r = redis_client
super().__init__()

def __del__(self):
self.r.close()

@classmethod
def cache(cls, timeout: int):
def func(fun):
Expand Down
8 changes: 3 additions & 5 deletions yyetsweb/databases/other.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def request_approval(document: "dict"):
logging.info("Telegram response: %s", resp)


class Other(Mongo):
class Other(Mongo, Redis):
def reset_top(self):
# before resetting, save top data to history
json_data = requests.get("http://127.0.0.1:8888/api/top").json()
Expand All @@ -159,12 +159,10 @@ def reset_top(self):

def import_ban_user(self):
usernames = self.db["users"].find({"status.disable": True}, projection={"username": True})
r = Redis().r
r.delete("user_blacklist")
self.r.delete("user_blacklist")
logging.info("Importing ban users to redis...%s", usernames)
for username in [u["username"] for u in usernames]:
r.hset("user_blacklist", username, 100)
r.close()
self.r.hset("user_blacklist", username, 100)

def fill_user_hash(self):
users = self.db["users"].find({"hash": {"$exists": False}}, projection={"username": True})
Expand Down
13 changes: 5 additions & 8 deletions yyetsweb/databases/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,20 +240,18 @@ def get_top_resource(self) -> dict:
return all_data


class ResourceLatest(Mongo):
@staticmethod
def get_latest_resource() -> dict:
redis = Redis().r
class ResourceLatest(Mongo, Redis):
def get_latest_resource(self) -> dict:
key = "latest-resource"
latest = redis.get(key)
latest = self.r.get(key)
if latest:
logging.info("Cache hit for latest resource")
latest = json.loads(latest)
latest["data"] = latest["data"][:100]
else:
logging.warning("Cache miss for latest resource")
latest = ResourceLatest().query_db()
redis.set(key, json.dumps(latest, ensure_ascii=False))
self.r.set(key, json.dumps(latest, ensure_ascii=False))
return latest

def query_db(self) -> dict:
Expand Down Expand Up @@ -286,10 +284,9 @@ def query_db(self) -> dict:
return dict(data=ok)

def refresh_latest_resource(self):
redis = Redis().r
logging.info("Getting new resources...")
latest = self.query_db()
redis.set("latest-resource", json.dumps(latest, ensure_ascii=False))
self.r.set("latest-resource", json.dumps(latest, ensure_ascii=False))
logging.info("latest-resource data refreshed.")


Expand Down
20 changes: 9 additions & 11 deletions yyetsweb/databases/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ def update_user_last(self, username: str, now_ip: str) -> None:
)

def update_user_info(self, username: str, data: dict) -> dict:
redis = Redis().r
valid_fields = ["email"]
valid_data = {}
for field in valid_fields:
Expand All @@ -147,7 +146,7 @@ def update_user_info(self, username: str, data: dict) -> dict:
# rate limit
user_email = valid_data.get("email")
timeout_key = f"timeout-{username}"
if redis.get(timeout_key):
if self.r.get(timeout_key):
return {
"status_code": HTTPStatus.TOO_MANY_REQUESTS,
"status": False,
Expand All @@ -162,9 +161,9 @@ def update_user_info(self, username: str, data: dict) -> dict:
context = {"username": username, "text": text}
send_mail(user_email, subject, context)
# 发送成功才设置缓存
redis.set(timeout_key, username, ex=1800)
redis.hset(user_email, mapping={"code": verify_code, "wrong": 0})
redis.expire(user_email, 24 * 3600)
self.r.set(timeout_key, username, ex=1800)
self.r.hset(user_email, mapping={"code": verify_code, "wrong": 0})
self.r.expire(user_email, 24 * 3600)

self.db["users"].update_one({"username": username}, {"$set": valid_data})
return {
Expand Down Expand Up @@ -199,11 +198,10 @@ def get_avatar(self, username, user_hash=None):
return {"image": None, "content_type": None}


class UserEmail(Mongo):
class UserEmail(Mongo, Redis):
def verify_email(self, username, code):
r = Redis().r
email = self.db["users"].find_one({"username": username})["email"]["address"]
verify_data = r.hgetall(email)
verify_data = self.r.hgetall(email)
wrong_count = int(verify_data["wrong"])
MAX = 10
if wrong_count >= MAX:
Expand All @@ -219,16 +217,16 @@ def verify_email(self, username, code):
correct_code = verify_data["code"]

if correct_code == code:
r.expire(email, 0)
r.expire(f"timeout-{email}", 0)
self.r.expire(email, 0)
self.r.expire(f"timeout-{email}", 0)
self.db["users"].update_one({"username": username}, {"$set": {"email.verified": True}})
return {
"status": True,
"status_code": HTTPStatus.CREATED,
"message": "邮箱已经验证成功",
}
else:
r.hset(email, "wrong", wrong_count + 1)
self.r.hset(email, "wrong", wrong_count + 1)
return {
"status": False,
"status_code": HTTPStatus.FORBIDDEN,
Expand Down
4 changes: 2 additions & 2 deletions yyetsweb/handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from tornado import gen, web
from tornado.concurrent import run_on_executor

from databases.base import Redis
from databases.base import redis_client
from handlers import cf

index = pathlib.Path(__file__).parent.parent.joinpath("templates", "index.html").as_posix()
Expand All @@ -34,7 +34,7 @@ def __init__(self, application, request, **kwargs):
module = importlib.import_module(f"databases.{self.filename}")

self.instance = getattr(module, class_name, lambda: 1)()
self.r = Redis().r
self.r = redis_client

def add_tauri(self):
origin = self.request.headers.get("origin", "")
Expand Down

0 comments on commit 3353f36

Please sign in to comment.