Skip to content

Commit e382c61

Browse files
author
codeEmpress1
authored
add cache to cves data (#5484)
* add cache to cves data * fix failing tests
1 parent 5f0ea97 commit e382c61

4 files changed

Lines changed: 51 additions & 1 deletion

File tree

tests/endpoints/cve/test_has_cve_api.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from unittest.mock import patch
22
from tests.endpoints.endpoint_testing import TestEndpoints
3+
from cache.cache_utility import redis_cache
34

45

56
class TestCveEndpoints(TestEndpoints):
@@ -24,6 +25,14 @@ def _set_user_is_canonical(self, value):
2425

2526
def setUp(self):
2627
super().setUp()
28+
# Clear cache before each test
29+
if redis_cache.redis_available:
30+
try:
31+
redis_cache.client.flushdb()
32+
except Exception:
33+
pass
34+
else:
35+
redis_cache.fallback.clear()
2736
self._log_in_with_canonical_status(is_canonical=False)
2837

2938

tests/endpoints/test_cve_get_by_revision.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,20 @@
33
import json
44
from webapp.publisher.cve.cve_helper import CveHelper
55
from werkzeug.exceptions import NotFound
6+
from cache.cache_utility import redis_cache
67

78

89
class CveHGetByRevisionTest(unittest.TestCase):
910

1011
def setUp(self):
12+
# Clear cache before each test
13+
if redis_cache.redis_available:
14+
try:
15+
redis_cache.client.flushdb()
16+
except Exception:
17+
pass
18+
else:
19+
redis_cache.fallback.clear()
1120

1221
self.file_metadata = {"download_url": "https://example.com/file.json"}
1322

tests/publisher/cve/test_has_cve.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,21 @@
33

44
from webapp.publisher.cve.cve_helper import CveHelper
55
from werkzeug.exceptions import NotFound
6+
from cache.cache_utility import redis_cache
67

78

89
class HasRevisionsWithCvesTest(unittest.TestCase):
910

11+
def setUp(self):
12+
# Clear cache before each test
13+
if redis_cache.redis_available:
14+
try:
15+
redis_cache.client.flushdb()
16+
except Exception:
17+
pass
18+
else:
19+
redis_cache.fallback.clear()
20+
1021
@patch("webapp.publisher.cve.cve_helper.CveHelper._get_cve_file_metadata")
1122
def test_returns_revision_numbers(self, mock_get_metadata):
1223
mock_get_metadata.return_value = [

webapp/publisher/cve/cve_helper.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from webapp.publisher.snaps import (
99
logic,
1010
)
11+
from cache.cache_utility import redis_cache
1112

1213
REST_API_URL = "https://api.github.com"
1314
GITHUB_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

Comments
 (0)