單例模式鏈接數(shù)據(jù)庫(kù)

//db.php


/**

* 單例鏈接數(shù)據(jù)庫(kù)

* */

classDb {

static private$_instance;

static private$_connectSource;

private$_dbConfig=array(

'host'=>'127.0.0.1',

'user'=>'root',

'password'=>'',

'database'=>'video',

);

private function__construct() {

}

static public functiongetInstance() {

if(!(self::$_instanceinstanceof self)) {

self::$_instance=new self();

}

return self::$_instance;

}

public functionconnect() {

if(!self::$_connectSource) {

self::$_connectSource= @mysql_connect($this->_dbConfig['host'],$this->_dbConfig['user'],$this->_dbConfig['password']);

if(!self::$_connectSource) {

throw newException('mysql connect error '. mysql_error());

//die('mysql connect error' . mysql_error());

}

mysql_select_db($this->_dbConfig['database'],self::$_connectSource);

mysql_query("set names UTF8",self::$_connectSource);

}

return self::$_connectSource;

}

}

//response.php


classResponse {

constJSON="json";

/**

* 按綜合方式輸出通信數(shù)據(jù)

*@paraminteger $code 狀態(tài)碼

*@paramstring $message 提示信息

*@paramarray $data 數(shù)據(jù)

*@paramstring $type 數(shù)據(jù)類型

* return string

*/

public static functionshow($code,$message='',$data=array(),$type=self::JSON) {

if(!is_numeric($code)) {

return'';

}

if(in_array($_GET['format'],array('json','xml','jsonp'))){

$type='json';

}

//? ? $type = isset($_GET['format']) ? $_GET['format'] : self::JSON;

$result=array(

'code'=>$code,

'message'=>$message,

'data'=>$data,

);

if($type=='json') {

self::json($code,$message,$data);

exit;

}elseif($type=='array') {

var_dump($result);

}elseif($type=='xml') {

self::xmlEncode($code,$message,$data);

exit;

}else{

//TODO

}

}

/**

* 按json方式輸出通信數(shù)據(jù)

*@paraminteger $code 狀態(tài)碼

*@paramstring $message 提示信息

*@paramarray $data 數(shù)據(jù)

* return string

*/

public static functionjson($code,$message='',$data=array()) {

if(!is_numeric($code)) {

return'';

}

$result=array(

'code'=>$code,

'message'=>$message,

'data'=>$data

);

echojson_encode($result);

exit;

}

/**

* 按xml方式輸出通信數(shù)據(jù)

*@paraminteger $code 狀態(tài)碼

*@paramstring $message 提示信息

*@paramarray $data 數(shù)據(jù)

* return string

*/

public static functionxmlEncode($code,$message,$data=array()) {

if(!is_numeric($code)) {

return'';

}

$result=array(

'code'=>$code,

'message'=>$message,

'data'=>$data,

);

header("Content-Type:text/xml");

$xml="\n";

$xml.="\n";

$xml.=self::xmlToEncode($result);

$xml.="";

echo$xml;

}

/**

* 轉(zhuǎn)換成xml數(shù)據(jù)返回

* */

public static functionxmlToEncode($data) {

$xml=$attr="";

foreach($dataas$key=>$value) {

if(is_numeric($key)) {

$attr=" id='{$key}'";

$key="item";

}

$xml.="<{$key}{$attr}>";

$xml.= is_array($value) ?self::xmlToEncode($value) :$value;

$xml.="\n";

}

return$xml;

}

}

//list.php



// http://app.com/list.php?page-=1&pagesize=12

require_once('./response.php');

require_once('./file.php');

$file=newFile();

$data=$file->cacheData('index_cron_cahce');

if($data) {

returnResponse::show(200,'首頁數(shù)據(jù)獲取成功',$data);

}else{

returnResponse::show(400,'首頁數(shù)據(jù)獲取失敗',$data);

}

exit;

require_once('./db.php');

require_once('./file.php');

$page=isset($_GET['page']) ?$_GET['page'] :1;

$pageSize=isset($_GET['pagesize']) ?$_GET['pagesize'] :6;

if(!is_numeric($page) || !is_numeric($pageSize)) {

returnResponse::show(401,'數(shù)據(jù)不合法');

}

$offset= ($page-1) *$pageSize;

