PHP文件缓存smarty模板应用实例分析_php实例
本文实例分析了PHP文件缓存smarty模板应用。分享给大家供大家参考,具体如下:
一、使用缓存
要开启smarty的缓存,只需将caching设为true,并指定cache_dir即可.
使用cache_lefetime指定缓存生存时间,单位为秒
要对相同页面生成多个不同的缓存,在display或fetch中加入第二参数cache_id,如:
$smarty->display('index.tpl',$my_cache_id);
此特性可用于对不同的$_GET进行不同的缓存
二、清除缓存
clear_all_cache();//清除所有缓存 clear_cache('index.tpl');//清除index.tpl的缓存 clear_cache('index.tpl',cache_id);//清除指定id的缓存
三、使用自定义缓存方式
设置cache_handler_func使用自定义的函数处理缓存
如:
$smarty->cache_handler_func = "myCache"; function myCache($action, &$smarty_obj, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null){ }
该函数的一般是根椐$action来判断缓存当前操作:
switch($action){ case "read"://读取缓存内容 case "write"://写入缓存 case "clear"://清空 }
一般使用md5($tpl_file.$cache_id.$compile_id)作为唯一的cache_id
如果需要,可使用gzcompress和gzuncompress来压缩和解压
四、局部关闭缓存
要在某些区域使缓存失效(只对需要的缓存),有几种方法:
insert:
定义一个insert标签要使用的处理函数,函数名格式为:insert_xx(array $params, object &$smarty)其中的xx是insert的name,也就是说,如果你定义的函数为insert_abc,则模板中使用方法为{insert name='abc'}
参数通过$params传入
也可以做成insert插件,文件名命名为:insert.xx.php,函数命名为:smarty_insert_aa($params,&$smarty),xx定义同上
register_block:
定义一个block:
smarty_block_name($params,$content, &$smarty){return $content;} //name表示区域名
注册block:
$smarty->register_block('name', 'smarty_block_name', false); //第三参数false表示该区域不被缓存
模板写法:
{name}内容{/name}
写成block插件:
1)定义一件插件函数:block.cacheless.php,放在smarty的plugins目录
block.cacheless.php的内容如下:
<?php function smarty_block_cacheless($param, $content, &$smarty) { return $content; } ?>
2) 编写程序及模板
示例程序:testCacheLess.php
<?php include('Smarty.class.php'); $smarty = new Smarty; $smarty->caching=true; $smarty->cache_lifetime = 6; $smarty->display('cache.tpl'); ?>
所用的模板:cache.tpl
已经缓存的: {$smarty.now}<br> {cacheless} 没有缓存的: {$smarty.now} {/cacheless}
更多关于PHP相关内容感兴趣的读者可查看本站专题:《smarty模板入门基础教程》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
