Skip to content

Commit

Permalink
cache make_svg
Browse files Browse the repository at this point in the history
  • Loading branch information
kittinan committed Jul 21, 2020
1 parent bee3ba3 commit b6b9902
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions api/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ def load_image_b64(url):
return b64encode(resposne.content).decode("ascii")


def make_svg(item, is_now_playing):
@functools.lru_cache(maxsize=128)
def make_svg(artist_name, song_name, img, is_now_playing):

print("make_svg")

height = 445
num_bar = 75
Expand All @@ -68,11 +71,6 @@ def make_svg(item, is_now_playing):

css_bar = generate_css_bar(num_bar)

img = load_image_b64(item["album"]["images"][1]["url"])
artist_name = item["artists"][0]["name"]
song_name = item["name"]
url = item["external_urls"]["spotify"]

rendered_data = {
"height": height,
"num_bar": num_bar,
Expand Down Expand Up @@ -162,7 +160,11 @@ def catch_all(path):
item = recent_plays["items"][idx]["track"]
is_now_playing = False

svg = make_svg(item, is_now_playing)
img = load_image_b64(item["album"]["images"][1]["url"])
artist_name = item["artists"][0]["name"]
song_name = item["name"]

svg = make_svg(artist_name, song_name, img, is_now_playing)

resp = Response(svg, mimetype="image/svg+xml")
resp.headers["Cache-Control"] = "s-maxage=1"
Expand Down

1 comment on commit b6b9902

@vercel
Copy link

@vercel vercel bot commented on b6b9902 Jul 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.