2010-02-13 64 views
0

我有一組類別,我通過JSON在我的應用程序的平均使用過程中多次通過javascript訪問這些分類。如何根據respond_to類型返回memcached字符串?

所以在我的控制目前我有一個

@categories = Category.all 

respond_to do |format| 
    format.html # index.html.erb 
    format.json 

與格式什麼,我需要儘可能JSON適當index.json.erb文件一起。

現在我想在我加入

的index.json.erb文件添加一些memcached的功能,這使
<% cache "JSON_CATEGORIES_ALL" do -%> block around my output 

我的問題是如何得到我的控制器,以迴應時稱此緩存鍵一個JSON請求並正常動作,從其他調用中從數據庫中提取?

感謝

回答

1

您可以檢查請求的格式:

@categories = Category.all unless request.format == "application/json" and fragment_exists?("JSON_CATEGORIES_ALL") 

respond_to do |format| 
    format.html # @categories is available 
    format.json # no database call if your cache fragment already exists 
end 
+0

雅那是接近我一直在用......我想通了,爲什麼它不工作見下文。 – 2010-02-14 00:55:07

0

我想通了......的人誰在這個跌倒在這兒呢。

@categories = Category.all unless request.format == "application/json" and Rails.cache.exist?("views/JSON_CATEGORIES_ALL") 

注意:加入意見/到緩存關鍵!似乎rails會將此視爲緩存。

agregoire:謝謝你的

request.format == "application/json" 
相關問題