search
HomeBackend DevelopmentPHP Tutorialphp—Smarty-缓存一(25)

php—Smarty-缓存1(25)

一、            缓存原理:

IE:将资源文件保存至本地

Smarty:将缓存保存到服务器

编译     

二、            为什么使用缓存技术

1、提升网站访问速度

2、减轻web服务器压力

3、减轻数据库服务器压力

三、            设置缓存

1、在smarty中,设置缓存主要通过以下几行代码:

l  $smarty->setCacheDir($cache_dir)

设置缓存目录

将来生成的缓存文件会自动放在这个目录下

l  $smarty->caching=true

开启缓存机制

只要使用想缓存技术,必须在display方法调用之前开启缓存机制,(清除缓存不要开启)

l  $smarty->cach_lifetime=3600

设置缓存有效时间(生命周期),

默认是3600秒(一小时),可以做适当调整

2、代码部分:

程序代码:

模板代码:

运行以上程序之后,我们发现在当前目录下,自动产生两个文件夹:

其中templates_c和cache分别表示编译和缓存目录,并且它们是自动创建的

但是强烈建议大家,不要过度依赖自动创建功能,因为在有些操作系统中,默认没有创建权限,所以建议我们在运行之前手动创建这两个目录

3、观察这两个文件区别:

编译文件:

缓存文件:

通过观察,我们发现,在编译文件中,关于模板变量的值是每次通过PHP程序动态读取的

在缓存文件中,这两个变量的值是直接写到文件中的

4、缓存是指针对模板的缓存

我们可以尝试创建其它php程序,让这些程序再次显示上面这个模板,发现这些程序只会直接运行以上程序产生的缓存文件

5、Smarty缓存工作原理:

6、两个相关的属性

$smarty->compile_check

默认值为true,表示每次执行时,都会检查模板是否改变,如果改变,则重新读取,再生成编译和缓存文件。但是,一个项目上线之后,模板基本上是不会有改动的或不经常动的,每次都判断会影响系统性能,所以在项目上线之后,建议大家关闭此选项(设置为false)。

$smarty->force_compile

默认值是false,表示每次执行时,都会检查缓存是否过期,如果没有过期,则直接使用缓存文件的内容。但是,一个项目开发时,缓存会影响程序员对程序最直接的判断,会影响观察最新的数据。建议大家将此选项设置为true,表示每次运行时都重新生成编译和缓存文件。

四、检查缓存

$smarty->isCached(“tpl.tpl”)

检查某个模板是否具有缓存文件

 

实际应用:

五、            清除缓存

比如说,在一个商城系统里,这件商品价格是1000.00,现在商城打折,打七折。如果使用缓存,但商品价格仍然是原来的价格。这时需要我们将以前的缓存文件删除即可。

l  $smarty->clearCache(“tpl.tpl”)

清除某个模板的缓存文件

l  $smarty->clearAllCache()

清除所有模板的缓存文件

一个商品信息的改变了,需要重新生成该商品的缓存文件

一般情况下,修改数据,是需要重新生成缓存文件(要把原来的缓存文件删除).

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

How do you optimize PHP applications for performance?How do you optimize PHP applications for performance?May 08, 2025 am 12:08 AM

TooptimizePHPapplicationsforperformance,usecaching,databaseoptimization,opcodecaching,andserverconfiguration.1)ImplementcachingwithAPCutoreducedatafetchtimes.2)Optimizedatabasesbyindexing,balancingreadandwriteoperations.3)EnableOPcachetoavoidrecompil

What is dependency injection in PHP?What is dependency injection in PHP?May 07, 2025 pm 03:09 PM

DependencyinjectioninPHPisadesignpatternthatenhancesflexibility,testability,andmaintainabilitybyprovidingexternaldependenciestoclasses.Itallowsforloosecoupling,easiertestingthroughmocking,andmodulardesign,butrequirescarefulstructuringtoavoidover-inje

Best PHP Performance Optimization TechniquesBest PHP Performance Optimization TechniquesMay 07, 2025 pm 03:05 PM

PHP performance optimization can be achieved through the following steps: 1) use require_once or include_once on the top of the script to reduce the number of file loads; 2) use preprocessing statements and batch processing to reduce the number of database queries; 3) configure OPcache for opcode cache; 4) enable and configure PHP-FPM optimization process management; 5) use CDN to distribute static resources; 6) use Xdebug or Blackfire for code performance analysis; 7) select efficient data structures such as arrays; 8) write modular code for optimization execution.

PHP Performance Optimization: Using Opcode CachingPHP Performance Optimization: Using Opcode CachingMay 07, 2025 pm 02:49 PM

OpcodecachingsignificantlyimprovesPHPperformancebycachingcompiledcode,reducingserverloadandresponsetimes.1)ItstorescompiledPHPcodeinmemory,bypassingparsingandcompiling.2)UseOPcachebysettingparametersinphp.ini,likememoryconsumptionandscriptlimits.3)Ad

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools