Reputation: 115
I am currently using django and I'm attempting to cache my view like so
from django.views.decorators.cache import cache_page
@cache_page(60 * 15)
def index(request):
# This method takes time to run, which is why I need to cache this view
a_method_that_preforms_heavy_db_transactions()
context_dict={'Models': Model.objects.all()}
return render(request, 'webapp/index.html', context_dict)
I have a_method_that_preforms_heavy_db_transactions()
In just to test the loading time to see if it cached the view but the loading time time doesn't change when its supposed to be "cached" and I'm unsure why this is my settings.py
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': 'My_computers_ip_address:11211',
}
}
Upvotes: 1
Views: 6488
Reputation: 15903
You'll need to add a caching service
, e.g Memcached
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211/A_ROUTE_WITH_A_HEAVY_TRANSACTION',
}
}
You'll find everything you need here:
https://docs.djangoproject.com/en/stable/topics/cache/
Upvotes: 3
Reputation: 34704
Have you set caching up? You need to define CACHES
in your settings.py
file. You can use memcached, Redis, your database or a file system. The simplest setup is with local memory as it doesn't require any external services:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'unique-snowflake',
}
}
Upvotes: 1