如何使用 PDO 和 memcached 设计缓存系统? [英] How i can design a cache system using PDO and memcached?

查看:21
本文介绍了如何使用 PDO 和 memcached 设计缓存系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PDO 连接到我想要实现 memcached 的系统中的数据库.

I'm using PDO for connect to the database in a system where I want implement memcached.

我不知道什么键用于缓存结果,因为我无法使用 PDO 获取最终查询的字符串(因为准备好的语句).

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?

提前致谢.

推荐答案

如果你只是想直接根据查询字符串缓存查询结果,Mysql 的查询缓存已经为你完成了.不要重新发明轮子.一个潜在的区别是 Mysql 的查询缓存被主动失效,因此永远不会返回陈旧的(过时的、不正确的)数据;根据您处理失效的方式,您的策略可能会进一步减少数据库负载,但代价是定期提供过时的过时数据.

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.

此外,当更新发生时,你真的不能有选择地使你的各种缓存键过期(你怎么知道当插入/更新运行时哪些查询字符串应该过期?);因此,您只需设置一个较短的到期时间(可能以秒为单位),以最大限度地减少您提供过时数据的时间.这可能意味着低缓存命中率.最后,你描述的缓存策略实现起来很简单,但是效果不是很好.

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.

请务必阅读通用设计方法"部分memecached 常见问题解答.一个好的缓存策略会在更新发生时立即删除/替换缓存的数据——这允许您将数据缓存数小时/天/周,同时永远不会向用户提供过时的数据.

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.

这篇关于如何使用 PDO 和 memcached 设计缓存系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