开发者

Using Memcached in Python/Django - questions

开发者 https://www.devze.com 2022-12-31 05:41 出处:网络
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

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.mod开发者_如何学JAVAels 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?


For second question (about django-memcached-0.1.2 status):

http://effbot.org/zone/django-memcached-view.htm#more-statistics

http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt

0

精彩评论

暂无评论...
验证码 换一张
取 消