Cached instances for Django REST Framework
Project description
Speed up Django REST Framework (DRF) reads by storing instance data in cache.
This code was split from web-platform-compat. You may be interested in viewing the web-platform-compat source code for a full example implementation.
Free software: Mozilla Public License Version 2.0
Documentation: https://drf-cached-instances.readthedocs.org
Generated from: https://github.com/jwhitlock/cookiecutter-django-jw.git
How it works
In a normal DRF view, a Django queryset is used to load an object or list of objects. A serializer is used to convert the objects into the “native” representation, and then a renderer works on this native representation. If the serializer includes data from related models, then multiple database queries may be required to generate a native representation. Some database efficiency can be gained by using select_related, but a minimum of one query is needed, which is unfortunate for an API with heavy read usage.
This project replaces the Django queryset with a cache-aware proxy class, making it possible to serve a read request with zero database requests (to retrieve an instance) or one request (to get the primary keys for a list view). It is suitable for APIs with heavy read operations and lots of linking between related instances.
When using the cache, Django objects are serialized to JSON. Only the attributes needed for the DRF native representation are stored in the cache. This include the JSON representation of fields such as foreign keys, reverse relations, and dates and times. These serialized objects are stored by primary key in the cache. When an instance is found in the cache, no database reads are needed to render the DRF representation. If the instance is not in the cache, it is serialized and stored, so that future reads will be faster.
The API implementor writes methods to handle JSON serialization, loading from the database, and identifying invalid cache entries on changes. There are a few integration points, including a mixin for views to load data from the cache. With only a few changes to existing code, your read views could be a lot faster.
Project status
This code is used for the web-platform-compat project, which is in development as of Novemeber 2014. The code may change in response to the needs of the web-platform-compat project. It is expected to stabilize by early 2015. And, like all open source projects, more documentation is needed.
History
0.2.0 (2014-12-11)
Add update_only option to cache.update_instance, to support eventual consistency for cold caches.
0.1.0 (2014-11-06)
First release on PyPI.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file drf-cached-instances-0.2.0.tar.gz
.
File metadata
- Download URL: drf-cached-instances-0.2.0.tar.gz
- Upload date:
- Size: 39.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8002ded3cb5eb7e67f76cb04d5d08103173686346f1ab9410a1ccf416c4c2fcf |
|
MD5 | 0ed4389b0f496bca709b5fa6eb5d5b33 |
|
BLAKE2b-256 | afd7f32982d33b7c2c089b2768eccdef242ba1443a694207a0de2c6473bdcd63 |
File details
Details for the file drf_cached_instances-0.2.0-py2.py3-none-any.whl
.
File metadata
- Download URL: drf_cached_instances-0.2.0-py2.py3-none-any.whl
- Upload date:
- Size: 11.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d40630bbda761ea853b0fda036d1e0376bd0cb156fa5b34f2c0742973d18d481 |
|
MD5 | 60c9b69ab57e880505cd698b465c4822 |
|
BLAKE2b-256 | 4140f814d94e22f2117300353f7e07f21070ee6d6ca10d3fc9c1209db00ac4a3 |