Skip to content

Commit cf78ac6

Browse files
committed
feat: add expire time
1 parent 2684e4b commit cf78ac6

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

app/api/web.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from email.policy import default
21
import json
32

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

100101
return show_reponse(
101102
data={
@@ -113,7 +114,7 @@ def get_news_by_id():
113114
if not data:
114115
return show_reponse(code=Status.other, message="param error")
115116
article_id = data.get("id")
116-
117+
117118
prefix_key = f"pbs_{article_id}"
118119
if redis_helper.has(prefix_key):
119120
detail = redis_helper.get(prefix_key)

app/libs/redis.py

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ def __init__(self) -> None:
2323
else:
2424
logger.error("Connect Redis Fail!")
2525

26+
def set_expire(self, name, time):
27+
return self.__conn.expire(name, time)
28+
2629
def has(self, name):
2730
return self.__conn.exists(name) == 1
2831

docker-compose.yml

+13-1
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,23 @@ services:
1111
- "3308:3306"
1212
volumes:
1313
# init database and table
14-
- ./init_db.sql:/docker-entrypoint-initdb.d/init_db.sql
14+
- ./init_db.sql:/docker-entrypoint-initdb.d/init_db.sql
15+
- ./mysql-data:/var/lib/mysql
1516
networks:
1617
- db
1718
environment:
1819
MYSQL_DATABASE: ai_english
1920
MYSQL_ROOT_PASSWORD: abc124
2021

22+
redis:
23+
image: redis
24+
command: redis-server /usr/local/etc/redis/redis.conf
25+
volumes:
26+
- ./redis-cnf:/usr/local/etc/redis
27+
- ./redis-data:/data
28+
networks:
29+
- redis
30+
2131
# uwsgi
2232
api:
2333
build: .
@@ -34,6 +44,7 @@ services:
3444
- api
3545
depends_on:
3646
- db
47+
- redis
3748
env_file: .env
3849
environment:
3950
UWSGI_SOCKET: 0.0.0.0:8000
@@ -93,5 +104,6 @@ services:
93104

94105
networks:
95106
db:
107+
redis:
96108
api:
97109
web:

0 commit comments

Comments
 (0)