Skip to main content

Speed up Django REST Framework (DRF) reads by storing instance data in cache.

This code was split from browsercompat. You may be interested in viewing the browsercompat source code for a full example implementation.

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 browsercompat project, which was developed from 2015 - 2016, but is on hold as of August 2016. Since this was the primary user of this code, it may be a while before more features are implemented.

History

0.3.4 (2016-08-14)

  • Drop support for Django 1.7, Python 2.6

  • Expand tests to Django 1.10, Django REST Framework 3.4, Python 3.5

0.3.3 (2015-11-05)

  • Add serializer for datetime.timedelta, to support Django 1.8’s DurationField (creynold)

  • Convert string representations of datetimes, dates, and timedeltas

  • Expand tests to Django 1.9 beta 1 and Django REST Framework 3.3

0.3.2 (2015-09-23)

  • Remove deprecation warning in Django 1.8

  • Expand tests to Django master and Django REST Framework 3.2

  • Fix invalid mock.patch tests that break under mock 1.3.0

  • Documentation updates and fixes

0.3.1 (2015-06-16)

  • Move get_object_or_404 to mixin method, to allow easier extending.

0.3.0 (2015-04-09)

  • Tested with Django 1.8

  • Tested with Django REST Framework 2.4, 3.0, and 3.1

  • CachedModel now supports .pk attribute as an alias, usually to the .id field. DRF 3 uses .pk to determine if a model is saved to database, and returns empty relation data for unsaved fields.

  • cache.delete_all_versions() will delete all cached instances of a model and PK. This is useful when changes are made outside of normal requests, such as during a data migration.

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.

Release files for drf-cached-instances 0.3.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for drf-cached-instances 0.3.4
File Size Uploaded
drf-cached-instances-0.3.4.tar.gz 40.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for drf-cached-instances 0.3.4
File Interpreter ABI Platform
drf_cached_instances-0.3.4-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 54.0 kB

Release files / drf-cached-instances-0.3.4.tar.gz

Download URL drf-cached-instances-0.3.4.tar.gz
Size 40.9 kB
Tags Source
SHA-256 checksum
How to use checksums
63c81f04b9624997005f9b1e1b125e5465cc0fc46b870f3a128f7975ab49d890
BLAKE2b-256 checksum
How to use checksums
2566d4fa2347c2b22b7cb5a11f3d5fb27913b154d75d38cf86d9c1206e8e3b8c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / drf_cached_instances-0.3.4-py2.py3-none-any.whl

Download URL drf_cached_instances-0.3.4-py2.py3-none-any.whl
Size 13.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
d07200254a43c407bffc92c088ea592284f34cffe0add0c5aa48e6daf273d24c
BLAKE2b-256 checksum
How to use checksums
d4e166daaf608326b2dcc50fb52d699bcd462ea441cd6941c6bc83ed9f8b3553
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.3.4 This release

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page