采用memcache与数据库连接查询的方式进行数据缓存目前是采用单个的memcache服务器,以后会添加多个的
<?php
/**
* @author
* @name data link class and memcache class
* //使用memcache的查询结果
* 传送sql语句,返回是查询后的数组,数组有可能为空
* $dataArrayName = $db->get_Date($sql);
* 如果查询的是单条数据,则要进行输出时采用
* $dataArrayName[0]['字段名']的格式
*/
class Datelink
{
private $DateServer = "localhost";//mysql数据库地址
private $DateBase = "basename";//mysql中的数据库
private $DateUser = "username";//mysql数据库连接帐号
private $Datepwd = "userpwd";//mysql数据库连接密码
private $dbLink;//连接对象
private $result;//数据查询结果
private $insert_id;//定义插入序号
private $affected_rows;//定义影响行数
static $data_obj;
private $mem_obj;
#采用单例模式,实例化时进行数据库连接
function __construct(){
$this->dbLink=@mysql_connect($this->DateServer,$this->DateUser,$this->Datepwd)or die(mysql_error());
if(!$this->dbLink)$this->dbhalt("exsiting error when connecting!");
if(!@mysql_select_db($this->DateBase,$this->dbLink))$this->dbhalt("can't use this database,please check database!");
mysql_query("set names utf-8",$this->dbLink);//如果是utf-8可以改为utf-8
$this->mem_obj = Mem::__GetObj();
}
private function __clone(){}
static function contect_data(){
if(!self::$data_obj instanceof self){
self::$data_obj = new self();
}
return self::$data_obj;
}
function __set($name,$value){//设置属性
$this->$name=$value;
}
function __get($name){//获取属性
return $this->$name;
}
function dbhalt($errmsg){//错误反馈
die($errmsg);
}
function get_Date($sql){//仅针对select 查询进行缓存
if(preg_match("/^select/i",$sql)){//如果是select这里增加memcache内容的判断
if($this->mem_obj->cache_Obj){//进行mem 查询看是否存在缓存
if($temp=$this->mem_obj->get($sql)){
$this->result=$temp;
return $temp;
}else{
$this->execute($sql);
$rows = $this->num_rows();
$temp = array();
for ($i=0;$i<$rows;$i++) {
$fields = mysql_num_fields($this->result);
$row = mysql_fetch_array($this->result);
for ($j=0;$j<$fields;$j++) {
if ($i == 0) {
$columns[$j] = mysql_field_name($this->result,$j);
}
$temp[$i][$columns[$j]] = $row[$j];
}
}
//$temp = $this->fetch_array();
$this->mem_obj->set($sql,$temp);
return $temp;
}
}//如果不是select 或者没有memcache
}
//如果以上没有进行 memcache的查询,则进行普通查询并返回
$this->execute($sql);
$rows = $this->num_rows();
$temp = array();
for ($i=0;$i<$rows;$i++) {
$fields = mysql_num_fields($this->result);
$row = mysql_fetch_array($this->result);
for ($j=0;$j<$fields;$j++) {
if ($i == 0) {
$columns[$j] = mysql_field_name($this->result,$j);
}
$temp[$i][$columns[$j]] = $row[$j];
}
}
return $temp;
}
function fetch_array(){
return mysql_fetch_array($this->result);
}
function execute($sql){//执行sql
$this->result = mysql_query($sql,$this->dbLink);
}
function num_rows(){//返回行数,参数记录集
return mysql_num_rows($this->result);
}
function get_rows($sql){//返回行数,参数为sql
$this->execute($sql);
return $this->num_rows($this->result);
}
function insert($sql){//插入sql-有自动增长序号,返回新建序号
$this->execute($sql);
$this->insert_id = mysql_insert_id($this->dbLink);
$this->free_result($this->result);
return $this->insert_id;
}
function insert_($sql){//插入sql-没有自动增长序号,返回影响行数
$this->execute($sql);
$this->affected_rows = mysql_affected_rows($this->dbLink);
$this->free_result($this->result);
return $this->affected_rows;
}
function update($sql){//更新sql
$this->execute($sql);
$this->affected_rows=mysql_affected_rows($this->dbLink);
$this->free_result($this->result);
return $this->affected_rows;
}
function del($sql){//删除sql
$this->execute($sql);
$this->affected_rows=mysql_affected_rows($this->dbLink);
$this->free_result($this->result);
return $this->affected_rows;
}
function free_result(){//释放记录集
@mysql_free_result($this->result);
}
function close(){//关闭当前数据库
@mysql_close($this->dbLink);
}
}//结束class的括号
class Mem{//memcache设置
private $server_ip="";
private $server_port="11211";//默认端口
static $mem_Obj;
public $cache_Obj;
function __construct(){
if($this->cache_Obj=@new Memcache){
if(!@$this->cache_Obj->connect($this->server_ip,$this->server_port))$this->cache_Obj=false; }
}
private function __clone(){}
static function __GetObj(){
if(!self::$mem_Obj instanceof self)self::$mem_Obj = new self;
return self::$mem_Obj;
}
public function set($sql,$tempSource){//设置cache
return $this->cache_Obj->set(md5($sql),$tempSource);
}
public function add($sql,$tmpSource){//add添加cache,如果存在则删除后添加
if($this->get($sql))$this->del($sql);
return $this->set($sql,$tmpSource);
}
public function get($sql){//获取cache
if($temp=$this->cache_Obj->get(md5($sql))){
return $temp;
}
return false;
}
public function del($sql){//删除某个数据
return $this->cache_Obj->delete(md5($sql));
}
public function delAll(){//让所有cache失效,并不清空,会有新数据取代
return $this->cache_Obj->flush();
}
}
$db=Datelink::contect_data(); //这里是调用数据库连接
// $db->mem_obj->delAll();//这里可以其清除全部缓存
?>
Memcache与mysql配合查询,进行数据缓存
更多信息请查看IT技术专栏