Skip to main content

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 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 is in development as of September 2015. The code may change in response to the needs of the browsercompat project. It is expected to stabilize by early 2016. And, like all open source projects, more documentation is needed.

History

0.3.3 (2015-11-05)

  • Add serilizer 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.

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

drf-cached-instances-0.3.3.tar.gz (41.1 kB view details)

Uploaded Source

Built Distribution

drf_cached_instances-0.3.3-py2.py3-none-any.whl (13.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file drf-cached-instances-0.3.3.tar.gz.

File metadata

File hashes

Hashes for drf-cached-instances-0.3.3.tar.gz
Algorithm Hash digest
SHA256 e34ad8be712f6a72f0011956a8a4cfb1a096839f9ef5e1db74f7a10896304e88
MD5 0648e5ac943c4991de03e87de937b2b2
BLAKE2b-256 cd4870c549a7d69da040753fbe720af3ffebf2557b39f82b3cfc32ee0f39ed82

See more details on using hashes here.

File details

Details for the file drf_cached_instances-0.3.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for drf_cached_instances-0.3.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c924c8d2ee4b9b62df3919180cb3b4b969dd547a07179bd3e96637973c3fc677
MD5 ca17e4051a72a8ec9b5050d0a2949a62
BLAKE2b-256 bf2659e41c08706dce28c900edae02f98d8a3f8932c9583c94428e8435b01d4b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page