php - Caching a MySQL Resultset -


i need working code storing mysql result set in apc cache!
searched on google & , did not find any!
hope share working code.

example:

$stmt=mysqli_prepare($con,"select uid 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 calendar 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 outcoming sql result in apc?

i want know code cache in apc (not in mysql)

you don't want this!

adding caching layer application should 1 of last things do. caching can uncover tremendous number of bugs in code, both subtle , obvious.

if trying improve performance of code, should performing code profiling , eliminating real bottlenecks rather perceived ones.

especially in case.

your example query here bad. horrible, even. you're fetching uid column every row in table, failing specify order rows, , fetching first row, whichever might be.

even using query, not think caching it, dangerous. if it's real query code broken begin with! results coming database not guaranteed in specific order. indeed, @ least mysql, can reset on-disk data ordering using alter table ... order by. if want 1 row, should using order by clause , limit clause, not selecting everything , fetching 1 row.

i'm going operate under assumption you've oversimplified query example, , humor quick primer on 2 or 3 apc functions need know. should read manual pages all of other functions gain understanding of how apc works.

let's @ code.

$stmt=mysqli_prepare($con,"select uid users"); mysqli_stmt_execute($stmt); mysqli_stmt_bind_result($stmt, $uid); mysqli_stmt_fetch($stmt); mysqli_stmt_close($stmt); 

in code, preparing query select uid users, binding first column of result set php variable $uid, fetching single row, closing statement handle (discarding other results).

$uid contains single value want cache. cache value in apc, can use apc_store:

apc_store($key, $uid); 

$key cache key name.

of course, putting in cache silly without, say, querying database if need to. can use apc_fetch 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 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 try , pull $key out of cache before querying database, more wanted accomplish. note i've intentionally left off cache expire time here.

you note 1 value got cached here. if insist on using bound result variables, you're either going need manually build array , cache array instead. highly suggest not using bound variables. @ using mysql_stmt_get_result fetch result set, can grab array from. watch out, manual says get_result returns boolean, yet has return result set object in example code. ymmv. may suggest pdo future projects?

no matter database interface being used, can not cache actual statement handle or result set objects, data return.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -