問題描述
我正在使用 PDO 連接到我想要實現(xiàn) memcached 的系統(tǒng)中的數(shù)據(jù)庫.
I'm using PDO for connect to the database in a system where I want implement memcached.
我不知道什么鍵用于緩存結(jié)果,因為我無法使用 PDO 獲取最終查詢的字符串(因為準(zhǔn)備好的語句).
I don't know what keys use for caching the results because I can't get the string of the final query with PDO (because the prepared statements).
解決這個問題有什么好主意嗎?
Any good idea for resolve this?
提前致謝.
推薦答案
如果你只是想直接根據(jù)查詢字符串緩存查詢結(jié)果,Mysql 的查詢緩存已經(jīng)為你完成了.不要重新發(fā)明輪子.一個潛在的區(qū)別是 Mysql 的查詢緩存被主動失效,因此永遠不會返回陳舊的(過時的、不正確的)數(shù)據(jù);根據(jù)您處理失效的方式,您的策略可能會進一步減少數(shù)據(jù)庫負載,但代價是定期提供過時的過時數(shù)據(jù).
If you're just going to cache query results directly based on query string, Mysql's query cache already does this for you. Don't reinvent the wheel. The one potential difference is Mysql's query cache is aggressively invalidated so that stale (out of date, incorrect) data is never returned; depending on how you handle invalidation, your strategy may further reduce database load, but at the cost of serving stale, out of date data on a regular basis.
此外,當(dāng)更新發(fā)生時,你真的不能有選擇地使你的各種緩存鍵過期(你怎么知道當(dāng)插入/更新運行時哪些查詢字符串應(yīng)該過期?);因此,您只需設(shè)置一個較短的到期時間(可能以秒為單位),以最大限度地減少您提供過時數(shù)據(jù)的時間.這可能意味著低緩存命中率.最后,你描述的緩存策略實現(xiàn)起來很簡單,但是效果不是很好.
Additionally, you won't really be able to selectively expire your various cache keys when updates happen (how would you know which query strings should be expired when an insert/update runs?); as a result you'll just have to set a short expiration time (probably in seconds), to minimize the amount of time you're serving stale data. This will probably mean a low cache hit rate. In the end, the caching strategy you describe is simple to implement, but it's not very effective.
請務(wù)必閱讀通用設(shè)計方法"部分memecached 常見問題解答.一個好的緩存策略會在更新發(fā)生時立即刪除/替換緩存的數(shù)據(jù)——這允許您將數(shù)據(jù)緩存數(shù)小時/天/周,同時永遠不會向用戶提供過時的數(shù)據(jù).
Make sure to read the "Generic Design Approaches" section of the memecached FAQ. A good caching strategy deletes/replaces cached data immediately when updates occur -- this allows you to cache data for hours/days/weeks, and simultaneously never serve out of date data to users.
這篇關(guān)于如何使用 PDO 和 memcached 設(shè)計緩存系統(tǒng)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!