88from webapp .publisher .snaps import (
99 logic ,
1010)
11+ from cache .cache_utility import redis_cache
1112
1213REST_API_URL = "https://api.github.com"
1314GITHUB_SNAPCRAFT_BOT_USER_TOKEN = getenv ("GITHUB_SNAPCRAFT_BOT_USER_TOKEN" )
@@ -118,6 +119,12 @@ def _fetch_file_content(snap_name, revision, file_metadata):
118119
119120 @staticmethod
120121 def get_revisions_with_cves (snap_name ):
122+ cve_with_revisions_cache_key = f"cve_revisions:{ snap_name } "
123+ cached_revision_files = redis_cache .get (
124+ cve_with_revisions_cache_key , expected_type = list
125+ )
126+ if cached_revision_files :
127+ return cached_revision_files
121128 try :
122129 contents = CveHelper ._get_cve_file_metadata (
123130 f"snap-cves/{ snap_name } "
@@ -131,21 +138,35 @@ def get_revisions_with_cves(snap_name):
131138 for item in contents
132139 if (match := re .match (r"(\d+)\.yaml$" , item ["name" ]))
133140 ]
141+ redis_cache .set (
142+ cve_with_revisions_cache_key , revision_files , ttl = 3600
143+ )
134144
135145 return revision_files
136146 except NotFound :
137147 return []
138148
139149 @staticmethod
140150 def get_cve_with_revision (snap_name , revision ):
151+ cve_with_revision_cache_key = f"cves:{ snap_name } :{ revision } "
152+ cached_file_content = redis_cache .get (
153+ cve_with_revision_cache_key , expected_type = list
154+ )
155+ if cached_file_content :
156+ return cached_file_content
157+
141158 file_metadata = CveHelper ._get_cve_file_metadata (
142159 "snap-cves/{}.json" .format (snap_name )
143160 )
144161
145162 if file_metadata :
146- return CveHelper ._fetch_file_content (
163+ file_content = CveHelper ._fetch_file_content (
147164 snap_name , revision , file_metadata
148165 )
166+ redis_cache .set (
167+ cve_with_revision_cache_key , file_content , ttl = 3600
168+ )
169+ return file_content
149170 return []
150171
151172 @staticmethod
0 commit comments