Skip to content

Commit

Permalink
feat: add expire time
Browse files Browse the repository at this point in the history
  • Loading branch information
heng1025 committed Aug 18, 2022
1 parent 2684e4b commit cf78ac6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
5 changes: 3 additions & 2 deletions app/api/web.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from email.policy import default
import json

from app.libs.db import db_helper
Expand Down Expand Up @@ -96,6 +95,8 @@ def get_news():
if page == default_page and page_size == default_page_size:
top_20 = list(map(json.dumps, data))
redis_helper.set_list(prex_key, *top_20)
# cache 6h
redis_helper.set_expire(prex_key, 6 * 60 * 60)

return show_reponse(
data={
Expand All @@ -113,7 +114,7 @@ def get_news_by_id():
if not data:
return show_reponse(code=Status.other, message="param error")
article_id = data.get("id")

prefix_key = f"pbs_{article_id}"
if redis_helper.has(prefix_key):
detail = redis_helper.get(prefix_key)
Expand Down
3 changes: 3 additions & 0 deletions app/libs/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def __init__(self) -> None:
else:
logger.error("Connect Redis Fail!")

def set_expire(self, name, time):
return self.__conn.expire(name, time)

def has(self, name):
return self.__conn.exists(name) == 1

Expand Down
14 changes: 13 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,23 @@ services:
- "3308:3306"
volumes:
# init database and table
- ./init_db.sql:/docker-entrypoint-initdb.d/init_db.sql
- ./init_db.sql:/docker-entrypoint-initdb.d/init_db.sql
- ./mysql-data:/var/lib/mysql
networks:
- db
environment:
MYSQL_DATABASE: ai_english
MYSQL_ROOT_PASSWORD: abc124

redis:
image: redis
command: redis-server /usr/local/etc/redis/redis.conf
volumes:
- ./redis-cnf:/usr/local/etc/redis
- ./redis-data:/data
networks:
- redis

# uwsgi
api:
build: .
Expand All @@ -34,6 +44,7 @@ services:
- api
depends_on:
- db
- redis
env_file: .env
environment:
UWSGI_SOCKET: 0.0.0.0:8000
Expand Down Expand Up @@ -93,5 +104,6 @@ services:

networks:
db:
redis:
api:
web:

0 comments on commit cf78ac6

Please sign in to comment.