diff --git a/license_library/models.py b/license_library/models.py index 4f4e8c3d..baa446fd 100644 --- a/license_library/models.py +++ b/license_library/models.py @@ -828,9 +828,10 @@ def __str__(self): return f"{self.short_name} ({self.key})" def save(self, *args, **kwargs): - """Clear the licensing cache on License object changes.""" + """Clear the licensing cache entry of the License Dataspace on changes.""" super().save(*args, **kwargs) - caches["licensing"].clear() + # Delete only this Dataspace entry: `clear()` would flush the whole Redis database. + caches["licensing"].delete(str(self.dataspace.uuid)) def clean(self, from_api=False): if self.is_active is False and self.spdx_license_key: diff --git a/license_library/tests/test_models.py b/license_library/tests/test_models.py index 93f8ff11..0ffbec1f 100644 --- a/license_library/tests/test_models.py +++ b/license_library/tests/test_models.py @@ -8,6 +8,7 @@ from collections import OrderedDict +from django.core.cache import caches from django.test import TestCase from license_expression import LicenseSymbol @@ -498,6 +499,21 @@ def test_license_model_get_all_urls(self): ] self.assertEqual(expected, self.license1.get_all_urls()) + def test_license_model_save_deletes_dataspace_licensing_cache_entry(self): + licensing_cache = caches["licensing"] + dataspace_key = str(self.dataspace.uuid) + other_dataspace_key = str(self.other_dataspace.uuid) + licensing_cache.set(dataspace_key, "licensing") + licensing_cache.set(other_dataspace_key, "other_licensing") + caches["default"].set("session", "data") + self.addCleanup(caches["default"].delete, "session") + + self.license1.save() + + self.assertIsNone(licensing_cache.get(dataspace_key)) + self.assertEqual("other_licensing", licensing_cache.get(other_dataspace_key)) + self.assertEqual("data", caches["default"].get("session")) + class LicenseChoiceModelTestCase(TestCase): def setUp(self):