Skip to content

Commit

Permalink
allow to set cache timeout and staggering in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrDlouhy committed Oct 6, 2023
1 parent a864d09 commit 63e37a4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.2.1 (2023-10-06)

- allow the cache timeout and staggering to be changed in settings


## 0.2.0 (2023-10-05)

- introduce staggered cacing to prevent all users hitting the cache miss at once
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,13 @@ This can be achieved by setting ``COUNTABLE_FIELD_RELATED_NAMES`` property in ``
}
```

## Settings

The cache timeout and staggering can be changed by:

```python
CATEGORIES_SETTINGS = {
"CACHE_TIMEOUT": 60 * 60,
"CACHE_STAGGERING": 60 * 10,
}
```
33 changes: 22 additions & 11 deletions categories_api/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@
from categories.models import Category


def staggered_cache_page(timeout, staggering=600):
# Introduce a variation to prevent all users hitting the cache miss at once
variation = random.randint(0, staggering)
adjusted_timeout = timeout + variation

return cache_page(adjusted_timeout)


class CategorySerializer(serializers.ModelSerializer):
class Meta:
model = Category
Expand Down Expand Up @@ -48,10 +40,29 @@ def _get_children(self, obj):
return TreeCategorySerializer(children, many=True).data


CACHE_TIMEOUT = 60 * 60
CACHE_STAGGERING = 60 * 10


if hasattr(settings, "CATEGORIES_SETTINGS"):
countable_field_names = getattr(settings, "CATEGORIES_SETTINGS").get("COUNTABLE_FIELD_RELATED_NAMES", [])
categories_settings = getattr(settings, "CATEGORIES_SETTINGS")
countable_field_names = categories_settings.get("COUNTABLE_FIELD_RELATED_NAMES", [])
if hasattr(categories_settings, "CACHE_TIMEOUT"):
CACHE_TIMEOUT = settings["CATEGORIES_SETTINGS"]["CACHE_TIMEOUT"]
if hasattr(categories_settings, "CACHE_STAGGERING"):
CACHE_STAGGERING = settings["CATEGORIES_SETTINGS"]["CACHE_STAGGERING"]
else:
countable_field_names = []


def staggered_cache_page(timeout, staggering=CACHE_STAGGERING):
# Introduce a variation to prevent all users hitting the cache miss at once
variation = random.randint(0, staggering)
adjusted_timeout = timeout + variation

return cache_page(adjusted_timeout)


countable_fields = [
f
for f in Category._meta.get_fields()
Expand Down Expand Up @@ -130,10 +141,10 @@ def get_serializer_class(self):

permission_classes = (permissions.IsAuthenticatedOrReadOnly,)

@method_decorator(staggered_cache_page(60 * 60))
@method_decorator(staggered_cache_page(CACHE_TIMEOUT))
def list(self, *args, **kwargs):
return super().list(*args, **kwargs)

@method_decorator(staggered_cache_page(60 * 60))
@method_decorator(staggered_cache_page(CACHE_TIMEOUT))
def retrieve(self, *args, **kwargs):
return super().retrieve(*args, **kwargs)

0 comments on commit 63e37a4

Please sign in to comment.