何时使用内存缓存

发布于 2024-10-26 11:32:05 字数 2122 浏览 1 评论 0原文

我大概了解了 memcached 的工作原理。您可以使用它存储数据块来提高站点性能。当你想检索一些数据时,你首先检查它是否在memcached中,如果是,那么你检索它,否则你检查你的数据库/文件系统等。

我只是不知道如何/何时使用它?什么会是一个好机会呢?

我有下表:

作者:

id 用户名 电子邮件 密码 盐 email_salt email_verified ip_地址

作者线程:

线程 ID、作者 ID

主题:

ID、标题、内容、已创建

标签:

ID、姓名

Thread_tags:

tad_id,线程_id

我想选择最新的 30 个主题、它们的作者和所有标签。这是我使用的 SQL 语句:

       SELECT thread.title, thread.id as thread_id,
       thread.content, author.username, author.id as author_id,
       GROUP_CONCAT(DISTINCT tag.name ORDER BY tag.name DESC SEPARATOR ',') AS tags
       FROM thread 
       JOIN thread_tags ON thread.id = thread_tags.thread_id
       JOIN tag ON thread_tags.tag_id = tag.id

       JOIN author_threads ON thread.id = author_threads.thread_id
       JOIN author ON author_threads.author_id = author.id

       GROUP BY thread.id DESC
       LIMIT 0, 30

这是我使用的 PHP:

function get_latest_threads($link, $start)
{

   $start = minimal_int($start);

   $threads = sql_select($link, "SELECT thread.title, thread.id as thread_id,
                                 thread.content, author.username, author.id as author_id,
                                 GROUP_CONCAT(DISTINCT tag.name ORDER BY tag.name DESC SEPARATOR ',') AS tags
                                 FROM thread 
                                 JOIN thread_tags ON thread.id = thread_tags.thread_id
                                 JOIN tag ON thread_tags.tag_id = tag.id

                                 JOIN author_threads ON thread.id = author_threads.thread_id
                                 JOIN author ON author_threads.author_id = author.id

                                 GROUP BY thread.id DESC
                                 LIMIT $start, 30"  # I only want to retrieve 30 records each time
                        );

   return $threads;

}

memcached 将在哪里/如何使用?

I sorta get how memcached works. You use it store chunks of data to improve site performance. When you want to retrieve some data you check if its in memcached first, if it is, then you retrieve it, otherwise you check your database/filesystem etc.

I just don't know how/when to use it? What would be a good opportunity?

I have the following tables:

Author:

id username email password salt
email_salt email_verified
ip_address

Author_threads:

thread_id, author_id

Thread:

id, title, content, created

Tag:

id, name

Thread_tags:

tad_id, thread_id

I want to select the latest 30 threads, their author and all their tags. This is the SQL statement I use:

       SELECT thread.title, thread.id as thread_id,
       thread.content, author.username, author.id as author_id,
       GROUP_CONCAT(DISTINCT tag.name ORDER BY tag.name DESC SEPARATOR ',') AS tags
       FROM thread 
       JOIN thread_tags ON thread.id = thread_tags.thread_id
       JOIN tag ON thread_tags.tag_id = tag.id

       JOIN author_threads ON thread.id = author_threads.thread_id
       JOIN author ON author_threads.author_id = author.id

       GROUP BY thread.id DESC
       LIMIT 0, 30

This is the PHP that I use:

function get_latest_threads($link, $start)
{

   $start = minimal_int($start);

   $threads = sql_select($link, "SELECT thread.title, thread.id as thread_id,
                                 thread.content, author.username, author.id as author_id,
                                 GROUP_CONCAT(DISTINCT tag.name ORDER BY tag.name DESC SEPARATOR ',') AS tags
                                 FROM thread 
                                 JOIN thread_tags ON thread.id = thread_tags.thread_id
                                 JOIN tag ON thread_tags.tag_id = tag.id

                                 JOIN author_threads ON thread.id = author_threads.thread_id
                                 JOIN author ON author_threads.author_id = author.id

                                 GROUP BY thread.id DESC
                                 LIMIT $start, 30"  # I only want to retrieve 30 records each time
                        );

   return $threads;

}

Where/how would memcached be uses here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

记忆里有你的影子 2024-11-02 11:32:05

我只是不知道如何/何时使用它?

仅当您证明添加缓存是可以获得的最佳性能提升时才使用它。将数据缓存或输出缓存添加到以前没有任何类型缓存的复杂应用程序中可以发现大量微妙的错误和奇怪的行为。

首先使用代码分析器。找出您的代码在哪里存在真正性能问题。找出瓶颈并解决它们。如果该修复涉及缓存,那就这样吧,但首先收集证据

I just don't know how/when to use it?

Use it only once you have proved that adding caching is the best performance boost you can get. Adding data caching or output caching to a complex application that previously did not have any sort of caching can uncover a large number of subtle bugs and bizarre behavior.

Use a code profiler first. Find out where your code is having real performance problems. Identify the bottlenecks and fix them. If that fix involves caching, so be it, but gather evidence first.

亢潮 2024-11-02 11:32:05

没有关于何时使用它的既定指南。您必须找出哪些数据库查询最频繁和/或成本最高,并缓存它们。在你的情况下,我可能会缓存该函数调用的结果。

我会感到困惑的地方(就像你可能一样)是创建新线程时要做什么。每次有人创建线程时,您的查询都会给出不同的结果。因此,在这种情况下,当有人创建线程时,您应该做的是更新数据库,然后通过踢出最旧的线程并添加新线程来调整缓存中的结果集。您甚至不需要从缓存中重新加载最新的 30 个线程,因为它会被更新。

There's no set guidelines on when to use it. You have to figure out which database queries are most frequent and / or most costly, and cache those. In your case, I would probably cache the result of that function call.

Where I would get confused (as you may be) is what do do as new threads are created. Your query is going to give different results every time someone creates a thread. So what you should do in that case, when someone creates a thread, is to update the db, and then adjust your result set in cache by kicking out the oldest thread and adding the new one. You won't even need to reload the newest 30 threads from cache, since it will be updated.

冰雪梦之恋 2024-11-02 11:32:05

许多站点用户可能会请求最新线程的列表,因此使用 memcached 缓存整个 SQL 结果听起来是一个绝佳的机会。

memcached 的一个非常简单的方法是使用 SQL 查询作为键,并将它们各自的结果作为值 (即在此处查看本教程)。在开始进一步优化之前,您可能需要首先对所有高频数据库查询进行尝试并分析结果。

The list of latest threads will be probably be requested by a lot of your site users, so caching the entire SQL result using memcached sounds like a perfect opportunity.

A very simple approach to memcached is to use SQL queries as keys and their respective results as values (i.e. see this tutorial here). You might want to try that first on all high-frequency database queries and profile the result before you start optimizing any further.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文