午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

    php數(shù)據(jù)庫(kù)連接配合memcache
    來(lái)源:易賢網(wǎng) 閱讀:1726 次 日期:2014-09-10 11:35:00
    溫馨提示:易賢網(wǎng)小編為您整理了“php數(shù)據(jù)庫(kù)連接配合memcache”,方便廣大網(wǎng)友查閱!

    采用memcache與數(shù)據(jù)庫(kù)連接查詢的方式進(jìn)行數(shù)據(jù)緩存目前是采用單個(gè)的memcache服務(wù)器,以后會(huì)添加多個(gè)的

    <?php

    /**

    * @author

    * @name data link class and memcache class

    * //使用memcache的查詢結(jié)果

    * 傳送sql語(yǔ)句,返回是查詢后的數(shù)組,數(shù)組有可能為空

    * $dataArrayName = $db->get_Date($sql);

    * 如果查詢的是單條數(shù)據(jù),則要進(jìn)行輸出時(shí)采用

    * $dataArrayName[0]['字段名']的格式

    */

    class Datelink

    {

    private $DateServer = "localhost";//mysql數(shù)據(jù)庫(kù)地址

    private $DateBase = "basename";//mysql中的數(shù)據(jù)庫(kù)

    private $DateUser = "username";//mysql數(shù)據(jù)庫(kù)連接帳號(hào)

    private $Datepwd = "userpwd";//mysql數(shù)據(jù)庫(kù)連接密碼

    private $dbLink;//連接對(duì)象

    private $result;//數(shù)據(jù)查詢結(jié)果

    private $insert_id;//定義插入序號(hào)

    private $affected_rows;//定義影響行數(shù)

    static $data_obj;

    private $mem_obj;

    #采用單例模式,實(shí)例化時(shí)進(jìn)行數(shù)據(jù)庫(kù)連接

    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){//設(shè)置屬性

    $this->$name=$value;

    }

    function __get($name){//獲取屬性

    return $this->$name;

    }

    function dbhalt($errmsg){//錯(cuò)誤反饋

    die($errmsg);

    }

    function get_Date($sql){//僅針對(duì)select 查詢進(jìn)行緩存

    if(preg_match("/^select/i",$sql)){//如果是select這里增加memcache內(nèi)容的判斷

    if($this->mem_obj->cache_Obj){//進(jìn)行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 或者沒(méi)有memcache

    }

    //如果以上沒(méi)有進(jìn)行 memcache的查詢,則進(jìn)行普通查詢并返回

    $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){//執(zhí)行sql

    $this->result = mysql_query($sql,$this->dbLink);

    }

    function num_rows(){//返回行數(shù),參數(shù)記錄集

    return mysql_num_rows($this->result);

    }

    function get_rows($sql){//返回行數(shù),參數(shù)為sql

    $this->execute($sql);

    return $this->num_rows($this->result);

    }

    function insert($sql){//插入sql-有自動(dòng)增長(zhǎng)序號(hào),返回新建序號(hào)

    $this->execute($sql);

    $this->insert_id = mysql_insert_id($this->dbLink);

    $this->free_result($this->result);

    return $this->insert_id;

    }

    function insert_($sql){//插入sql-沒(méi)有自動(dòng)增長(zhǎng)序號(hào),返回影響行數(shù)

    $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(){//關(guān)閉當(dāng)前數(shù)據(jù)庫(kù)

    @mysql_close($this->dbLink);

    }

    }//結(jié)束class的括號(hào)

    class Mem{//memcache設(shè)置

    private $server_ip="";

    private $server_port="11211";//默認(rèn)端口

    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){//設(shè)置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){//刪除某個(gè)數(shù)據(jù)

    return $this->cache_Obj->delete(md5($sql));

    }

    public function delAll(){//讓所有cache失效,并不清空,會(huì)有新數(shù)據(jù)取代

    return $this->cache_Obj->flush();

    }

    }

    $db=Datelink::contect_data(); //這里是調(diào)用數(shù)據(jù)庫(kù)連接

    // $db->mem_obj->delAll();//這里可以其清除全部緩存

    ?>

    Memcache與mysql配合查詢,進(jìn)行數(shù)據(jù)緩存

    更多信息請(qǐng)查看IT技術(shù)專欄

    更多信息請(qǐng)查看網(wǎng)絡(luò)編程
    易賢網(wǎng)手機(jī)網(wǎng)站地址:php數(shù)據(jù)庫(kù)連接配合memcache
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2025國(guó)考·省考課程試聽(tīng)報(bào)名

    • 報(bào)班類型
    • 姓名
    • 手機(jī)號(hào)
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專用圖標(biāo)