我正在尝试采用缓存类在我的网页上使用.
简单的逻辑是这样的:
尝试从缓存中获取数据:
>如果没有:运行查询并将其设置为缓存.
>如果有:显示来自缓存的数据.
我的代码
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
query("SELECT" *="" from="" members");="" while($r="$query->fetch(PDO::FETCH_ASSOC))" {="" echo="" "$r[id]";="" lets="" cache="" query="" above.="" $cache->set("cachethis",$r,10);="" }="" $time_end="microtime(true);" $time="($time_end" -="" $time_start);="" "
我的输出和问题
我有两排.
row1
row2
row2
我试过的
我尝试使用fetchAll()函数而不是fetch()但这次我收到通知:未定义索引:id错误.
所以我被困住了,需要你的帮助.
最佳答案
如果一次为所有记录,请使用fetchAll
$r = $cache->get("cachethis");
if(!is_array($r)) {
// not in cache,from db
$query = $handler->query("SELECT * FROM members");
$r = $query->fetchAll(PDO::FETCH_ASSOC);
$cache->set("cachethis",10);
}
foreach($r as $v) echo $v['id']."