Skip to main content

django cacheable model

A cacheable model for django.

  • A generic way of creating cache keys from Django model fields
  • Retrieve django models from cache with field values (cache on the way if cache missed)
  • Retrieve all the model instances (suitable for small set of models)
  • Support for cache aliases

See usage example below

1. Install

pip install django_cacheable_model

2. Configuration

  • DEFAULT_CACHE_ALIAS the default cache name to use from settings.CACHES. If not set default alias is name "default" and must be configured in CACHES.
  • CACHE_SET_MANY_LIMIT is chunk size for calls to cache.set_many.
    when all_ins_from_cache brings in all entries from cache, it will set each object
    in chunks to control request size. Default is 5 i.e if there are 10 instances of a model
    from db this config will set each of the models to the cache in two groups of 5
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

CACHE_DEFAULT_ALIAS = 'example_cache_alias'
CACHE_SET_MANY_LIMIT = 10
CACHES = {
   ...,
    'example_cache_alias': {
        'BACKEND': 'django.core.cache.backends.memcached.PyMemcacheCache',
        'LOCATION': 'memcached:11211',
    },
    ...
}

3. Usage

See samples in example_django_project views.py and models.py.

3.1. Create a model that inherits from CacheableModel

class Question(CacheableModel):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')


class Choice(CacheableModel):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

3.2. Use cache operations from django_cacheable_model.utils

from django_cacheable_model.utils import all_ins_from_cache, model_ins_from_cache

# Get all instances of model from cache (use for smaller set of models)
context['choices'] = all_ins_from_cache(Choice)

# Get all instances with select_related and order_by
choices = all_ins_from_cache(Choice,
                             select_related=('question',),
                             order_by_fields=('-id',))

# Get a single model. Note this method returns a list of matching objects
context['choice'] = model_ins_from_cache(Choice, {'id': 5})[-1]

4. License

Apache2 License

Metadata

Release files for django-cacheable-model 1.2.7

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

Source distribution (sdist)

Source distribution for django-cacheable-model 1.2.7
File Size Uploaded Provenance Actions
django_cacheable_model-1.2.7.tar.gz 5.1 kB Without provenance Details for django_cacheable_model-1.2.7.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-cacheable-model 1.2.7
File Interpreter ABI Platform Actions
django_cacheable_model-1.2.7-py3-none-any.whl Python 3 none any Details for django_cacheable_model-1.2.7-py3-none-any.whl

Total release size: 11.1 kB

Release files / django_cacheable_model-1.2.7.tar.gz

Download URL django_cacheable_model-1.2.7.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
56925ea7ccafe66ec033edcbb3fb95d99f77a4940c052dc8fca6ba3bb2c02fbd
BLAKE2b-256 checksum
How to use checksums
31ad74cd28aa3a361114ddca4a47e8494f88f0b94168584b4a644bbd87cd525a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release files / django_cacheable_model-1.2.7-py3-none-any.whl

Download URL django_cacheable_model-1.2.7-py3-none-any.whl
Size 6.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a9d71f1b5e4c14b8e6090e9b266e10090725ff2c20600e8bc08d05b790c4caff
BLAKE2b-256 checksum
How to use checksums
aa782caae61d993ede91d2f403802caf495e288a4325d4ef30ac8c5545df30ba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Security for django-cacheable-model 1.2.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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