如何使用 PDO 和 memcached 设计缓存系统?

How i can design a cache system using PDO and memcached?(如何使用 PDO 和 memcached 设计缓存系统?)
本文介绍了如何使用 PDO 和 memcached 设计缓存系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)