php 使用redis作为mysql数据库的缓存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16268950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:49:10  来源:igfitidea点击:

Using redis as a cache for a mysql database

phpcachingredis

提问by Alexandre C. do Carmo

I need to create a solution using php, with a mysql database with lots of data. My program will have many requisitions, I think that if I work with cache and an OO database, I'll have a good result, but I don't have experience.

我需要使用 php 创建一个解决方案,其中包含一个包含大量数据的 mysql 数据库。我的程序会有很多请求,我认为如果我使用缓存和OO数据库,我会得到一个很好的结果,但我没有经验。

I think for example if I cache the information that is saved in mysql in a redis database, performance will be improved, but I don't know if this is a good idea, so I would like someone to help me to choose.

我想比如我把mysql中保存的信息缓存到redis数据库中,性能会有所提升,但是我不知道这是不是一个好主意,所以我希望有人帮我选择。

Sorry if my English is not very good, I'm from Brazil.

对不起,如果我的英语不是很好,我来自巴西。

回答by Krzysztof Bujniewicz

Yes, redis is good for that. But to get the gist, there are basically two approaches to caching. Depending on whether you use a framework (and which) or not, you may have first option available in standard or with use of a plug-in:

是的,redis 对此有好处。但要了解要点,基本上有两种缓存方法。根据您是否使用框架(和哪个),您可能有标准或使用插件的第一个选项:

  1. Cache database queries, that is - selected queries and their results will be kept in redis for quicker access for a given time or until clearing cache (useful after updating databse). In this case you can use built-in mysql query caching, it will be simpler than using additional key-value store, or you can override default database integration with your own class making use of cache (for example http://pythonhosted.org/johnny-cache/).
  2. Custom caching, that is creating your own structures to be kept in cache and periodically or manually refilling them with data fetched from the database. It is more flexible and potentially more powerful, because you can use built-in redis features such as lists or sorted sets, which make update overhead much smaller. It requires a bit more of coding, but it usually offers better results, since it is more customized. Good example is keeping top articles in form of redis list of ids, and then accessing serialized article(s) with given id as well from redis. You can keep that article unnormalized - ie. serialized object can contain user id as well as user name, so that you can keep the overhead of additional queries to a minimum.
  1. 缓存数据库查询,即 - 选定的查询及其结果将保存在 redis 中,以便在给定时间内更快地访问或直到清除缓存(更新数据库后有用)。在这种情况下,您可以使用内置的 mysql 查询缓存,这比使用其他键值存储更简单,或者您可以使用自己的类覆盖默认数据库集成,使用缓存(例如http://pythonhosted.org /约翰尼缓存/)。
  2. 自定义缓存,即创建您自己的结构以保存在缓存中,并使用从数据库获取的数据定期或手动重新填充它们。它更灵活,也可能更强大,因为您可以使用内置的 redis 功能,例如列表或排序集,从而使更新开销小得多。它需要更多的编码,但通常会提供更好的结果,因为它更加定制化。一个很好的例子是以redis id列表的形式保存顶级文章,然后从redis访问具有给定id的序列化文章。您可以使那篇文章保持非规范化 - 即。序列化对象可以包含用户 ID 和用户名,以便您可以将额外查询的开销保持在最低限度。

It is yours to decide which approach to take, I personally almost always go with approach number two. But, of course, everything depends on how much time you have, and what the application is supposed to do - you might as well start with mysql query caching and if the results are not good enough move to redis and custom caching.

由您决定采用哪种方法,我个人几乎总是采用第二种方法。但是,当然,一切都取决于您有多少时间,以及应用程序应该做什么 - 您不妨从 mysql 查询缓存开始,如果结果不够好,则转向 redis 和自定义缓存。