Skip to content

Commit 7273d1d

Browse files
author
codeEmpress1
authored
add cache to webapp/blog (#5493)
* add cache to webapp/blog * fix lint * update cache keys
1 parent 55f992e commit 7273d1d

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

webapp/blog/views.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
build_blueprint,
99
NotFoundError,
1010
)
11+
from cache.cache_utility import redis_cache
1112
from dateutil import parser
1213
from requests.exceptions import RequestException
1314

@@ -30,6 +31,13 @@ def init_blog(app, url_prefix):
3031

3132
@blog.route("/api/snap-posts/<snap>")
3233
def snap_posts(snap):
34+
cached_articles_key = f"snap_posts:{snap}"
35+
cached_articles = redis_cache.get(
36+
cached_articles_key, expected_type=list
37+
)
38+
if cached_articles:
39+
return flask.jsonify(cached_articles)
40+
3341
try:
3442
blog_tags = blog_api.get_tag_by_slug(f"sc:snap:{snap}")
3543
except NotFoundError:
@@ -76,11 +84,18 @@ def snap_posts(snap):
7684
"image": featured_media,
7785
}
7886
)
79-
87+
redis_cache.set(cached_articles_key, articles, ttl=3600)
8088
return flask.jsonify(articles)
8189

8290
@blog.route("/api/series/<series>")
8391
def snap_series(series):
92+
cached_articles_key = f"snap_series:{series}"
93+
cached_articles = redis_cache.get(
94+
cached_articles_key, expected_type=list
95+
)
96+
if cached_articles:
97+
return flask.jsonify(cached_articles)
98+
8499
blog_articles = None
85100
articles = []
86101

@@ -96,7 +111,7 @@ def snap_series(series):
96111
"title": article["title"]["rendered"],
97112
}
98113
)
99-
114+
redis_cache.set(cached_articles_key, articles, ttl=3600)
100115
return flask.jsonify(articles)
101116

102117
@blog.context_processor

0 commit comments

Comments
 (0)