Heim  >  Artikel  >  Backend-Entwicklung  >  Ausführliche Erklärung, wie man die Entwicklung umfangreicher Websites in PHP optimiert

Ausführliche Erklärung, wie man die Entwicklung umfangreicher Websites in PHP optimiert

黄舟
黄舟Original
2017-07-27 10:12:472516Durchsuche

1.memcached

memcached 是一个高效的分布式的内存对象缓存系统 ,他可以支持把各种php的数据(array,对象,基本数据类型)放入到它管理的内存中.注:需通过脚本定时清除缓存,防止缓存过大影响网站性能

示例代码:
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 &#39;conn.php&#39;;
$id=$_GET[&#39;id&#39;];
$memcache = new memcache;
$memcache->connect(&#39;127.0.0.1&#39;, 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[&#39;id&#39;],MEMCACHE_COMPRESSED,60*60*24);
    }
}
?>


2.页面静态化技术

a.真静态化

1.创建模板文件template.html
2.通过模板文件,创建静态页面的 php文件  xx.php
3. 用户访问生成的静态页面  xx.html

newsAction.php
<?php


    header("content-type:text/html;charset=utf-8");


    function replace($row,$title,$content){
            //含义是 用 $title的内容替换 $row中的 %title%
            $row=str_replace("%title%",$title,$row);
            $row=str_replace("%content%",$content,$row);
            return $row;

    }


    //处理添加、修改、删除请求
    //1.接收一下oper
    $oper=$_REQUEST[&#39;oper&#39;];

    
    if($oper=="add"){
        

            //接收title,content
            $title=$_POST[&#39;title&#39;];
            $content=$_POST[&#39;content&#39;];

            //1.把数据放入到mysql, 同时创建一个html

            //添加到数据库 SqlHelper.class.php

            $conn=mysql_connect("localhost","root","root");
        
            if(!$conn){
                die("连接失败");
            }

            //构建html_filename
            //$file=

            mysql_select_db("spdb1",$conn);

            $sql="insert into  news (title,content) values(&#39;$title&#39;,&#39;$content&#39;)";

            if(mysql_query($sql,$conn)){
                
                //获取刚刚插入数据的id号
                $id=mysql_insert_id();
                $html_filename="news_id".$id.".html";
                //echo "文件名=".$html_filename;

                //创建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);
                        //替换(小函数)
                        $new_row=replace($row,$title,$content);

                        //把替换后的一行写入到html文件
                        fwrite($fp_html_file,$new_row);
                }

                //关闭文件流
                fclose($fp_tmp);
                fclose($fp_html_file);

                echo "添加到数据库并成功创建html文件<a href=&#39;news_list.php&#39;>返回列表</a>";

            }

            
            mysql_close($conn);

    }


?>
show_news.php
<?php


	//接受id
	$id=@$_GET[&#39;id&#39;];

		//看看如何使用html静态页面
		//思路,看看html页面是否有,如果有,直接访问,没有就创建
		//构建一个文件名.
		$html_filename="news_id".$id.".html";

		echo file_get_contents($html_filename);
		//filemtime()=>获取文件的最后修改时间
		//filemtime($html_filename)+30>time() 表示静态文件,
//		if(file_exists($html_filename)&& filemtime($html_filename)+30>time()){
//			
//			//直接访问html页面(把html页面的内容 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=&#39;1px&#39; bordercolor=&#39;green&#39; cellspacing=&#39;0&#39; width=400px height=200px>";
//			echo "<tr><td>新闻详细内容</td></tr>";
//			echo "<tr><td>{$row[&#39;title&#39;]}</td></tr>";
//			echo "<tr><td>{$row[&#39;content&#39;]}</td></tr>";
//			echo "</table>";
//		}else{
//			echo "没有结果";
//		}
//
//		$html_content=ob_get_contents();
//		$my_hader="<head><meta http-equiv=&#39;content-type&#39; content=&#39;text/html;charset=utf-8&#39;/></head>";
//		//把ob->$html_filename (必要时,需要考虑路径)
//		file_put_contents($html_filename,$my_hader.$html_content);
//
//		mysql_free_result($res);
//		mysql_close($conn);


?>


b.伪静态化

环境配置:#LoadModule rewrite_module modules/mod_rewrite.so 在httpd.conf去掉改项#,并项目目录下配置.htaccess文件

.htaccess
<IfModule rewrite_module>
#写你的rewrite规则
RewriteEngine On
#news-id(\d+)\.html$ 是规则  news.php?id=$1 是转发的页面
#正则 子表达式 捕获 反向引用
#  "news-id33.html"
# 可以配置多个规则,匹配的顺序是从上到下
RewriteRule  news-id(\d+)\.html$   news.php?id=$1
RewriteRule  news-id(\d+)\.html$     error.php
</IfModule>


①真静态访问效率高,利于seo.可以减少对数据库的操作。但是会占用大量的磁盘.
②伪静态一、可以方便的实现对搜索引擎的优化,二、占空间比较小。三、通过生成不同view-id2.hmtl 可以实现内容的变化.四有效的防止了注入攻击

注:但是两者在启用页面缓存时(ob_start)需要注意一个问题,不要需要经常修改的html文件放入页面缓存,否则会造成页面无法刷新得到最新结果,页面缓存一般存放经常被查询的html且不会被更新

c.mysql优化技巧

配置慢查询日志:

在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)

分析慢查询日志

通过select sleep(4);测试

通过explain 慢sql语句或mysqldumpslow 慢查询日志

查询sql语句状态

set profilling=on;
show profiles;
show profile for query id;

1. 使用order by null  禁用排序(默认为filesort)

比如 select * from dept group by ename order by null

2. 在精度要求高的应用中,建议使用定点数(decimal)来存储数值,以保证结果的准确性

3.表的水平划分/垂直分割


Das obige ist der detaillierte Inhalt vonAusführliche Erklärung, wie man die Entwicklung umfangreicher Websites in PHP optimiert. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn