Skip to content
Snippets Groups Projects
Commit 3b91670b authored by Robbie Luijben's avatar Robbie Luijben
Browse files

Merge branch 'hotfix/set-cache-expiration' into 'main'

Set cache expiration timeout explicitly (instead of depending on the 5 minute default)

See merge request !100
parents c0e15287 f1650236
No related branches found
No related tags found
1 merge request!100Set cache expiration timeout explicitly (instead of depending on the 5 minute default)
Pipeline #45695 passed
......@@ -226,6 +226,28 @@ Subsequently, navigate to `/ldvspec/silk/` to visit the profiler dashboard.
> python.manage.py migrate --settings=ldvspec.settings.docker_sdc_profiling
```
## Caching
We use the Django Cache Framework, which is a generic API into which you can plug different types of cache. Some common ones are Memcached and Redis. This generic API is described here: https://docs.djangoproject.com/en/4.1/topics/cache/. In LDVspec, we use Memcached as caching backend. This is defined in the settings file like so:
```
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.PyMemcacheCache',
'LOCATION': f'{os.environ["CACHE_HOST_SERVER"]}:{os.environ["CACHE_HOST_PORT"]}',
}
}
```
When looking at documentation on how to use caching, look at the Django Cache Framework documentation listed above. Don't look at the documentation of the underlying cache, because their API will probably be different. By using the Django Cache Framework, we can abstract away the underlying implementation and consistently use the Django API.
For example, when you add something to the cache using `cache.set` function, there is a parameter for expiration timeout, for example: `cache.set('my_key', 'hello, world!', 30)`. Use the Django Cache Framework definition of this parameter:
`The timeout argument is optional and defaults to the timeout argument of the appropriate backend in the CACHES setting (explained above). It’s the number of seconds the value should be stored in the cache. Passing in None for timeout will cache the value forever. A timeout of 0 won’t cache the value.`
In memcached, you would pass the value `0` for have an infinite timeout. In the Django Cache Framework, you need to pass the value `None` (which will then subsequently pass `0` to Memcached)
*NOTE*: when you add something to the cache, always pass in a timeout explicitly, as it depends on the use case. Don't rely on the framework default (which is 5 minutes).
***
# Troubleshooting
......
......@@ -93,9 +93,9 @@ class TestDataProductFieldCache(dtest.TestCase):
@mock.patch("lofardata.view_helpers.dataproductinfo.cache.get")
@mock.patch("lofardata.view_helpers.dataproductinfo.cache.set")
def test_put_in_cache_with_correct_key_and_data_when_not_cached(self, cache_set_mock, cache_get_mock):
def test_put_in_cache_with_correct_key_and_data_and_expiration_when_not_cached(self, cache_set_mock, cache_get_mock):
cache_get_mock.return_value = None
get_distinct_dataproduct_field('location')
cache_set_mock.assert_called_with("distinct_dataproduct_location", ["Dwingeloo", "Groningen"])
cache_set_mock.assert_called_with("distinct_dataproduct_location", ["Dwingeloo", "Groningen"], None)
......@@ -61,7 +61,8 @@ def get_distinct_dataproduct_field(dataproduct_field):
if not distinct_field_values:
distinct_field_values = list(DataProduct.objects.distinct(dataproduct_field).values_list(dataproduct_field, flat=True))
cache.set(cache_key, distinct_field_values)
expiration_timeout_infinite = None
cache.set(cache_key, distinct_field_values, expiration_timeout_infinite)
return distinct_field_values
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment