我想第一次使用memcached,但是當涉及到使用動態變量的查詢時,我有幾個問題。在Coldfusion中使用Memcached
這是迄今爲止我的代碼示例。我有一切工作,但如果你看到我的代碼有任何錯誤,請讓我知道。
<cfoutput>
<cfset variables.memcachedFactory = createObject("component","memcachedfactory").init("127.0.0.1:11211")>
<cfset variables.memcached = variables.memcachedFactory.getmemcached()>
<cfset email = "sbcglobal"> //Pretend that this is a list with multiple choices for the user to choose from.
//The query below would have multiple outputs based on the selection of the user above
<cfquery name="test" datasource="#ds#">
select top 10 * from contact where email like '%#email#%'
</cfquery>
//Setting the key
<cfset set100 = variables.memcached.set(key="queryTest2",value=test,expiry="5000")>
//Getting the key
<Cfset test6 = variables.memcached.get("queryTest2")>
<br><br>this is the query test - did we retrieve it correctly? - <br />
<Cfdump var="#Test6#"><br />
</cfoutput>
現在,我必須爲每個可能的查詢結果設置一個鍵嗎? (在我的情況下,這將是幾百個鍵來覆蓋所有可能的結果) 我的理解是,memcached在第一個請求之後將值保存到一個鍵,然後在沒有db調用的情況下獲得輸出。 如果我在我的情況下使用memcached,它會浪費代碼,因爲每個查詢都會根據用戶的選擇而有所不同?
此外,如何在輸出更改的情況下更新緩存中的值?這是我必須手動做的事嗎?我的數據庫每天獲得100個交易,上午10點的select * from contacts
在下午5點將不會有相同的輸出
由於您詢問您是否犯過錯誤,因此沒有order by子句的頂級x查詢可能會產生錯誤的結果。 –
更新緩存中的值的一種方法是在執行SQL更新的同時執行更新。或者,您可以在緩存項目上設置更短的到期時間,具體取決於對陳舊數據的容忍程度。 –