Analysis of ThinkPHP cache-fast cache (F method) and dynamic cache (S method) (daily arrangement) _ php instance

Source: Internet
Author: User
The F method of thinkPHP can only be used to cache simple data types, and does not support validity periods and cache objects. The S () cache method supports the validity period, also known as the dynamic cache method. This article is a daily compilation of thinkphp cache methods. If you are interested in the thinkphp cache method, learn about it. The default cache method of the system is File Cache, we can define other caching methods in the project configuration file. For example, modify the default caching method to Xcache (of course, your environment must 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 parameters'); // $ 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 Method $ 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 cache file'; dump ($ value );}}

The first access time is as follows:

Refresh again, as shown in the following figure:

Next we will introduce the fast caching of the F method in ThinkPHP separately.

Files can be cached to meet the requirements. Therefore, the system also provides a fast file caching method F.

$ Path = "/cms/tpl/Index/Public/Runtime /";
$ Str = "asdfasdfasdaaaaaaaaaaaaaaaaaaaa ";
F ("str/andy", $ str, $ path );

In this way, the $ str string is stored in the/cms/tpl/Index/Public/Runtime/str/andy. php file.

The content of the andy. php file is as follows:

<? Php
Return 'asdfasdfasdaaaaaaaaaaaaaaaaaaaaaaaa ';
?>

The following code is the ThinkPHP cache method s ().

The F method of thinkPHP can only be used to cache simple data types, and does not support validity periods and cache objects. The S () cache method supports the validity period, also known as the dynamic cache method. The example is as follows:

The Code is as follows:

The Code is as follows:


// Use the data identifier to cache $ Data data
S ('data', $ data); // The front is the cache identifier, followed by the cached Data

The Code is as follows:

// Cache $ Data for 3600 seconds
S ('data', $ data, 3600 );

The Code is as follows:

The Code is as follows:


// Delete cache data
S ('data', NULL); // The cached identifier when the first parameter is set.

The Code is as follows:

$ Cache = S ($ cachename); // you can specify the cache ID. // you can determine whether the query cache exists. if (! $ Cache) {// $ cache indicates the cache (each query corresponds to a cache, that is, different queries have different caches) $ 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); // sets the cache survival time} S ($ cachename, NULL); // deletes the cache

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.