I am starting use Memcached to make my website faster. For constant data in my database I use this:
from django.core.cache import cache
cache_key = 'regions'
regions = cache.get(cache_key)
if result is None:
"""Not Found in Cache"""
regions = Regions.objects.all()
cache.set(cache_key, regions, 2592000) #(2592000sekund = 30 dni)
return regions
For seldom data changes I use signals:
from django.core.cache import cache
from django.db.models import signals
def nuke_social_network_cache(self, instance, **kwargs):
cache_key = 'networks_for_%s' % (self.instance.user_id,)
cache.delete(cache_key)
signals.post_save.connect(nuke_social_network_cache, sender=SocialNetworkProfile)
signals.post_delete.connect(nuke_social_network_cache, sender=SocialNetworkProfile)
Is it correct way?
I have installed django-memcached-0.1.2, which show me:
Memcached Server Stats
Server Keys Hits Gets Hit_Rate Traffic_In Traffic_Out Usage Uptime
127.0.0.1 15 220 276 79% 83.1 KB 364.1 KB 18.4 KB 22:21:25
Can somebody explain what columns means?
And the last question. I have templates where I am getting many records from a few table (relationships). So in my view I get records from one table and in templates show it and related info from others. Generating page last a few seconds for very small table (<100 records). Is it some easy way to cache queries from templates or all page? Have I to do some big structure in my view (with all related tables), cache it and send to template? It is some more easy way?