django_model_cached_property
Django model cached property is useful for caching of property results for more time than lifetime of object during the request
This package use redis like a caching backend, because redis allow to delete keys by template. It is usefull for caching different keys for different users.
Install
pip install -U django-model-cached-property
Install and configure requirements
Install redis for caching.
sudo apt install redis-server
Configure setup.py of your django project.
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
}
}
Using
Package contains two functions:
- model_cached_property - it is decorator for class methods, which will cache results of property call for separate records.
- invalidate_model_cached_property - it is function for invalidation of method on the model record property.
model_cached_property
You can use this decorator by following way.
from django_model_cached_property import model_cached_property
class Article(models.Model):
@model_cached_property
def comments_count(self):
return self.comments.count()
It means you cache comments count for each record of article in your Django project.
You can set up cache timeout 3000 second by following way.
class Article(models.Model):
@model_cached_property(timeout=3000)
def comments_count(self):
return self.comments.count()
By default, caching timeout is 60 second, by you can set up it globally in settings.py.
MODEL_CACHED_PROPERTY_TIMEOUT = 300000
Additionally, you can use caching of model property with input arguments. And in this case caching will be evaluated for all input sets of arguments.
For example caching by authenticated user.
class Article(models.Model):
@model_cached_property
def __user_in_bookmarks(self, user):
return self.bookmarks.filter(user=user).exists()
def user_in_bookmarks(self, user):
return self.__user_in_bookmarks(user) if user.is_authenticated else False
invalidate_model_cached_property
This function invalidate all cache keys on the property for one model record in database.
For example
article = get_object_or_404(Article, pk=12)
invalidate_model_cached_property(article, article.comments_count)
In this case you invalidate all cached keys for article with primary key 2.
WARNING - Limitation
Limitation of this caching functionality consists in the fact that you can use it with unique input arguments only. It means, that will not work with AnonymousUser object, because of in each request information about AnonymousUser object will be different, although it will be called by same user. Therefore, use it on unique information only.
Metadata
Release files for django-model-cached-property 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django_model_cached_property-0.0.2.tar.gz | 4.9 kB | Without provenance | Details for django_model_cached_property-0.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_model_cached_property-0.0.2-py3-none-any.whl | Python 3 | none | any | Details for django_model_cached_property-0.0.2-py3-none-any.whl |
Total release size: 10.8 kB
Release files / django_model_cached_property-0.0.2.tar.gz
| Download URL | django_model_cached_property-0.0.2.tar.gz |
|---|---|
| Size | 4.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5bf756e76b39d214e6f05fee56e5153844e8510b34c3360027bb13df18e7a18e
|
|
BLAKE2b-256 checksum How to use checksums |
d52e55d2cd8539408e36badd592388f68c4f6559c052dee9b4a75c57f71c047a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.8.10
|
Release files / django_model_cached_property-0.0.2-py3-none-any.whl
| Download URL | django_model_cached_property-0.0.2-py3-none-any.whl |
|---|---|
| Size | 5.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bb1abb3a0746b146e5171e61266dc97acfc31c1bcfdecd088b77611d4cea929a
|
|
BLAKE2b-256 checksum How to use checksums |
4087e8f4761b48349a5cce30bf2fc7330c09435372a68099b3e5a204f3e771ba
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.8.10
|
Security for django-model-cached-property 0.0.2
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.