關注的緩存自然后端頁面緩存相關,從php的smarty的緩存切入,所以就安裝用下。
下載
目前smarty有3.x和2.x版本,兩者的對于php的版本要求不同,3.x版要求php5.2+。我下載了2.x版。
- 在centos根目錄下新建data目錄
mkdir data
cd data - 在data目錄下下載解壓smarty 2.x版
wget https://github.com/smarty-php/smarty/archive/v2.6.28.tar.gz
tar -zxvf v2.6.28.tar.gz
例子
進入smarty的解壓目錄,把其中的libs目錄拷貝到php服務目錄下
cd smarty-2.6.28
cp -r libs /var/www/html/libs在php服務目錄下執行以下新建Mysmarty/cache(緩存目錄)、Mysmarty/configs(配置目錄)、Mysmarty/templates(模板目錄)、Mysmarty/templates_c(模板編譯目錄),注意把cache,templates_c對用戶權限修改成可讀寫
mkdir Mysmarty
cd Mysmarty
mkdir cache configs templates templates_c
chmod 777 cache //更改成全權限,在測試使用中可這樣做
chmod 777 templates_c-
在templates目錄下新建模板文件test.tpl
cd templates
vi test.tpl編輯test.tpl內容: <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <title><{$title}></title> </head> <body> <{$content}> </body> </html>
-
在Mysmarty目錄下新建調用文件test.php
cd .. //回到Mysmarty目錄
vi test.php編輯test.php內容: <?php include_once("/var/www/html/libs/Smarty.class.php"); //包含smarty類文件 $smarty=new Smarty(); //建立smarty實例對象$smarty $smarty->template_dir='/var/www/html/Mysmarty/templates'; //設置模板目錄 $smarty->compile_dir='/var/www/html/Mysmarty/templates_c'; //設置模板編譯生成文件目錄 $smarty->config_dir='/var/www/html/Mysmarty/configs'; $smarty->cache_dir='/var/www/html/Mysmarty/cache'; //設置緩存目錄 $smarty->cache_lifetime=30; //緩存時間 $smarty->caching=true; //true開啟緩存,false關閉緩存 $smarty->left_delimiter="<{"; $smarty->right_delimiter="}>"; $smarty->assign("title","test"); $smarty->assign("content","hello smarty."); $smarty->display("test.tpl"); ?>
瀏覽器訪問驗證
瀏覽器下訪問域名+/Mysmarty/test.php,我自己的的路徑是http://192.16.137.2/Mysmarty/test.php, 看到結果如下,就OK了。
73VS(}FOA4Y2_A3_RZ2UW45.png
參考
http://www.php100.com/manual/smarty/installing.smarty.basic.html