django-cache-utils2 ay isang Django app na nagbibigay ng naka-cache na dekorador at magpawalang-bisa sa function.
Pag-install
PIP install django-cache-utils2
Usage
mula cache_utils2 import-cache, magpawalang-bisa
cached (60)
def foo (x, y = 0):
& Nbsp; print 'foo ay tinatawag na'
& Nbsp; bumalik x + y
foo (1, 2) # foo ay tinatawag na
foo (1, y = 2)
foo (5, 6) # foo ay tinatawag na
foo (5, 6)
magpawalang-bisa (foo, {'x': 1, 'y': 2})
foo (1, 2) # foo ay tinatawag na
foo (5, 6)
foo (x = 2) # foo ay tinatawag na
foo (x = 2)
klase foo (object):
& Nbsp;cached (60)
& Nbsp; def foo (self, x, y):
& Nbsp; print "foo ay tinatawag na"
& Nbsp; bumalik x + y
obj = foo ()
obj.foo (1,2) # foo ay tinatawag na
obj.foo (1,2)
magpawalang-bisa (Foo.foo, {'x': 1, 'y': 2})
obj.foo (1,2) # foo ay tinatawag na
Django halimbawa
mula django.db import na mga modelo
mula cache_utils2 import-cache, magpawalang-bisa
klase CityManager (models.Manager):
& Nbsp; # cache resulta method. Parameter 'sarili' ay hindi pinansin
& Nbsp;cached (60 * 60 * 24)
& Nbsp; def default (self):
& Nbsp; return self.active () [0]
& Nbsp; # cache resulta method. 'Self' parameter ay hindi pinansin, args at
& Nbsp; # kwargs ay ginagamit upang bumuo ng mga key cache
& Nbsp;cached (60 * 60 * 24)
& Nbsp; def makakuha (self, * args, ** kwargs):
& Nbsp; return super (CityManager, self) .get (* args, ** kwargs)
klase City (models.Model):
& Nbsp; # ... field pahayag
& Nbsp; mga bagay = CityManager ()
& Nbsp; # halimbawa paraan model cache django resulta sa pamamagitan ng halimbawa pk
& Nbsp;cached (30, mag-iiba = 'self.pk')
& Nbsp; def has_offers (self):
& Nbsp; return self.offer_set.count ()> 0
#-Bisa ng pamamaraan ng modelo
magpawalang-bisa (City.has_offers, {'self.pk': 1}
Mga Tala
Kung ginayakan function ay nagbabalik cache_utils2.NO_CACHE cache ay bypassed.
Pagpapatakbo ng pagsusulit
. Kunin ang source code at patakbuhin runtests.py
Kinakailangan :
- sawa
- Django
Mga Komento hindi natagpuan