缓存 MySQL 结果集

发布于 2024-11-03 20:41:23 字数 654 浏览 1 评论 0 原文

我需要一个工作代码来将 MySQL 结果集存储在 APC 缓存中!
我在 Google 上搜索所以并没有找到任何!
希望有人能分享一个工作代码。

示例:

$stmt=mysqli_prepare($con,"SELECT UID FROM Users");
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $UID);
mysqli_stmt_fetch($stmt);
mysqli_stmt_close($stmt);

示例2:

$stmt=mysqli_prepare($con,"SELECT Events FROM Calendar where UID=?");
mysqli_stmt_bind_result($stmt, "i",$UID);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $events);
while(mysqli_stmt_fetch($stmt))
{
   ...
}
mysqli_stmt_close($stmt);

如何将输出的SQL 结果存储在APC 中?

我想知道在 APC 中缓存它的代码(而不是在 MySQL 中)

I need a working code for storing a MySQL result set in APC cache!
i searched on Google & SO and did not find any!
Hope someone will share a working code.

Example:

$stmt=mysqli_prepare($con,"SELECT UID FROM Users");
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $UID);
mysqli_stmt_fetch($stmt);
mysqli_stmt_close($stmt);

Example 2:

$stmt=mysqli_prepare($con,"SELECT Events FROM Calendar where UID=?");
mysqli_stmt_bind_result($stmt, "i",$UID);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $events);
while(mysqli_stmt_fetch($stmt))
{
   ...
}
mysqli_stmt_close($stmt);

How store the outcoming SQL result in APC?

i want to know the code to cache it in APC (not in MySQL)

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

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

发布评论

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

评论(2

⊕婉儿 2024-11-10 20:41:23

您可能并不真的想这样做!

向您的应用程序添加缓存层应该是您最后要做的事情之一。缓存可以发现代码中大量的错误,无论是微妙的还是明显的。

如果您想提高代码的性能,则应该执行代码分析并消除真实瓶颈而不是感知瓶颈。

尤其是在这种情况下。

您此处的示例查询很糟糕。甚至可怕。您从表中的每一行中获取UID列,但未能指定这些行的顺序,然后只实际获取第一行,无论它是哪一行。

即使使用这个查询,甚至不考虑缓存它,也是危险的。 如果这是一个真实的查询,那么您的代码从一开始就损坏从数据库返回的结果保证按任何特定顺序排列。事实上,至少对于 MySQL,您实际上可以使用ALTER TABLE ... ORDER BY。如果您只想要一行,则应该使用 ORDER BY 子句和 LIMIT 子句,而不是选择所有内容,然后只获取一行。

我将假设您已经过度简化了查询作为示例,并会通过两个或三个APC 您需要了解的功能。您应该阅读所有其他函数的手册页,以了解 APC 的工作原理。

让我们看看你的代码。

$stmt=mysqli_prepare($con,"SELECT UID FROM Users");
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $UID);
mysqli_stmt_fetch($stmt);
mysqli_stmt_close($stmt);

在此代码中,您正在准备查询 SELECT UID FROM Users,将结果集的第一列绑定到 PHP 变量 $UID,获取单行,然后关闭语句句柄(丢弃所有其他结果)。

$UID 包含您要缓存的单个值。要在 APC 中缓存此值,您可以使用 apc_store

apc_store($key, $UID);

$key 是缓存键名称。

当然,如果不只在需要时查询数据库,将其放入缓存中是愚蠢的。我们可以使用 apc_fetch 来首先查找缓存的值。

$value_in_cache = false;
$value = apc_fetch($key, $value_in_cache);
if(!$value_in_cache) {
    $value = null;
    $stmt = mysqli_prepare($con,"SELECT UID FROM Users");
    mysqli_stmt_execute($stmt);
    mysqli_stmt_bind_result($stmt, $value);
    mysqli_stmt_fetch($stmt);
    mysqli_stmt_close($stmt);
    apc_store($key, $value);
}

此代码将始终在查询数据库之前尝试将 $key 从缓存中拉出,这更有可能是您想要完成的任务。请注意,我在这里故意省略了缓存过期时间。

您会注意到,显然这里只缓存了一个值。如果您坚持使用绑定结果变量,那么您要么需要手动构建一个数组并缓存该数组。我强烈建议不要使用绑定变量。查看使用 mysql_stmt_get_result 获取 结果集,然后您就可以获取它一个数组来自. 注意,手册说 get_result 返回布尔值,但示例代码中却返回结果集对象。 YMMV。我可以为您未来的项目建议 PDO 吗?

无论使用什么数据库接口,您都无法缓存实际的语句句柄或结果集对象,只能缓存它们返回的数据。

