I am trying to use a cache class to use on my web page.
The simple logic is this:
Try to get data from the cache:
- If not: run the request and set it as a cache.
- If there is: show data from the cache.
my code
<?php // Require Library require_once("phpfastcache.php"); $cache = phpFastCache(); //lets get it from cache. $r = $cache->get("cachethis"); if($r == null) { echo " THIS TIME RUN WITHOUT CACHE <br> "; $time_start = microtime(true); $query = $handler->query("SELECT * FROM members"); while($r = $query->fetch(PDO::FETCH_ASSOC)) { //echo echo "$r[id]"; //lets cache query above. $cache->set("cachethis", $r, 10); } $time_end = microtime(true); $time = ($time_end - $time_start); echo "<p>done in " . $time . " seconds</p>"; } //if ($r != null) else { echo "RUN WITH CACHE. YES.<br> "; $time_start = microtime(true); //echo from cache.. echo "$r[id]"; $time_end = microtime(true); $time = ($time_end - $time_start); echo "<p>cache done in " . $time . " seconds</p>"; } ?>
my conclusion and problem
I have two lines.
When I retrieve them from the query, my code outputs both of them. Which is nice.
row1 row2
But when the page displays it from the cache, it only displays the last line.
row2
what i tried
I tried using the fetchAll() function instead of fetch() , but this time I get a notification: Undefined index: id error.
So I'm stuck and need your help.
source share