$sql="select*from video where status = 1 order by orderby desc limit ".$offset." , ".$pageSize;

$cache=newFile();

$videos=array();

if(!$videos=$cache->cacheData('index_mk_cache'.$page.'-'.$pageSize)) {

echo1;exit;

try{

$connect= Db::getInstance()->connect();

}catch(Exception$e) {

// $e->getMessage();

returnResponse::show(403,'數(shù)據(jù)庫(kù)鏈接失敗');

}

$result= mysql_query($sql,$connect);

while($video= mysql_fetch_assoc($result)) {

$videos[] =$video;

}

if($videos) {

$cache->cacheData('index_mk_cache'.$page.'-'.$pageSize,$videos,1200);

}

}

if($videos) {

returnResponse::show(200,'首頁數(shù)據(jù)獲取成功',$videos);

}else{

returnResponse::show(400,'首頁數(shù)據(jù)獲取失敗',$videos);

}

//緩存文件



/**

* 生成緩存文件、刪除、讀取緩存的操作封裝

*

* */

classFile {

private$_dir;

constEXT='.txt';

/**

* 初始化文件存儲(chǔ)位置

* */

public function__construct() {

$this->_dir= dirname(__FILE__) .'/files/';

}

/**

* param $key 緩存文件名稱

* param $value 緩存文件的值

* */

public functioncacheData($key,$value='') {

$filename=$this->_dir.$key.self::EXT;

if($value!=='') {// 將value值寫入緩存

if(is_null($value)) {

return@unlink($filename);

}

$dir= dirname($filename);

if(!is_dir($dir)) {

mkdir($dir,0777);

}

returnfile_put_contents($filename,json_encode($value));

}

if(!is_file($filename)) {

return FALSE;

}

/*

* 定時(shí)刪除緩存文件

* */

//? ? $contents = file_get_contents($filename);

//? ? $cacheTime = (int)substr($contents, 0 ,11);

//? ? $value = substr($contents, 11);

//? ? if($cacheTime !=0 && ($cacheTime + filemtime($filename) < time())) {

//? ? ? unlink($filename);

//? ? ? return FALSE;

//? ? }

returnjson_decode(file_get_contents($filename),true);

}

}





/**

* 生成緩存文件、刪除、讀取緩存的操作封裝

*

* */

classFile {

private$_dir;

constEXT='.txt';

/**

* 初始化文件存儲(chǔ)位置

* */

public function__construct() {

$this->_dir= dirname(__FILE__) .'/files/';

}

/**

* param $key 緩存文件名稱

* param $value 緩存文件的值

* */

public functioncacheData($key,$value='') {

$filename=$this->_dir.$key.self::EXT;

if($value!=='') {// 將value值寫入緩存

if(is_null($value)) {

return@unlink($filename);

}

$dir= dirname($filename);

if(!is_dir($dir)) {

mkdir($dir,0777);

}

returnfile_put_contents($filename,json_encode($value));

}

if(!is_file($filename)) {

return FALSE;

}

/*

* 定時(shí)刪除緩存文件

* */

//? ? $contents = file_get_contents($filename);

//? ? $cacheTime = (int)substr($contents, 0 ,11);

//? ? $value = substr($contents, 11);

//? ? if($cacheTime !=0 && ($cacheTime + filemtime($filename) < time())) {

//? ? ? unlink($filename);

//? ? ? return FALSE;

//? ? }

returnjson_decode(file_get_contents($filename),true);

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,568評(píng)論 19 139
  • 如何單例模式鏈接數(shù)據(jù)庫(kù) =================== 1.單例模式 單例模式主要是三點(diǎn):隱藏掉(priv...
    MadLife程序員閱讀 1,919評(píng)論 0 3
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,706評(píng)論 18 399
  • 聞?dòng)嵧瑢W(xué)徐輝的著作出版,訂購(gòu),散文集,《淌過詩人的河流》。 收到老同學(xué)岳陽快遞的《淌過詩人的河流》,萬分親切!然,...
    傳頻閱讀 419評(píng)論 0 2
  • 靜態(tài)緩存 要使用靜態(tài)緩存功能,需要開啟HTML_CACHE_ON參數(shù),并且使用HTML_CACHE_RULES配置...
    欒呱呱閱讀 2,374評(píng)論 0 6

友情鏈接更多精彩內(nèi)容