Dynamic cache (S method) and quick cache (F method) in ThinkPHP)

Source: Internet
Author: User
Tags delete cache
: This article mainly introduces the dynamic cache (S method) and quick cache (F method) in ThinkPHP. if you are interested in the PHP Tutorial, refer to it. The default caching method is File caching. you can define other caching methods in the project configuration File. for example, you can modify the default caching method to Xcache (of course, your environment needs to support Xcache)
ThinkPHP also provides a solution for caching large volumes of files under the cache directory in the File mode, which enables the hash subdirectory.
'Data _ cache_subdir' => true

You can also set the level of the hash Directory, for example

'Data _ PATH_LEVEL '=> 2

You can automatically create multiple sub-directories to cache based on the hash of the cache ID.
The S method supports the cache validity period. in many cases, we may not need the validity period, or the file cache can meet the requirements, therefore, the system also provides a fast cache method F for files. The F method can only be used to cache simple data types, and does not support validity periods and cache objects. The following is used:

// Cache Data quickly. The Data is saved in the DATA_PATH directory by default.
F ('data', $ data );
// Cache Data quickly and save it to the specified directory
F ('data', $ data, TEMP_PATH );
F ('user/data', $ data );
// Delete cache data
F ('data', null );
// Obtain cache data
$ Data = F ('data ');
Configuration file config. php
// Dynamic cache. the cached file exists in \ Runtime \ Temp
'Data _ CACHE_TYPE '=> 'File ',
'Data _ CACHE_TIME '=> '123 ',
// 'Data _ cache_subdir' => true, // enable subdirectory
// 'Data _ CACHE_LEVEL '=> 3, // sets the sub-directory level
Action file:
Function view (){
// Cache
// $ Cache = Cache: getInstance ('cache method ', 'cache parameter ');
// $ Cache = Cache: getInstance ('xcache', array ('expire '=> 60 ));
// $ Cache-> set ('name', 'value'); or $ cache-> name = 'value ';
// $ Value = $ cache-> get ('name'); or $ value = $ cache-> name;
// $ Cache-> rm ('name'); or unset ($ cache-> name );

// S ('name', 'data', '123', 'type') cache shortcut

$ User = M ('haodetong ');

$ Value = S ('list ');
If (empty ($ value )){
$ List = $ user-> select ();

S ('list', $ list, 3600 );
Echo 'This is a file directly read from the database ';
Dump ($ list );
} Else {
Echo 'This is a cached file ';
Dump ($ value );
}

}
The first access time is as follows:

Refresh again, as shown in the following figure:

The above introduces the dynamic cache (S method) and quick cache (F method) in ThinkPHP, including some content, and hope to help friends who are interested in PHP tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.