php MySQL 的 memcache 是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10135391/
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
How does memcache with MySQL work?
提问by Kevin Rave
I am trying to understand (and probably deploy) memcached in our env.
我正在尝试了解(并可能部署)我们环境中的 memcached。
We have 4 web servers on loadbalancer running a big web app developed in PHP. We are already using APC. I want to see how memcached works? At least, may be I don't understand how caching works.
我们在负载均衡器上有 4 个 web 服务器,运行一个用 PHP 开发的大型 web 应用程序。我们已经在使用 APC。我想看看 memcached 是如何工作的?至少,可能我不明白缓存是如何工作的。
We have some complex dynamic queries that combine several tables to pull data. Each time, the data is going to be from different client databases and data keeps changing. From my understanding, if some data is stored in cache, and if the request is same next time, the same data is returned. (Or I may be completely wrong here).
我们有一些复杂的动态查询,它们结合了几个表来提取数据。每次,数据都将来自不同的客户端数据库,并且数据不断变化。根据我的理解,如果缓存中存储了一些数据,如果下次请求相同,则返回相同的数据。(或者我在这里可能完全错了)。
How does this whole memcache (or for that matter, any caching stuff works)?
整个内存缓存是如何工作的(或者就此而言,任何缓存的东西都是如何工作的)?
回答by Mike Purcell
Cache, in general, is a very fast key/value storage engine where you can store values (usually serialized) by a predetermined key, so you can retrieve the stored values by the same key.
缓存,一般来说,是一个非常快的键/值存储引擎,您可以在其中按预定键存储值(通常是序列化的),因此您可以通过相同的键检索存储的值。
In relation to MySQL, you would write your application code in such a way, that you would check for the presence of data in cache, before issuing a request to the database. If a match was found (matching key exists), you would then have access to the data associated to the key. The goal is to not issue a request to the more costly database if it can be avoided.
对于 MySQL,您可以这样编写应用程序代码,即在向数据库发出请求之前检查缓存中是否存在数据。如果找到匹配项(匹配键存在),您就可以访问与该键关联的数据。如果可以避免,目标是不向成本更高的数据库发出请求。
An example (demonstrative only):
示例(仅用于演示):
$cache = new Memcached();
$cache->addServer('servername', 11211);
$myCacheKey = 'my_cache_key';
$row = $cache->get($myCacheKey);
if (!$row) {
// Issue painful query to mysql
$sql = "SELECT * FROM table WHERE id = :id";
$dbo->prepare($sql);
$stmt->bindValue(':id', $someId, PDO::PARAM_INT);
$row = $stmt->fetch(PDO::FETCH_OBJ);
$cache->set($myCacheKey, serialize($row));
}
// Now I have access to $row, where I can do what I need to
// And for subsequent calls, the data will be pulled from cache and skip
// the query altogether
var_dump(unserialize($row));
Check out PHP docs on memcachedfor more info, there are some good examples and comments.
查看memcached上的 PHP 文档以获取更多信息,其中有一些很好的示例和注释。
回答by linuxeasy
There are several examples on how memcache works. Hereis one of the links.
有几个关于 memcache 工作原理的示例。这是链接之一。
Secondly, Memcache can work with or without MySQL.
其次,Memcache 可以使用或不使用 MySQL。
It caches your objects which are in PHP, now whether it comes from MySQL, or anywhere else, if its an PHP Object, it can be stored in MemCache.
它缓存你在 PHP 中的对象,现在无论它来自 MySQL,还是其他任何地方,如果它是 PHP 对象,它可以存储在 MemCache 中。
APC gives you some more functionality than Memcache. Other than storing/caching PHP objects, it also caches PHP-executable-machine-readable-opcodes so that your PHP files won't go through the processes of loading in memory-> Being Comiled, rather, it directly runs the already compiled opcode from the memory.
APC 为您提供了比 Memcache 更多的功能。除了存储/缓存 PHP 对象外,它还缓存 PHP-executable-machine-readable-opcodes,这样你的 PHP 文件就不会经历加载到内存中的过程 -> Being Comiled,而是直接运行已经编译好的操作码从记忆中。
回答by Alfred
If your data keeps changing(between requests) then caching is futile, because that data is going to be stale. But most of the times(I bet even in your cache) multiple requests to database result in same data set in which case a cache(in memory) is very useful.
如果您的数据不断变化(在请求之间),那么缓存是徒劳的,因为该数据将是陈旧的。但是大多数时候(我打赌甚至在你的缓存中)对数据库的多个请求会导致相同的数据集,在这种情况下缓存(在内存中)非常有用。
P.S: I did a quick google search and found this video about memcached which has rather good quality => http://www.bestechvideos.com/2009/03/21/railslab-scaling-rails-episode-8-memcached. The only problem could be that it talks about Ruby On Rails(which I also don't use that much, but is very easy to understand). Hopefully it is going to help you grasp the concept a little better.
PS:我在谷歌上进行了快速搜索,发现这个关于 memcached 的视频质量相当好 => http://www.bestechvideos.com/2009/03/21/railslab-scaling-rails-episode-8-memcached。唯一的问题可能是它谈到了 Ruby On Rails(我也不经常使用它,但很容易理解)。希望它能帮助你更好地理解这个概念。

