ThinkPHP cache fast cache and dynamic cache
thinkPHP's F method can only be used to cache simple data types and does not support validity periods and cached objects. The S() cache method supports validity period, also known as dynamic cache method. This article is the editor’s daily compilation of thinkphp caching methods. Friends who are interested in thinkphp caching methods should learn together.
The system’s default caching method is File caching. We can define other caches in the project configuration file. Method, for example, modify the default caching method to Xcache (of course, your environment needs to support Xcache)
For the problem of a large number of files in the cache directory under the File mode cache due to too much cached data, ThinkPHP A solution is also given to enable hash subdirectory caching.
'DATA_CACHE_SUBDIR'=>true
You can also set the level of the hash directory. For example,
'DATA_PATH_LEVEL'=>2
can automatically create multi-layer subdirectories for caching based on the hash of the cache identifier.
The S method supports cache validity period. In many cases, we may not need the concept of validity period, or using file mode caching can meet the requirements, so the system also provides a fast cache specifically for file mode. Cache method F method. The F method can only be used to cache simple data types, and does not support validity periods and cache objects. Use the following:
//快速缓存Data数据,默认保存在DATA_PATH目录下面
F('data',$data);
//快速缓存Data数据,保存到指定的目录
F('data',$data,TEMP_PATH);
F('user/data',$data);
//删除缓存数据
F('data',null);
//获取缓存数据
$data=F('data');Configuration file config.php
//动态缓存,缓存文件存在于\Runtime\Temp 'DATA_CACHE_TYPE'=>'file', 'DATA_CACHE_TIME'=>'3600', //'DATA_CACHE_SUBDIR'=>true,//开启子目录 //'DATA_CACHE_LEVEL'=>3,//设置子目录的层次
Action file:
function view(){
//缓存
//$cache=Cache::getInstance('缓存方式','缓存参数');
//$cache=Cache::getInstance('Xcache',array('expire'=>60));
//$cache->set('名字','值');或者$cache->name='值';
//$value=$cache->get('名字');或者$value=$cache->name;
//$cache->rm('名字');或者unset($cache->name);
//S('名字','数据','3600','类型')缓存快捷方法
$user=M('haodetong');
$value=S('list');
if(empty($value)){
$list=$user->select();
S('list',$list,3600);
echo '这个是直接从数据库中读取的文件';
dump($list);
}else{
echo '这个是缓存文件';
dump($value);
}
} As shown below when visiting for the first time:

As shown after refreshing again:

