缓存 MySQL 结果集
我需要一个工作代码来将 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 中)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能并不真的想这样做!
向您的应用程序添加缓存层应该是您最后要做的事情之一。缓存可以发现代码中大量的错误,无论是微妙的还是明显的。
如果您想提高代码的性能,则应该执行代码分析并消除真实瓶颈而不是感知瓶颈。
尤其是在这种情况下。
您此处的示例查询很糟糕。甚至可怕。您从表中的每一行中获取
UID
列,但未能指定这些行的顺序,然后只实际获取第一行,无论它是哪一行。即使使用这个查询,甚至不考虑缓存它,也是危险的。 如果这是一个真实的查询,那么您的代码从一开始就损坏!从数据库返回的结果不保证按任何特定顺序排列。事实上,至少对于 MySQL,您实际上可以使用
ALTER TABLE ... ORDER BY
。如果您只想要一行,则应该使用ORDER BY
子句和LIMIT
子句,而不是选择所有内容,然后只获取一行。我将假设您已经过度简化了查询作为示例,并会通过两个或三个APC 您需要了解的功能。您应该阅读所有其他函数的手册页,以了解 APC 的工作原理。
让我们看看你的代码。
在此代码中,您正在准备查询
SELECT UID FROM Users
,将结果集的第一列绑定到 PHP 变量$UID
,获取单行,然后关闭语句句柄(丢弃所有其他结果)。$UID
包含您要缓存的单个值。要在 APC 中缓存此值,您可以使用apc_store
:
$key
是缓存键名称。当然,如果不只在需要时查询数据库,将其放入缓存中是愚蠢的。我们可以使用
apc_fetch
来首先查找缓存的值。此代码将始终在查询数据库之前尝试将
$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 anORDER BY
clause and aLIMIT
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.
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 useapc_store
:$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.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 thatget_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.
这是一个工作代码:)
Here is a working code :)