A cacheable Django model
Project description
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 fromsettings.CACHES
. If not set default alias is name"default"
and must be configured in CACHES.CACHE_SET_MANY_LIMIT
is chunk size for calls tocache.set_many
.
whenall_ins_from_cache
brings in all entries from cache, it will set each object
in chunks to control request size. Default is5
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 of5
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
Project details
Release history Release notifications | RSS feed
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
Close
Hashes for django_cacheable_model-1.2.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a0501508a6d3ae75f16b5bf124c5a66061c676cc636957793017b56f852d9c6 |
|
MD5 | 72f407ef2c15d013e262aff8832196ba |
|
BLAKE2b-256 | 865bfd2ffc27a6c60b6259cab576f10c7297421b6e6e36ce0361352e594af74f |
Close
Hashes for django_cacheable_model-1.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 069d1a546f20a6ed2a8008470c0de58332d11a7e89eb4885da5069956eaed8f9 |
|
MD5 | a1c48c26e422b9973ab06865ca5f4bc5 |
|
BLAKE2b-256 | cdfa3ac35040f04bfa4b5ee0730fb0c85b9e98d295f6419ed5f1086d812d0e03 |