何时使用memcached

joh*_*sel 11 php memcached

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

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

我有以下表格:

作者:

id username email password salt email_salt email_verified ip_address

Author_threads:

thread_id,author_id

线:

id,title,content,created

标签:

id,名字

Thread_tags:

tad_id,thread_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
Run Code Online (Sandbox Code Playgroud)

这是我使用的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;

}
Run Code Online (Sandbox Code Playgroud)

memcached在哪里/如何使用?

Cha*_*les 9

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

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

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