https://github.com/null-none/drf-mixin-cache
Cache provides easy to use cache framework for django-rest-framwork apps
https://github.com/null-none/drf-mixin-cache
cache django django-rest-framework drf mixins
Last synced: 25 days ago
JSON representation
Cache provides easy to use cache framework for django-rest-framwork apps
- Host: GitHub
- URL: https://github.com/null-none/drf-mixin-cache
- Owner: null-none
- License: mit
- Created: 2024-08-17T05:52:33.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-18T12:43:40.000Z (almost 2 years ago)
- Last Synced: 2025-01-26T09:28:25.708Z (over 1 year ago)
- Topics: cache, django, django-rest-framework, drf, mixins
- Language: Python
- Homepage:
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# drf-mixin-cache
Easy to use cache framework for django-rest-framwork apps.
# Installation
Install using `pip`...
pip install drf_mixin_cache
Add `'drf_mixin_cache'` to your `INSTALLED_APPS` setting.
```python
INSTALLED_APPS = (
...
'drf_mixin_cache',
)
```
You must execute autodiscover to load your serializers. To do this change your `urls.py` adding the following code:
```python
from drf_mixin_cache.registry import cache_registry
cache_registry.autodiscover()
```
# Usage
```python
from rest_framework import serializers
# You must import the CachedSerializerMixin and cache_registry
from rest_framework_cache.serializers import CachedSerializerMixin
from rest_framework_cache.registry import cache_registry
from .models import User
class UserSerializer(serializers.ModelSerializer, CachedSerializerMixin):
class Meta:
model = User
cache_registry.register(UserSerializer)
```
## Using cache backend different of the default
If you need use a cache backend different of the default you can specify it on the `REST_FRAMEWORK_CACHE`.
To do this edit your `settings.py` like this:
```python
# ...
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
},
'locmem': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'unique-snowflake',
}
}
REST_FRAMEWORK_CACHE = {
'DEFAULT_CACHE_BACKEND': 'locmem',
}
# ...
```
## Cache timeout
You can set the cache timeout using `DEFAULT_CACHE_TIMEOUT`.
```python
REST_FRAMEWORK_CACHE = {
'DEFAULT_CACHE_TIMEOUT': 86400, # Default is 1 day
}
```