The following is a separate introduction to the fast caching of the F method in ThinkPHP
The file mode caching can meet the requirements, so the system also provides a fast caching dedicated to the file mode. Cache method F method
$path="../Public/Runtime/";
$str="asdfasdfasdaaaaaaaaaaaaaaaaaaaaaa";
F("str/andy",$str,$path);In this way, the content of the $str string is placed in the file of ../Public/Runtime/str/andy.php and the file of
andy.php The content is as follows:
<?php return 'asdfasdfasdaaaaaaaaaaaaaaaaaaaaaa'; ?>
The following code is an overview of ThinkPHP caching method S()
thinkPHP’s F method can only be used to cache simple data Type, expiration and cache objects are not supported. The S() cache method supports validity period, also known as the dynamic cache method. The usage example is as follows:
The code is as follows:
// 使用data标识缓存$Data数据 S('data',$Data); //前面的是缓存标示,后面的是缓存的数据
The code is as follows:
// 缓存$Data数据3600秒 S('data',$Data,3600);
The code is as follows:
// 删除缓存数据 S('data',NULL); //第一个参数时缓存的标识名
The code is as follows:
$cache=S($cachename);//设置缓存标示
// 判断是否有这个查询缓存
if(!$cache){ //$cache 中是缓存的标示(每个查询都对应一个缓存 即 不同的查询有不同的缓存)
$cache=$video->where($map)->order($order)->limit($limit)->select();
foreach($cache as $key=>$value){
$userlist=$user->where("id=".$value['user_id'])->find();
$cache[$key]["nickname"]=$userlist['nickname'];
}
S($cachename,$cache,3600); //设置缓存的生存时间
}
S($cachename,NULL); //删除缓存The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
Analysis on ThinkPHP’s implementation of static caching and dynamic caching
##
The above is the detailed content of ThinkPHP cache fast cache and dynamic cache. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
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)
How to work with arrays in php
Aug 20, 2025 pm 07:01 PM
PHParrayshandledatacollectionsefficientlyusingindexedorassociativestructures;theyarecreatedwitharray()or[],accessedviakeys,modifiedbyassignment,iteratedwithforeach,andmanipulatedusingfunctionslikecount(),in_array(),array_key_exists(),array_push(),arr
WordPress Custom Article Type Button Popup Form with AJAX Submission Tutorial
Aug 08, 2025 pm 11:09 PM
This tutorial provides detailed instructions on how to add a "Submit Quotation" button to each article in WordPress in a custom article type list. After clicking, a custom HTML form with the article ID pops up, and the form data is AJAX submission and success message display. The content covers front-end jQuery UI pop-up settings, dynamic data transfer, AJAX request processing, as well as back-end WordPress AJAX hook and data processing PHP implementation, ensuring complete functions, secure and good user experience.
How to use the $_COOKIE variable in php
Aug 20, 2025 pm 07:00 PM
$_COOKIEisaPHPsuperglobalforaccessingcookiessentbythebrowser;cookiesaresetusingsetcookie()beforeoutput,readvia$_COOKIE['name'],updatedbyresendingwithnewvalues,anddeletedbysettinganexpiredtimestamp,withsecuritybestpracticesincludinghttponly,secureflag
Compare and contrast PHP Traits, Abstract Classes, and Interfaces with practical use cases.
Aug 11, 2025 pm 11:17 PM
Useinterfacestodefinecontractsforunrelatedclasses,ensuringtheyimplementspecificmethods;2.Useabstractclassestosharecommonlogicamongrelatedclasseswhileenforcinginheritance;3.Usetraitstoreuseutilitycodeacrossunrelatedclasseswithoutinheritance,promotingD
Describe the Observer design pattern and its implementation in PHP.
Aug 15, 2025 pm 01:54 PM
TheObserverdesignpatternenablesautomaticnotificationofdependentobjectswhenasubject'sstatechanges.1)Itdefinesaone-to-manydependencybetweenobjects;2)Thesubjectmaintainsalistofobserversandnotifiesthemviaacommoninterface;3)Observersimplementanupdatemetho
WordPress Custom Article Button Popup Form with AJAX Submission Guide
Aug 08, 2025 pm 11:06 PM
This tutorial details how to add a Submit Quotation button to the list item of each custom post type (such as "Real Estate") in WordPress, and a custom HTML form with a specific post ID pops up after clicking it. The article will cover how to create modal popups using jQuery UI Dialog, dynamically pass the article ID through data attributes, and use WordPress AJAX mechanism to implement asynchronous submission of forms, while processing file uploads and displaying submission results, thus providing a seamless user experience.
Explain database indexing strategies (e.g., B-Tree, Full-text) for a MySQL-backed PHP application.
Aug 13, 2025 pm 02:57 PM
B-TreeindexesarebestformostPHPapplications,astheysupportequalityandrangequeries,sorting,andareidealforcolumnsusedinWHERE,JOIN,orORDERBYclauses;2.Full-Textindexesshouldbeusedfornaturallanguageorbooleansearchesontextfieldslikearticlesorproductdescripti
Implement pop-up form and AJAX submission for each custom post button in WordPress
Aug 08, 2025 pm 10:57 PM
This tutorial will provide detailed instructions on how to implement a pop-up submission form in WordPress for a standalone button for each custom post (such as the "Real Estate" type). We will use jQuery UI Dialog to create modal boxes and dynamically pass the article ID through JavaScript. Additionally, the tutorial will cover how to submit form data via AJAX and handle backend logic without refreshing the page, including file uploads and result feedback.


