Metadata-Version: 2.1
Name: django_model_cached_property
Version: 0.0.2
Summary: Django model cached property
Home-page: https://github.com/Legotckoi/django_model_cached_property/
Author: Evgenii Legotckoi
Author-email: legotskoy@gmail.com
License: MIT License
Project-URL: Bug Tracker, https://github.com/Legotckoi/django_model_cached_property/issues
Project-URL: repository, https://github.com/Legotckoi/django_model_cached_property
Keywords: django model cached property
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: Web Environment
Classifier: Framework :: Django
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
Requires-Python: >=3.5
Description-Content-Type: text/markdown
License-File: LICENSE.md
License-File: AUTHORS.md
License-File: AUTHORS.txt

# 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.
