mysql如何用mongodb做缓存
function query($sql, $cache_expire = ''){
//开启缓存 读取
if($cache_expire > 0){
$mongo = new Mongo();
$cachedata = $mongo->pwashop2->cachedata;//集合句柄
$sql_key = md5($sql);
$select = array('sql_key' => $sql_key);//查询条件
$row = $cachedata->findOne($select);
if($row && isset($row['cache_time']) && $row['cache_time'] > time()){//读取缓存,有数据且未过期
$query = array('读取缓存!!', $row['query'], date("Y-m-d H:i:s", $row['cache_time']));
return $query;
}
}
$query = array(array('username' => 'jwage'), array('password' => date("Y-m-d H:i:s", time())));//数据
//开启缓存 设置
if($cache_expire > 0){
$cachedata->remove($select);//先移除旧数据
$cache_time = time() + $cache_expire; //缓存过期时间
$row = array(
'sql_key' => md5($sql),
'query' => $query,
'cache_time' => $cache_time);
$cachedata->insert($row);
return array('设置缓存!!', $query);
}
return array('未开启缓存!!', $query);
}
$sql = "select 1+1";
$query = query($sql, 30);
echo date("Y-m-d H:i:s", time()).'
';
print_r($query);
exit;
希望与广大网友互动??
点此进行留言吧!