


smarty local caching technology [source code analysis]_PHP tutorial
I have been using other template engines before. Today, when I was listening to the bkJia training course, I talked about the local cache of the smarty template engine. It felt very good. I combined my own understanding here and sighed. If there is anything wrong, place, I hope friends will reply. Let’s all learn together. At the same time, I would also like to thank Teacher Zhang for providing a very excellent learning platform for PHP beginners like us.
I discovered that smarty is such a powerful and magical thing, which makes me like it as much as the template engine I used before.
Because I enable smarty’s cache by default, but the data in some places is updated in real time or updates quickly, so it is not suitable for caching. In this way, local caching will be useful.
1. insert method
Define a function to display time:
<ol class="dp-c"> <li class="alt"><span><span class="keyword">function</span><span> insert_get_current_time(){ </span></span></li> <li> <span> </span><span class="vars">$timestamp</span><span>=</span><span class="func">empty</span><span class="keyword">empty</span><span>(</span><span class="vars">$timestamp</span><span>)?time():</span><span class="vars">$timestamp</span><span>; </span> </li> <li class="alt"> <span> </span><span class="vars">$timeoffset</span><span>=(int) </span><span class="string">+8</span><span>; </span> </li> <li> <span> </span><span class="keyword">return</span><span> </span><span class="vars">$ret</span><span>=</span><span class="func">gmdate</span><span>(</span><span class="string">"Y-n-j g:ia"</span><span>, </span><span class="vars">$timestamp</span><span> + </span><span class="vars">$timeoffset</span><span> * 3600); </span> </li> <li class="alt"><span>} </span></li> </ol>
Then in the template:
<ol class="dp-c"><li class="alt"><span><span>{insert name=</span><span class="string">"get_current_time"</span><span>} </span></span></li></ol>
In this way, every time you open the page, the real-time time will be displayed instead of the cached time. Note that the function name here must start with insert, and the name in the template corresponds to it.
This method is simple, but if the content to be displayed is a large piece, it should not be used.
2. Dynamic block method
<ol class="dp-c"> <li class="alt"><span><span class="comment">//部分缓存 </span><span> </span></span></li> <li> <span class="keyword">function</span><span> smarty_block_nocache(</span><span class="vars">$param</span><span>, </span><span class="vars">$content</span><span>, </span><span class="vars">$smarty</span><span>) </span> </li> <li class="alt"><span> { </span></li> <li> <span> </span><span class="keyword">return</span><span> </span><span class="vars">$content</span><span>; </span> </li> <li class="alt"><span>} </span></li> <li> <span class="vars">$smarty</span><span>->register_block(</span><span class="string">nocache</span><span>, </span><span class="string">smarty_block_nocache</span><span>, false); </span> </li> </ol>
In template:
<ol class="dp-c"> <li class="alt"><span><span>{nocache} </span></span></li> <li> <span>{</span><span class="vars">$smarty</span><span>.now} </span> </li> <li class="alt"><span>{/nocache} </span></li> </ol>
In this way, every time you refresh the page, the displayed time will be different.
3. Plug-in block method
Create a file in the Smarty/plugins directory
Block.nocache.php content is as follows:
<ol class="dp-c"><li class="alt"><span><span><?php </span></span></li><li><span class="keyword">function</span><span> smarty_block_nocache(</span><span class="vars">$param</span><span>, </span><span class="vars">$content</span><span>, </span><span class="vars">$smarty</span><span>) </span></li><li class="alt"><span>{ </span></li><li><span> </span><span class="keyword">return</span><span> </span><span class="vars">$content</span><span>; </span></li><li class="alt"><span>} </span></li><li><span>?> </span></span></li></ol>
This has the same effect as method 2, and the tags in the template are also the same. There is no need to register_block in the php file, which is very convenient.

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

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

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











MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

Xiaomi car software provides remote car control functions, allowing users to remotely control the vehicle through mobile phones or computers, such as opening and closing the vehicle's doors and windows, starting the engine, controlling the vehicle's air conditioner and audio, etc. The following is the use and content of this software, let's learn about it together . Comprehensive list of Xiaomi Auto app functions and usage methods 1. The Xiaomi Auto app was launched on the Apple AppStore on March 25, and can now be downloaded from the app store on Android phones; Car purchase: Learn about the core highlights and technical parameters of Xiaomi Auto, and make an appointment for a test drive. Configure and order your Xiaomi car, and support online processing of car pickup to-do items. 3. Community: Understand Xiaomi Auto brand information, exchange car experience, and share wonderful car life; 4. Car control: The mobile phone is the remote control, remote control, real-time security, easy

In September 23, the paper "DeepModelFusion:ASurvey" was published by the National University of Defense Technology, JD.com and Beijing Institute of Technology. Deep model fusion/merging is an emerging technology that combines the parameters or predictions of multiple deep learning models into a single model. It combines the capabilities of different models to compensate for the biases and errors of individual models for better performance. Deep model fusion on large-scale deep learning models (such as LLM and basic models) faces some challenges, including high computational cost, high-dimensional parameter space, interference between different heterogeneous models, etc. This article divides existing deep model fusion methods into four categories: (1) "Pattern connection", which connects solutions in the weight space through a loss-reducing path to obtain a better initial model fusion

Go language is a simple, efficient, and highly concurrency programming language. It is an open source language developed by Google. In the Go language, the use of spaces is very important, it can improve the readability and maintainability of the code. This article will introduce how to use spaces correctly in Go language and provide specific code examples. Why you need to use spaces correctly In the programming process, the use of spaces is very important for the readability and beauty of the code. Appropriate use of spaces can make code clearer and easier to read, thus reducing

Written above & The author’s personal understanding is that image-based 3D reconstruction is a challenging task that involves inferring the 3D shape of an object or scene from a set of input images. Learning-based methods have attracted attention for their ability to directly estimate 3D shapes. This review paper focuses on state-of-the-art 3D reconstruction techniques, including generating novel, unseen views. An overview of recent developments in Gaussian splash methods is provided, including input types, model structures, output representations, and training strategies. Unresolved challenges and future directions are also discussed. Given the rapid progress in this field and the numerous opportunities to enhance 3D reconstruction methods, a thorough examination of the algorithm seems crucial. Therefore, this study provides a comprehensive overview of recent advances in Gaussian scattering. (Swipe your thumb up

In the Go distributed system, caching can be implemented using the groupcache package. This package provides a general caching interface and supports multiple caching strategies, such as LRU, LFU, ARC and FIFO. Leveraging groupcache can significantly improve application performance, reduce backend load, and enhance system reliability. The specific implementation method is as follows: Import the necessary packages, set the cache pool size, define the cache pool, set the cache expiration time, set the number of concurrent value requests, and process the value request results.
