MySQL的记忆快取如何运作? [英] How does memcache with MySQL work?

查看:89
本文介绍了MySQL的记忆快取如何运作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图了解(并可能部署)我们环境中的内存缓存.

我们在loadbalancer上有4个Web服务器,运行着一个用PHP开发的大型Web应用程序.我们已经在使用APC. 我想看看memcached的工作原理吗?至少可能是我不了解缓存的工作原理.

我们有一些复杂的动态查询,这些查询结合了多个表来提取数据.每次,数据将来自不同的客户端数据库,并且数据不断变化.据我了解,如果某些数据存储在缓存中,并且下次请求相同,则返回相同的数据. (或者我可能在这里完全错了.)

整个内存缓存如何工作(或者就此而言,任何缓存的东西都起作用)?

解决方案

缓存通常是一种非常快速的键/值存储引擎,您可以在其中存储通过预定键存储的值(通常是序列化的),因此您可以检索通过相同的键存储值.

关于MySQL,您将以这种方式编写应用程序代码,以便在向数据库发出请求之前检查缓存中是否存在数据.如果找到匹配项(存在匹配密钥),则可以访问与该密钥关联的数据.目标是如果可以避免的话,不要向成本更高的数据库发出请求.

一个示例(仅用于说明):

$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));

memcached 上查看PHP文档,以获取更多信息,其中有一些很好的示例和注释.

I am trying to understand (and probably deploy) memcached in our env.

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.

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)?

解决方案

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.

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 memcached for more info, there are some good examples and comments.

这篇关于MySQL的记忆快取如何运作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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