You probably don't really want to do this!

Adding a caching layer to your application should be one of the last things you do. Caching can uncover a tremendous number of bugs in your code, both subtle and obvious.

If you are trying to improve the performance of your code, you should be performing code profiling and eliminating real bottlenecks rather than perceived ones.

Especially in this case.

Your example query here is bad. Horrible, even. You're fetching the UID column from every row in the table, failing to specify an order for those rows, and then only actually fetching the first row, whichever it might be.

Even using this query, not to even think about caching it, is dangerous. If it's a real query then your code is broken to begin with! Results coming back from the database are not guaranteed to be in any specific order. Indeed, at least with MySQL, you can actually reset the on-disk data ordering using ALTER TABLE ... ORDER BY. If you only want one row, you should be using an ORDER BY clause and a LIMIT clause, not selecting everything and then only fetching one row.

I'm going to operate under the assumption that you've oversimplified the query as an example, and will humor you with a quick primer on the two or three APC functions that you need to know. You should read the manual pages for all of the other functions to gain an understanding of how APC works.

Let's look at your code.

$stmt=mysqli_prepare($con,"SELECT UID FROM Users");
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $UID);
mysqli_stmt_fetch($stmt);
mysqli_stmt_close($stmt);

In this code, you are preparing the query SELECT UID FROM Users, binding the first column of the result set into the PHP variable $UID, fetching a single row, then closing the statement handle (discarding all other results).

$UID contains the single value you want to cache. To cache this value in APC, you can use apc_store:

apc_store($key, $UID);

$key is the cache key name.

Of course, putting it in the cache is silly without, say, only querying the database if you need to. We can use apc_fetch to look for the cached value first.

$value_in_cache = false;
$value = apc_fetch($key, $value_in_cache);
if(!$value_in_cache) {
    $value = null;
    $stmt = mysqli_prepare($con,"SELECT UID FROM Users");
    mysqli_stmt_execute($stmt);
    mysqli_stmt_bind_result($stmt, $value);
    mysqli_stmt_fetch($stmt);
    mysqli_stmt_close($stmt);
    apc_store($key, $value);
}

This code will always try and pull $key out of the cache before querying the database, which is more likely what you wanted to accomplish. Note that I've intentionally left off the cache expire time here.

You will note that obviously only one value got cached here. If you insist on using bound result variables, then you're either going to need to manually build an array and cache that array instead. I highly suggest not using bound variables. Look at using mysql_stmt_get_result to fetch a result set, which you can then grab an array from. Watch out, the manual says that get_result returns boolean, yet has it return a result set object in the example code. YMMV. May I suggest PDO for your future projects?

No matter what database interface is being used, you can not cache the actual statement handle or result set objects, only the data that they return.

筱武穆 2024-11-10 20:41:23

这是一个工作代码:)

<?php
$dba_host='localhost';
$dba_name='root';
$dba_pass='';
$dba_db='DB';

$con=mysqli_connect($dba_host,$dba_name,$dba_pass,$dba_db) or die('Connection Refused !');

$stmt=mysqli_prepare($con,"SELECT UID FROM Main");
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $value);
while(mysqli_stmt_fetch($stmt))
 $result[] = $value;
mysqli_stmt_close($stmt);
apc_store('uid', $result);


$value_in_cache = false;
$value = apc_fetch('uid', $value_in_cache);
if(!$value_in_cache) {
 echo 'Looking at DB';
$stmt=mysqli_prepare($con,"SELECT UID FROM Main");
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $value);
while(mysqli_stmt_fetch($stmt))
    $result[] = $value;
mysqli_stmt_close($stmt);
apc_store('uid', $result);
}
else
{
 echo 'Looking at Memory';
 print_r($result);
}

mysqli_close($con);
?>

Here is a working code :)

<?php
$dba_host='localhost';
$dba_name='root';
$dba_pass='';
$dba_db='DB';

$con=mysqli_connect($dba_host,$dba_name,$dba_pass,$dba_db) or die('Connection Refused !');

$stmt=mysqli_prepare($con,"SELECT UID FROM Main");
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $value);
while(mysqli_stmt_fetch($stmt))
 $result[] = $value;
mysqli_stmt_close($stmt);
apc_store('uid', $result);


$value_in_cache = false;
$value = apc_fetch('uid', $value_in_cache);
if(!$value_in_cache) {
 echo 'Looking at DB';
$stmt=mysqli_prepare($con,"SELECT UID FROM Main");
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $value);
while(mysqli_stmt_fetch($stmt))
    $result[] = $value;
mysqli_stmt_close($stmt);
apc_store('uid', $result);
}
else
{
 echo 'Looking at Memory';
 print_r($result);
}

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