PHP 中的查询缓存以提高 mySQL 性能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5086506/
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 16:45:33  来源:igfitidea点击:

Query Caching in PHP For mySQL Performance

phpmysqlcaching

提问by Arda

I'm looking for a system to cache an already coded project (with PHP) which has features like registration and login system etc.

我正在寻找一个系统来缓存一个已经编码的项目(使用 PHP),它具有注册和登录系统等功能。

I have searched for some caching solutions, but I read that the logging in and posting system fails if I use such features.

我搜索了一些缓存解决方案,但我读到如果我使用这些功能,登录和发布系统就会失败。

What I actually need is to store results of some spesific DB queries, if there's a cache, call that results, if not generate a new cache, and re-cache them in each x minutes. (results can be stored in txt. etc).

我真正需要的是存储一些特定数据库查询的结果,如果有缓存,则调用该结果,如果没有生成新缓存,并在每 x 分钟重新缓存它们。(结果可以存储在txt等中)。

How can I do that?

我怎样才能做到这一点?

By the way, setting query_cache_type to 1 don't work. I'm looking for alternative solutions.

顺便说一句,将 query_cache_type 设置为 1 不起作用。我正在寻找替代解决方案。

Thanks

谢谢

回答by shasi kanth

Basically you need to cache the query:

基本上你需要缓存查询:

 # After: [with memcache]
    $rSlowQuery = mysql_query_cache($sql);
    # $rSlowQuery is an array
    $rows = count($rSlowQuery);
    for ($i=0;$i<$rows;$i++) { }

回答by Ken Le

Reduce Database CallsCheck out phpFastCachethat support WinCache, MemCache, Files, X-Cache, APC Cache. It's simple for beginners

减少数据库调用查看phpFastCache的支持WINCACHE,内存缓存,文件,X-Cache功能,APC缓存。对于初学者来说很简单

PHP Caching Class For Database : Your website have 10,000 visitors who are online, and your dynamic page have to send 10,000 same queries to database on every page load. With phpFastCache, your page only send 1 query to DB, and use the cache to serve 9,999 other visitors.

PHP 数据库缓存类:您的网站有 10,000 名在线访问者,并且您的动态页面必须在每个页面加载时向数据库发送 10,000 个相同的查询。使用 phpFastCache,您的页面只向 DB 发送 1 个查询,并使用缓存为 9,999 个其他访问者提供服务。

<?php
    // In your config file
    include("php_fast_cache.php");
    // This is Optional Config only. You can skip these lines.
    // phpFastCache support "apc", "memcache", "memcached", "wincache" ,"files", "pdo", "mpdo" and "xcache"
    // You don't need to change your code when you change your caching system. Or simple keep it auto
    phpFastCache::$storage = "auto";
    // End Optionals

    // In your Class, Functions, PHP Pages
    // try to get from Cache first.
    $products = phpFastCache::get("products_page");

    if($products == null) {
        $products = YOUR DB QUERIES || GET_PRODUCTS_FUNCTION;
        // set products in to cache in 600 seconds = 10 minutes
        phpFastCache::set("products_page",$products,600);
    }

    foreach($products as $product) {
        // Output Your Contents HERE
    }
?>

回答by Ian N.

I recently ran into an similar issue, and here's how I resolved it...

我最近遇到了类似的问题,这是我解决它的方法......



Step 1:Add a .user.inifile to your project root (if you don't already have one), and the following directives to the file to enable the mysqlnd_qcplugin and set a TTL for the cache:

第 1 步:将一个.user.ini文件添加到您的项目根目录(如果您还没有),并将以下指令添加到文件中以启用mysqlnd_qc插件并为缓存设置 TTL:

; Enables MySQL query caching by PHP
mysqlnd_qc.enable_qc = 1

; Set a default TTL for the caching in seconds
mysqlnd_qc.ttl = 30

Step 2:Add a SQL hint before any query you want cached by mysqlnd_qc.

第 2 步:在要由 mysqlnd_qc 缓存的任何查询之前添加 SQL 提示。

Here's the hint that must be added to the query:

这是必须添加到查询中的提示:

"/*" . MYSQLND_QC_ENABLE_SWITCH . "*/" .

Here's an example of use:

下面是一个使用示例:

$res = $mysqli->query("/*" . MYSQLND_QC_ENABLE_SWITCH . "*/" . "SELECT id FROM test WHERE id = 1");


That should do it! You could also cache all queries using the mysqlnd_qc.cache_no_table = 1directive.

应该这样做!您还可以使用该mysqlnd_qc.cache_no_table = 1指令缓存所有查询。

All of this comes from PHP's documentation, here: http://php.net/manual/en/mysqlnd-qc.quickstart.caching.php

所有这些都来自 PHP 的文档,在这里:http: //php.net/manual/en/mysqlnd-qc.quickstart.caching.php

I hope that helps!

我希望这有帮助!

回答by m2ok

You can try this :

你可以试试这个:

$mysqli -> query("/*qc=on*/ SELECT * FROM table");