需求:显示大家的记录只显示前200条,第一页20条,共十页
数据库表结构:
数据库表结构:
mysql> desc record_cache;
+----------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| mc_key | varchar(2) | NO | MUL | | |
| mc_value | mediumtext | NO | | | |
+----------+------------+------+-----+---------+----------------+
+----------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| mc_key | varchar(2) | NO | MUL | | |
| mc_value | mediumtext | NO | | | |
+----------+------------+------+-----+---------+----------------+
<?php
class VMc
{
private $db;
public function __construct()
{
$this->db = new MysqlPdo ( );
}
public function vMcSet($mcKey, $mcValue)
{
$mcValue ['ctime'] = time ();
$data ['mc_value'] = serialize ( $mcValue );
$data ['mc_key'] = $mcKey;
if ($this->db->Insert ( 'record_cache', $data ))
{
return true;
}
else
{
return false;
}
return false;
}
public function vMcGet($mcKey)
{
$sql = "SELECT mc_value FROM record_cache WHERE mc_key = '$mcKey '";
$bRet = $this->db->Select ( $sql );
if ($bRet)
{
$res = $this->db->Fetch ();
if ($res)
{
$res = unserialize ( $res ['mc_value'] );
}
return $res;
}
return false;
}
public function flushCache()
{
$db = $this->db->ConnectM ();
$sql = 'TRUNCATE TABLE record_cache';
if ($db->query ( $sql ))
{
return true;
}
else
{
return false;
}
}
}
?>
class VMc
{
private $db;
public function __construct()
{
$this->db = new MysqlPdo ( );
}
public function vMcSet($mcKey, $mcValue)
{
$mcValue ['ctime'] = time ();
$data ['mc_value'] = serialize ( $mcValue );
$data ['mc_key'] = $mcKey;
if ($this->db->Insert ( 'record_cache', $data ))
{
return true;
}
else
{
return false;
}
return false;
}
public function vMcGet($mcKey)
{
$sql = "SELECT mc_value FROM record_cache WHERE mc_key = '$mcKey '";
$bRet = $this->db->Select ( $sql );
if ($bRet)
{
$res = $this->db->Fetch ();
if ($res)
{
$res = unserialize ( $res ['mc_value'] );
}
return $res;
}
return false;
}
public function flushCache()
{
$db = $this->db->ConnectM ();
$sql = 'TRUNCATE TABLE record_cache';
if ($db->query ( $sql ))
{
return true;
}
else
{
return false;
}
}
}
?>
public function GetRandomRecord(&$current, $page = 1, $offset = 20)
{
$total = 200;
*/
//////////////////////临时缓存//////////////////////////////
$vMc = new VMc ( );
$mcValues = $vMc->vMcGet ( $page );
if (! empty ( $mcValues ))
{
if (time () - $mcValues ['ctime'] <= 1800)
{
$current ['count'] = $mcValues ['count'];
unset ( $mcValues ['count'], $mcValues ['ctime'] );
foreach ( $mcValues as $k => $v )
{
if (! empty ( $v ))
{
$current ['record'] [$k] = $v;
}
}
return true;
}
}
/////////////////////////////////////////////////////////////
$current ['count'] = $total;
$start = $offset * ($page - 1);
$sql = "select * from record where status = 0 order by ctime desc limit " . $total;
$bRet = $this->db->Select ( $sql );
if ($bRet)
{
//fetch the result
$res = $this->db->FetchAll ();
}
//if mc is open put them into memcache
if (count ( $res ) < 200)
{
$current ['count'] = count ( $res );
}
$tp = ceil ( count ( $res ) / $offset );
$tmp = 0;
for($i = 0; $i < $tp; $i ++)
{
$cp = $i + 1;
$kstart = $i * $offset;
$pagerec = array_slice ( $res, $kstart, $offset );
$pagerec ['count'] = $current ['count'];
//////////////////////临时缓存//////////////////////////////
if ($cp == 1)
{
if ($vMc->flushCache ())
{
$tmp = 1;
}
}
if ($tmp == 1)
{
$vMc->vMcSet ( $cp, $pagerec );
}
//////////////////////////////////////////////////////////
/* if($this->mcobj){
$this->mcobj->set('RANDOM_RECORD_' . $cp , $pagerec ,false ,60*30);
}
*/
//set the return data
if ($cp == $page)
{
$current ['record'] = array_slice ( $res, $start, $offset );
}
}
return true;
}
{
$total = 200;
*/
//////////////////////临时缓存//////////////////////////////
$vMc = new VMc ( );
$mcValues = $vMc->vMcGet ( $page );
if (! empty ( $mcValues ))
{
if (time () - $mcValues ['ctime'] <= 1800)
{
$current ['count'] = $mcValues ['count'];
unset ( $mcValues ['count'], $mcValues ['ctime'] );
foreach ( $mcValues as $k => $v )
{
if (! empty ( $v ))
{
$current ['record'] [$k] = $v;
}
}
return true;
}
}
/////////////////////////////////////////////////////////////
$current ['count'] = $total;
$start = $offset * ($page - 1);
$sql = "select * from record where status = 0 order by ctime desc limit " . $total;
$bRet = $this->db->Select ( $sql );
if ($bRet)
{
//fetch the result
$res = $this->db->FetchAll ();
}
//if mc is open put them into memcache
if (count ( $res ) < 200)
{
$current ['count'] = count ( $res );
}
$tp = ceil ( count ( $res ) / $offset );
$tmp = 0;
for($i = 0; $i < $tp; $i ++)
{
$cp = $i + 1;
$kstart = $i * $offset;
$pagerec = array_slice ( $res, $kstart, $offset );
$pagerec ['count'] = $current ['count'];
//////////////////////临时缓存//////////////////////////////
if ($cp == 1)
{
if ($vMc->flushCache ())
{
$tmp = 1;
}
}
if ($tmp == 1)
{
$vMc->vMcSet ( $cp, $pagerec );
}
//////////////////////////////////////////////////////////
/* if($this->mcobj){
$this->mcobj->set('RANDOM_RECORD_' . $cp , $pagerec ,false ,60*30);
}
*/
//set the return data
if ($cp == $page)
{
$current ['record'] = array_slice ( $res, $start, $offset );
}
}
return true;
}
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/1660/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
评论列表