2015-07-13 33 views

回答

1

用於實現任何高速緩存的邏輯如下:

  1. 檢查是否通過獲取在緩存中存在對象它。
  2. 如果它不存在,計算對象(或生成它)並將其放入緩存中。
  3. 返回對象。

Django爲緩存提供了一個簡單的字典式API。一旦你有correctly configured the cache,你可以使用簡單的緩存API:

from django.core.cache import cache 

def get(request): 
    value = cache.get('somekey') 
    if not value: 
     # The value in the cache for the key 'somekey' has expired 
     # or doesn't exist, so we generate the value 
     value = 42 
     cache.set('somekey', value) 

有很多可以在Django緩存,請務必閱讀the documentation其中介紹瞭如何在模板中使用緩存,如何緩存整個視圖輸出等等。

相關問題