Table of Contents
smarty cache usage analysis, smarty cache usage
Home Backend Development PHP Tutorial smarty cache usage analysis, smarty cache usage_PHP tutorial

smarty cache usage analysis, smarty cache usage_PHP tutorial

Jul 13, 2016 am 10:11 AM
smarty usage cache

smarty cache usage analysis, smarty cache usage

This article analyzes the usage of smarty cache in detail. Share it with everyone for your reference. The specific analysis is as follows:

At first I thought smarty was just used to cover up PHP code functions, but later I learned that it also has the powerful function of template caching.

What is template caching? When we take out some data from the database and output it to the template, we access the database every time. But in fact, every database access is the same. If the traffic of the website Very large. This kind of repeated access is completely unnecessary and puts a lot of pressure on the database. smarty provides caching technology to solve this problem.

First use smarty’s cache, we have a few things to set up:

Enable caching

Copy code The code is as follows:
$Smarty->caching = true;

Set cache period

Copy code The code is as follows:
$Smarty->cache_lifetime = 30;

Set up cache visual inspection

Copy code The code is as follows:
$Smarty->cache_dir = './cache';

Then for the part of database access, we first make a judgment whether this part has been cached

Copy code The code is as follows:
if(!$Smarty->isCached('01.html')){//Judge whether it has been done After caching, if it has already been done, don’t go here and output the template directly
$conn = mysql_connect('localhost','root','root');
Mysql_query('set names utf8');
Mysql_query('use market');
$rs = mysql_query('select goods_id,goods_name,shop_price,add_time from goods where goods_id = ' . $goods_id,$conn);
$goods =array();
While($row = mysql_fetch_assoc($rs)){
         $goods[] = $row;
}  
echo 'Leave the database';
$Smarty->assign('goods);
}

But there is something to note in smarty's cache, that is, if you get the parameters from the address bar, it may affect the cache. Multiple different parameters only cache the first generated ones, so you need to use It is actually very simple to use single template and multiple cache technology, as long as
Copy code The code is as follows:
$Smarty->assign('goods',$goods);
A parameter is added here, This parameter is obtained from the address bar. Of course, when judging, you also need to add the parameter to determine whether it has been cached
Copy the code The code is as follows:
$Smarty->isCached('01.html',goods_id);

So how to delete this cache? It's very simple. You just need to call

Copy code The code is as follows:
$Smarty->clearCache( '01.html',$goods_id)
The second parameter is optional. If not filled in, all caches under this template will be deleted directly.
Finally, sometimes when debugging the program without caching, we can also set this parameter to temporarily stop caching:
Copy the code The code is as follows:
$Smarty->force_cache = true;

Finally, one thing to note is that the cache life cycle means that after this time, refreshing the page again will replace the old cache with the new cache. If a new cache is not generated, the old cache will not automatically Deleted, so in actual project development, if there are a lot of caches, it will actually affect the storage.

In fact, what we need to cache is only a small part of the HTML. Now many websites use memcached for caching.

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/928225.htmlTechArticlesmarty cache usage analysis, smarty cache usage This article analyzes the usage of smarty cache in detail. Share it with everyone for your reference. The specific analysis is as follows: At first I thought smarty was just for...
Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Where are video files stored in browser cache? Where are video files stored in browser cache? Feb 19, 2024 pm 05:09 PM

Which folder does the browser cache the video in? When we use the Internet browser every day, we often watch various online videos, such as watching music videos on YouTube or watching movies on Netflix. These videos will be cached by the browser during the loading process so that they can be loaded quickly when played again in the future. So the question is, in which folder are these cached videos actually stored? Different browsers store cached video folders in different locations. Below we will introduce several common browsers and their

How to view and refresh dns cache in Linux How to view and refresh dns cache in Linux Mar 07, 2024 am 08:43 AM

DNS (DomainNameSystem) is a system used on the Internet to convert domain names into corresponding IP addresses. In Linux systems, DNS caching is a mechanism that stores the mapping relationship between domain names and IP addresses locally, which can increase the speed of domain name resolution and reduce the burden on the DNS server. DNS caching allows the system to quickly retrieve the IP address when subsequently accessing the same domain name without having to issue a query request to the DNS server each time, thereby improving network performance and efficiency. This article will discuss with you how to view and refresh the DNS cache on Linux, as well as related details and sample code. Importance of DNS Caching In Linux systems, DNS caching plays a key role. its existence

Usage of WPSdatedif function Usage of WPSdatedif function Feb 20, 2024 pm 10:27 PM

WPS is a commonly used office software suite, and the WPS table function is widely used for data processing and calculations. In the WPS table, there is a very useful function, the DATEDIF function, which is used to calculate the time difference between two dates. The DATEDIF function is the abbreviation of the English word DateDifference. Its syntax is as follows: DATEDIF(start_date,end_date,unit) where start_date represents the starting date.

The relationship between CPU, memory and cache is explained in detail! The relationship between CPU, memory and cache is explained in detail! Mar 07, 2024 am 08:30 AM

There is a close interaction between the CPU (central processing unit), memory (random access memory), and cache, which together form a critical component of a computer system. The coordination between them ensures the normal operation and efficient performance of the computer. As the brain of the computer, the CPU is responsible for executing various instructions and data processing; the memory is used to temporarily store data and programs, providing fast read and write access speeds; and the cache plays a buffering role, speeding up data access speed and improving The computer's CPU is the core component of the computer and is responsible for executing various instructions, arithmetic operations, and logical operations. It is called the "brain" of the computer and plays an important role in processing data and performing tasks. Memory is an important storage device in a computer.

Will HTML files be cached? Will HTML files be cached? Feb 19, 2024 pm 01:51 PM

Title: Caching mechanism and code examples of HTML files Introduction: When writing web pages, we often encounter browser cache problems. This article will introduce the caching mechanism of HTML files in detail and provide some specific code examples to help readers better understand and apply this mechanism. 1. Browser caching principle In the browser, whenever a web page is accessed, the browser will first check whether there is a copy of the web page in the cache. If there is, the web page content is obtained directly from the cache. This is the basic principle of browser caching. Benefits of browser caching mechanism

Detailed explanation and usage introduction of MySQL ISNULL function Detailed explanation and usage introduction of MySQL ISNULL function Mar 01, 2024 pm 05:24 PM

The ISNULL() function in MySQL is a function used to determine whether a specified expression or column is NULL. It returns a Boolean value, 1 if the expression is NULL, 0 otherwise. The ISNULL() function can be used in the SELECT statement or for conditional judgment in the WHERE clause. 1. The basic syntax of the ISNULL() function: ISNULL(expression) where expression is the expression to determine whether it is NULL or

Advanced Usage of PHP APCu: Unlocking the Hidden Power Advanced Usage of PHP APCu: Unlocking the Hidden Power Mar 01, 2024 pm 09:10 PM

PHPAPCu (replacement of php cache) is an opcode cache and data cache module that accelerates PHP applications. Understanding its advanced features is crucial to utilizing its full potential. 1. Batch operation: APCu provides a batch operation method that can process a large number of key-value pairs at the same time. This is useful for large-scale cache clearing or updates. //Get cache keys in batches $values=apcu_fetch(["key1","key2","key3"]); //Clear cache keys in batches apcu_delete(["key1","key2","key3"]);2 .Set cache expiration time: APCu allows you to set an expiration time for cache items so that they automatically expire after a specified time.

Spring Boot performance optimization tips: create applications as fast as the wind Spring Boot performance optimization tips: create applications as fast as the wind Feb 25, 2024 pm 01:01 PM

SpringBoot is a popular Java framework known for its ease of use and rapid development. However, as the complexity of the application increases, performance issues can become a bottleneck. In order to help you create a springBoot application as fast as the wind, this article will share some practical performance optimization tips. Optimize startup time Application startup time is one of the key factors of user experience. SpringBoot provides several ways to optimize startup time, such as using caching, reducing log output, and optimizing classpath scanning. You can do this by setting spring.main.lazy-initialization in the application.properties file

See all articles