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

问题描述

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

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设计缓存系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 07:18