首页 | PHP资讯 | 技术专栏 | 资源共享 | PHP培训 | PHP职场 | 图书 | PHP ON WIN | PHP圈子
返回列表 回复 发帖

让PDO支持数据缓存,昨晚失眠的结果!

让PDO支持数据缓存,昨晚失眠的结果!

类定义
[php]
/**
* 作者:初十
* QQ:345610000
*/
class myPDO extends PDO
{
public $cache_Dir = null; //缓存目录
public $cache_expireTime = 7200; //缓存时间,默认两小时

//带缓存的查询
public function cquery($sql)
{
  //缓存存放总目录
  if ($this->cache_Dir == null || !is_dir($this->cache_Dir)) {
   exit ("缓存目录有误!");
  } else {
   $this->cache_Dir = str_replace("\\", "/", $this->cache_Dir);
   $FileName = trim($this->cache_Dir, "/") . '/' . urlencode(trim($sql)) . '.sql';
  }
  //判断生成缓存
  if (!file_exists($FileName) ||  time() - filemtime($FileName) > $this->cache_expireTime) {
   if ($tmpRS = parent::query($sql)) {
    $data = serialize($tmpRS->fetchAll());
    self::createFile($FileName, $data);
   } else  {
    exit ("SQL语法错误<br />");
   }
  }
  return $this->readCache($FileName);
}

//读缓存文件
private static function readCache($FilePath)
{
  if (is_file($FilePath) && $Data = file_get_contents($FilePath)) {
   return new cache_PDOStatement(unserialize($Data));
  }
  return false;
}

//生成文件
public static function createFile($FilePath, $Data = '')
{
  if (file_put_contents($FilePath, $Data)) {
   return true;
  } else {
   return false;
  }
}
}
//缓存用到Statement类
class cache_PDOStatement
{
private $recordArr = array();
private $cursorId = 0;
private $recordCount = 0;

public function __construct($arr)
{
  $this->recordArr = $arr;
  $this->recordCount = count($arr);
}

//返回一条记录,指针下移一行
public function fetch()
{
  if ($this->cursorId == $this->recordCount) {
   return false;
  } else if ($this->cursorId == 0) {
   $this->cursorId++;
   return current($this->recordArr);
  } else {
   $this->cursorId++;
   return next($this->recordArr);
  }
}

//返回全部结果
public function fetchAll()
{
  return $this->recordArr;
}

//单行单列查询
public function fetchColumn()
{
  $tmpArr = current($this->recordArr);
  return $tmpArr[0];
}
}
[/php]

使用方法
[php]
$db = new myPDO('mysql: host = localhost;dbname=news','newsadmin','123456');

$db->cache_Dir = "cache"; //设置缓存目录
$db->cache_expireTime = 7200; //设置缓存时间

$rs = $db->cquery("select * from news limit 0,10"); //用缓存查询方法cquery代替query
while ($row = $rs->fetch()) {
echo $row["F_title"] . "<br />";
}

$rs = null;
$db = null;
[/php]

:victory: :victory:  不错!

TOP

如果使用memcache效果会更好

TOP

看起来很高深
杂草丛生处:chenhongbin007.blog.163.com

TOP

好!支持!谢谢!

TOP

不错,有进步,但是这样做直接将缓存和数据库耦合到一起了,以后如果想换种缓存方式,就必须改程序。对于技术性的程序,最好是一次设计永远使用。再说继承也是不推荐的扩展方式,缓存本身和数据库也没什么直接的关系

TOP

不错的,顶个,不过程序还是不太灵活,一些功能可以用单独的方法来实现,比如对于缓存目录的调置,缓存时间的设置都应该在类的内部有相应的实现方法,楼主还需要加强面向对象程序设计方面的学习
人不能没有奋斗的目标,尽管有的时候你说不清楚你的目标是什么,但在你的心中一定得有一样让你始终坚持追求的东西,这样你才会活得充实,更有意义

TOP

晕,竟然有文件缓存.
楼上已经有人说了,用memcache,
内存缓存多了,
xcache也行.

还有方法里加个参数来决定是否缓存,否则全部缓存,有意义吗?
你说呢?
简直是瞎搞!!!!

[ 本帖最后由 aaxron 于 2008-8-6 15:00 编辑 ]

TOP

原帖由 aaxron 于 2008-8-6 14:58 发表
晕,竟然有文件缓存.
楼上已经有人说了,用memcache,
内存缓存多了,
xcache也行.

还有方法里加个参数来决定是否缓存,否则全部缓存,有意义吗?
你说呢?
简直是瞎搞!!!!


1.文件缓存也比连接数据库快,但是对于分布式或者多台web类网站脏数据库问题会比较严重
2.为投资人省钱是首要的,这样你才能拿更多。并不是每个公司都会去买专门的缓存服务器的。
3.xcache算什么缓存,他只是把php解释器解释过的中间码(May be machine code)缓存了一下而已,并非数据缓存。所以拿出来炫耀的时候一定要搞清楚什么东西是干什么用的

TOP

原帖由 hobbs136 于 2008-8-7 07:31 发表
3.xcache算什么缓存,他只是把php解释器解释过的中间码(May be machine code)缓存了一下而已,并非数据缓存。所以拿出来炫耀的时候一定要搞清楚什么东西是干什么用的



xcache不能做内存缓存?????

[ 本帖最后由 aaxron 于 2008-8-11 18:33 编辑 ]

TOP

返回列表