About thinkphp implementation of static cache and dynamic cache analysis

Source: Internet
Author: User
Tags delete cache
This article mainly introduces the thinkphp implementation of static cache and dynamic cache sample code, with a certain reference value, interested in small partners can refer to

Static caching

To use the static caching feature, you need to turn on the html_cache_on parameter and set the static cache rule file with the Html_cache_rules configuration parameter.

Defining static Rules

' html_cache_on '   +  true,//Turn on static cache ' Html_cache_time ',//  global Static cache lifetime (seconds) ' Html_file_ SUFFIX ' +  '. shtml ',//set static cache file suffix ' html_cache_rules ' = =   Array (//define static cache rule   //define format 1 array way   ' static address '  = =   Array (' Static rules ', ' expiration ', ' additional Rules '),    //Definition Format 2 string way   ' static address ' = '   static rule ',)

The root directory of a static cache file is under the path defined by Html_path, and only operations that have a static rule defined are statically cached. And the static cache supports different storage types. The static cache is only valid under GET requests.

Static Address

Global Operations Static Rules

' Read ' =>array (' {ID} ', 60)//define static rules for all read operations

Define a global controller static rule

' User: ' =>array (' User/{:action}_{id} ', ' 600 ')//define static rules for all user controllers

Static rules that define the operation of a controller//define the read operation of a blog controller for static caching

' Blog:read ' =>array (' {ID} ', 0)//parameter = 0 for permanent cache

Define a global static cache rule

' * ' =>array (' {$_server. REQUEST_URI|MD5} ')//This is a special case of use, the operation of any module is applicable

Static rules

Static rules are used to define the names of static files to be generated, and static rules are defined to ensure that they do not conflict.

Using System Variables

{$_xxx|function}//_get, _request, _server, _session, _cookie{$_get.name} {$_server. REQUEST_URI|MD5}

Variables that use framework customizations

{: Module}/{:controller}_{:action}//{:module}, {: Controller}, and {: action} represent the current module name, controller name, and operation name, respectively.

Using the _get variable

{Var|function}//{id} is actually equivalent to {$_get.id}

Using functions directly

{The |function}//{|time},time function gets the time after the file name

Mixed definition

' {ID},{NAME|MD5} '//characters outside of {} are treated as strings, and directories are created automatically if they contain "/". {: module}/{:action}_{id}//creates a subdirectory of the module name under the static directory, and then writes the operation name _id.shtml file.

Dynamic caching

[s method data cache]

Cache initialization

S (Array (' type ' = ' xcache ', ' expire ' =>60));

The types of caches currently supported by the system include: Apachenote, APC, Db, Eaccelerator, File, Memcache, Redis, Shmop, Sqlite, Wincache, and XCache. If the S method does not pass in the type parameter initialization, the Data_cache_type parameter value set in the configuration file is read as the default type. Similarly, if the prefix parameter is not passed in, the Data_cache_prefix parameter value of the configuration file is read, and the expire parameter is not passed in to read the Data_cache_time configuration value as the default.

For global caching, we generally recommend adding prefix (cache prefix) parameters to differentiate between different applications to avoid confusion.

Setting up the cache

The data cache can support the cache queue, simply to limit the number of caches, just specify the length parameter at initialization time.

S (' name ', $value); S (' name ', $value, 300);//cache data 300 seconds S (' name ', $value, Array (' type ' = ' file ', ' expire ' =>300));//Change the previous cache mode or more parameters s ( Array (' type ' = ' xcache ', ' length ' =>100, ' expire ' =>60));//The system caches only the last 100 cache data.

Read cache

$value = S (' name ');//returns False if the cache identity does not exist or has expired, otherwise the cached value is returned.

Delete Cache

S (' name ', null);

Note: When using each cache, you need to thinkphp load the corresponding driver files and set up the corresponding configuration.

Working with instances

$user = M (' user '), $value = S (' list '), if (Empty ($value)) {  $list = $user->select ();  S (' list ', $list, 3600);  Echo ' This is a file read directly from the database ';  Dump ($list);} else {  echo ' This is a cache file ';  Dump ($value);}

[Fast Cache]

The system also provides a quick cache method F can be used for faster operation, but the F method does not have an expiration date, the F method can support different storage types, if it is a file type, it is saved under the Data_path directory by default.

Cache data quickly

F (' data ', $Data); F (' Data ', $Data, Temp_path);//cache data data quickly, save to the specified directory F (' User/data ', $Data);//f method supports automatically creating cache subdirectories, caching data data under the Data_path directory, If the user subdirectory does not exist, it is created automatically.

Get Cached data

$Data = F (' Data ');

Delete cached data

F (' data ', NULL);

[Query Cache]

The query caching feature supports all databases and supports all caching methods and expiration dates.

When using the query cache, only the cache method of the model class needs to be called.

$Model->cache (True)->where (' Status=1 ')->select ();

If cache (TRUE) is used, a query cache with a unique identity is generated at the same time as the query based on information such as the current query condition, and if key is specified, the query cache with the name key is generated directly.

$Model->cache (' Cache_name ')->select ();

By default, the cache is cached in the Data_cache_type parameter setting (the system default is file representation cache), the cache validity period is the time of the Data_cache_time parameter setting, and the cache and expiration date of the query cache can be independently established.

$Model->cache (true,60, ' XCache ')->select ();

If you specify a key for the query cache, you can get the contents of the query cache directly outside the S method.

$value = S (' Cache_name ');

In addition to the Select method, the query cache supports the find and GetField methods, as well as their derived methods (including statistical queries and dynamic query methods).

$Model->where ($map)->cache (' key ', $)->find ();

This article is mainly from official documents, if you have questions see ThinkPHP3.2 official documentation-cache

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.