2011-08-19 71 views

回答

1

看看這段代碼http://djangosnippets.org/snippets/936/。將視圖的路徑(url)傳遞給expire_page函數函數,每次您想要使緩存失效時。

+0

作爲網址,我應該通過反向作用的結果?我通過:expire_page(reverse('index_show_freelinks',kwargs = {'name':'partners'}))但緩存仍然有效。 – robos85

+0

您確定「reverse」正在返回您想要的網址嗎?也許可以試着對它進行硬編碼,看它是否有效。你如何測試緩存是否仍然有效? – Drekembe

3

這是一個Django代碼片段,我發現可能會有所幫助:

from django.core.cache import cache 
from django.http import HttpRequest 
from django.utils.cache import get_cache_key 

def expire_page(path): 
    request = HttpRequest() 
    request.path = path 
    key = get_cache_key(request) 
    if cache.has_key(key): 
     cache.delete(key) 

否則這太問題進入關於這個更詳細:Expire a view-cache in Django?