33It is NOT part of the scratchattach Python library and won't be downloaded when you install scratchattach.
44"""
55
6- from flask import Flask , render_template , send_from_directory , jsonify
7- import scratchattach as sa
86import time
97import random
8+ from flask import Flask , render_template , send_from_directory , jsonify
9+ from vercel .cache import RuntimeCache
10+ import scratchattach as sa
1011
1112app = Flask (__name__ , template_folder = "source" )
1213
@@ -27,14 +28,17 @@ def serve_js(filename):
2728 return send_from_directory ('source/js' , filename )
2829
2930# community projects are cached to prevent spamming Scratch's API
30- community_projects_cache : list [dict ] = []
31- last_cache_time = 0
31+ # community_projects_cache: list[dict] = []
32+ # last_cache_time = 0
33+ cache = RuntimeCache (namespace = "website" )
3234
3335@app .route ('/api/community_projects/' )
3436def community_projects ():
35- global community_projects_cache
36- global last_cache_time
37- if time .time () > last_cache_time + 300 :
37+ # global community_projects_cache
38+ # global last_cache_time
39+ community_projects_cache_data = cache .get ("community_projects" )
40+ community_projects_cache = community_projects_cache_data and community_projects_cache_data .get ("value" )
41+ if not community_projects_cache :
3842 projects = sa .Studio (id = 31478892 ).projects (limit = 40 )
3943 if isinstance (projects [0 ], dict ): #atm the server this is running on still uses scratchattach 1.7.4 that returns a list of dicts here
4044 community_projects_cache = [
@@ -44,5 +48,5 @@ def community_projects():
4448 community_projects_cache = [
4549 {"project_id" :p .id , "title" :p .title , "author" :p .author_name , "thumbnail_url" :f"https://uploads.scratch.mit.edu/get_image/project/{ p .id } _480x360.png" } for p in projects
4650 ]
47- last_cache_time = time . time ( )
48- return jsonify (random .choices (community_projects_cache , k = 5 ))
51+ cache . set ( "community_projects" , { "value" : community_projects_cache }, { "ttl" : 300 , "tags" : [ "website" ]} )
52+ return jsonify (random .choices (community_projects_cache , k = 5 ))
0 commit comments