一、memcached
memcached 是一個高效的分布式的內(nèi)存對象緩存系統(tǒng) ,他可以支持把各種php的數(shù)據(jù)(array,對象,基本數(shù)據(jù)類型)放入到它管理的內(nèi)存中.注:需通過腳本定時清除緩存题暖,防止緩存過大影響網(wǎng)站性能。
示例代碼:
conn.php
<?php
$link=mysql_connect("localhost","root",null);
mysql_select_db("bbs",$link);
mysql_query("set names utf8");
?>
memcache_getid.php
<?php
include_once 'conn.php';
$id=$_GET['id'];
$memcache = new memcache;
$memcache->connect('127.0.0.1', 11211) or die ("連接失敗");
//$memcache->flush(); 清除緩存
if($info=$memcache->get($id))
{
echo $info;
exit;
}
else
{
$result=mysql_query("select * from user where id=$id");
if($result)
{
$arr=mysql_fetch_array($result);
echo "need mysql query";
$memcache->add($id,$arr['id'],MEMCACHE_COMPRESSED,60*60*24);
}
}
?>
二醒颖、頁面靜態(tài)化技術(shù)
1、真靜態(tài)化
(1)創(chuàng)建模板文件template.html
(2)通過模板文件,創(chuàng)建靜態(tài)頁面的 php文件 xx.php
(3) 用戶訪問生成的靜態(tài)頁面 xx.html
newsAction.php
<?php
header("content-type:text/html;charset=utf-8");
function replace($row,$title,$content){
//含義是 用 $title的內(nèi)容替換 $row中的 %title%
$row=str_replace("%title%",$title,$row);
$row=str_replace("%content%",$content,$row);
return $row;
}
//處理添加、修改、刪除請求
//1.接收一下oper
$oper=$_REQUEST['oper'];
if($oper=="add"){
//接收title,content
$title=$_POST['title'];
$content=$_POST['content'];
//1.把數(shù)據(jù)放入到mysql, 同時創(chuàng)建一個html
//添加到數(shù)據(jù)庫 SqlHelper.class.php
$conn=mysql_connect("localhost","root","root");
if(!$conn){
die("連接失敗");
}
//構(gòu)建html_filename
//$file=
mysql_select_db("spdb1",$conn);
$sql="insert into news (title,content) values('$title','$content')";
if(mysql_query($sql,$conn)){
//獲取剛剛插入數(shù)據(jù)的id號
$id=mysql_insert_id();
$html_filename="news_id".$id.".html";
//echo "文件名=".$html_filename;
//創(chuàng)建html文件
$fp_tmp=fopen("template.tpl","r");
$fp_html_file=fopen($html_filename,"w");
//思路->tmp->html 逐行讀取template.tpl文件击罪,然后逐行替換
while(!feof($fp_tmp)){
//讀取一行.
$row=fgets($fp_tmp);
//替換(小函數(shù))
$new_row=replace($row,$title,$content);
//把替換后的一行寫入到html文件
fwrite($fp_html_file,$new_row);
}
//關(guān)閉文件流
fclose($fp_tmp);
fclose($fp_html_file);
echo "添加到數(shù)據(jù)庫并成功創(chuàng)建html文件<a href='news_list.php'>返回列表</a>";
}
mysql_close($conn);
}
?>
show_news.php
<?php
//接受id
$id=@$_GET['id'];
//看看如何使用html靜態(tài)頁面
//思路,看看html頁面是否有贪薪,如果有媳禁,直接訪問,沒有就創(chuàng)建
//構(gòu)建一個文件名.
$html_filename="news_id".$id.".html";
echo file_get_contents($html_filename);
//filemtime()=>獲取文件的最后修改時間
//filemtime($html_filename)+30>time() 表示靜態(tài)文件画切,
// if(file_exists($html_filename)&& filemtime($html_filename)+30>time()){
//
// //直接訪問html頁面(把html頁面的內(nèi)容 echo 瀏覽器)
// echo file_get_contents($html_filename);
// exit;
// }
//
// $conn=mysql_connect("localhost","root","root");
//
// if(!$conn){
// die("連接失敗");
// }
//
// mysql_select_db("spdb1",$conn);
//
//
// $sql="select * from news where id=$id";
// $res=mysql_query($sql);
// //開啟ob緩存
// ob_start();
// if($row=mysql_fetch_assoc($res)){
//
// header("content-type:text/html;charset=utf-8");
// echo "<table border='1px' bordercolor='green' cellspacing='0' width=400px height=200px>";
// echo "<tr><td>新聞詳細內(nèi)容</td></tr>";
// echo "<tr><td>{$row['title']}</td></tr>";
// echo "<tr><td>{$row['content']}</td></tr>";
// echo "</table>";
// }else{
// echo "沒有結(jié)果";
// }
//
// $html_content=ob_get_contents();
// $my_hader="<head><meta http-equiv='content-type' content='text/html;charset=utf-8'/></head>";
// //把ob->$html_filename (必要時竣稽,需要考慮路徑)
// file_put_contents($html_filename,$my_hader.$html_content);
//
// mysql_free_result($res);
// mysql_close($conn);
?>
2、偽靜態(tài)化
環(huán)境配置:#LoadModule rewrite_module modules/mod_rewrite.so 在httpd.conf去掉改項#霍弹,并項目目錄下配置.htaccess文件
.htaccess
<IfModule rewrite_module>
#寫你的rewrite規(guī)則
RewriteEngine On
#news-id(d+).html$ 是規(guī)則 news.php?id=$1 是轉(zhuǎn)發(fā)的頁面
#正則 子表達式 捕獲 反向引用
# "news-id33.html"
# 可以配置多個規(guī)則毫别,匹配的順序是從上到下
RewriteRule news-id(d+).html$ news.php?id=$1
RewriteRule news-id(d+).html$ error.php
</IfModule>
①真靜態(tài)訪問效率高,利于seo.可以減少對數(shù)據(jù)庫的操作典格。但是會占用大量的磁盤岛宦。
②偽靜態(tài)一、可以方便的實現(xiàn)對搜索引擎的優(yōu)化耍缴;二砾肺、占空間比較小防嗡;三变汪、通過生成不同view-id2.hmtl 可以實現(xiàn)內(nèi)容的變化;四有效的防止了注入攻擊蚁趁;
注:但是兩者在啟用頁面緩存時(ob_start)需要注意一個問題裙盾,不要需要經(jīng)常修改的html文件放入頁面緩存,否則會造成頁面無法刷新得到最新結(jié)果,頁面緩存一般存放經(jīng)常被查詢的html且不會被更新闷煤。
3童芹、mysql優(yōu)化技巧
配置慢查詢?nèi)罩荆?br>
在my.ini最下面配置
log-slow-queries = e:/wamp/logs/mysql_slow_query.log
long_query_time=2
通過 show status/variables like '%query%'' 查看是否配置成功(即slow_query_log=ON)
分析慢查詢?nèi)罩?br>
通過select sleep(4);測試
通過explain 慢sql語句或mysqldumpslow 慢查詢?nèi)罩?br>
查詢sql語句狀態(tài)
set profilling=on;
show profiles;
show profile for query id;
- 使用order by null 禁用排序(默認為filesort)
比如 select * from dept group by ename order by null - 在精度要求高的應用中鲤拿,建議使用定點數(shù)(decimal)來存儲數(shù)值假褪,以保證結(jié)果的準確性
3.表的水平劃分/垂直分割
來源:http://www.jsdaima.com/blog/145.html