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.6.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2e3c79f0e4e05d3f1a4ea457707c95647e8e0d341b4ec18926ed908aa1815ed5 |
|
MD5 | aa68a60c840aee40ee2a916f29f24ce1 |
|
BLAKE2b-256 | b5adec08d9df5bd1c795c3c2ad9e520bd861ca42298e7d7e15940db4f399c07e |
Close
Hashes for django_cacheable_model-1.2.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3f3e0a1ac6806413f9ea6445c60763bd8f151a9d290b8c15c4d69b5bcfa0a5c3 |
|
MD5 | 2c303ba27cebd3663cd9442f6011c708 |
|
BLAKE2b-256 | 8bb419e0f72a19163ee998dfeb5357e1bf7b3f7d9ee010afc5c00495e476061e |