Easy value caching on Django.
Project description
django-cached-fields
Cached fields for Django
Usage
Defining a basic Cached Field
The first argument of the CachedField constructor can be the name of a method on the class.
This method only works with field triggers as signal triggers will require a CachedFieldSignalHandler
.
class Invoice(models.Model):
name = models.TextField(null=False)
quantity = models.IntegerField()
amount = models.IntegerField(null=False)
def calc_total(self):
self.total = self.quantity * self.amount
total = CachedIntegerField(
'calc_total',
field_triggers=[
'amount',
],
timeout=timedelta(seconds=5),
)
If you would like your cached field's callback to be triggered by a signal, you will need to create a signal handler.
class InvoiceSignalHandler(CachedFieldSignalHandler):
def calc_total(instance):
instance.total = instance.quantity * instance.amount
@for_class('Customer')
def cutomer_total(customer):
return invoice.quantity * invoice.amount
@for_class('Supplier')
def supplier_total(supplier):
invoice = supplier.invoice.last()
return invoice.quantity * invoice.amount
If you would like to offload task processing to Celery, django-cached-fields can handle that for you automatically.
settings.py
CACHED_FIELDS_CELERY_ENABLED = True
CACHED_FIELDS_CELERY = {
"queue": "default"
}
If you'd like to offload a recalculation to a specific queue, you can do this.
class InvoiceSignalHandler(CachedFieldSignalHandler):
def calc_total(instance):
instance.total = instance.quantity * instance.amount
@for_class('Customer', queue="low_priority")
def cutomer_total(customer):
invoices = customer.invoices.all()
for i in invoices:
i.cache_toolkit.update_cache('total', i.quantity * i.amount)
i.save()
@for_class('Supplier')
def supplier_total(supplier):
invoice = supplier.invoice.last()
invoice.total = invoice.quantity * invoice.amount
Force Recalculation
ym = YourModel.objects.create(name="fart", amount=6)
ym.cache_toolchain.refresh_field('total')
Get last time the cache was updated
For when you need to run queries ```python In [2]: print(ym.total_last_update) Out[2]: datetime.datetime(2019,10,30,4,30,54)
In [3]: YourModel.objcts.filter(total_last_update__date__gte=date(2019,10,1)).exists() Out[3]: True
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
Hashes for django-cached-fields-0.1.0a2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | e56881a7ac40d7667e08f4818e40e9fdb1b87c5650d5753b03892585da072f32 |
|
MD5 | 7d31aa087d335e890dc4f994a36db1c9 |
|
BLAKE2b-256 | fddf67202e390289d9b4349bf7bd19b59c99a547519100e27f8cfc62d8862e1e |
Hashes for django_cached_fields-0.1.0a2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9a6369f3be3e746ea4f913b964ef3898d9bbae58cc3ba050632efe08adc84e98 |
|
MD5 | 8301bd45bc8837b26a4290ef657a7008 |
|
BLAKE2b-256 | 887db8fe042244a8e66f260378594fc7a478a26ae6a53e89df19cd0b506e0a98 |