2015-06-23 29 views
1

查詢迴應是不是從ActiveRecord我怎樣才能得到與外部查詢(不與ActiveRecord)的API緩存

和響應是JSON格式。

我怎麼會緩存返回結果

def get_drug_usage_history_by_symptoms 
     pipeline_work = [ 
     {"$unwind" => "$records"}, 
     {'$match' => {'records.items' => {'$in' => items} }}, 
     {'$limit' => 8 } # FOR DEVELOPMENT USE 
    ] 
     cur = db[collection].find.aggregate(pipeline_work).allow_disk_use(true) 
    respond_to do |format| 
     format.json { render json: cur.to_a } 
    end 
    end 

回答

1

Rails提供,我已經找到了緩存API調用的結果是非常有用的,等等通用的低級別的數據緩存。瞭解它herehere。基本上這是如何工作的:

Rails.cache.fetch("unique_key_for_this_data", expires_in: 30.minutes) do 
    # make the API call 
    # Whatever value this block returns, will be stored in the cache 
    # If the value is already cached (and not expired), that value 
    # will be provided and this block won't even execute 
end 
相關問題