ThinkPHP implements static cache and dynamic cache sample code, and thinkphp sample code

Source: Internet
Author: User
Tags delete cache

ThinkPHP implements static cache and dynamic cache sample code, and thinkphp sample code

Static Cache

To use the static cache function, you must enable the HTML_CACHE_ON parameter and use the HTML_CACHE_RULES parameter to set the static cache rule file.

Define static rules

'Html _ CACHE_ON '=> true, // enable static cache' HTML _ CACHE_TIME '=> 60, // Global static cache validity period (seconds) 'html _ FILE_SUFFIX '=> '.shtml ', // set the static cache file suffix 'html _ CACHE_RULES '=> array (// define static cache rules // define format 1 array method 'static address' => array ('static rules' ', 'validity set', 'additional rule'), // define format 2 string method 'static address' => 'static rule ',)

The root directory of the static cache file is under the path defined by HTML_PATH, and the static cache is only performed for operations that define static rules. Static cache supports Different Storage types. The static cache is only valid for GET requests.

Static address

Global static operation rules

'Read' => array ('{id}', 60) // defines static rules for all read Operations

Define Global static Controller Rules

'User: '=> array ('user/{: action }_{ id}', '000000') // defines static rules for all user Controllers

Define static rules for operations on a controller // define the read operations of the Blog controller for static Cache

'Blog: read' => array ('{id}', 0) // The parameter 0 indicates permanent cache.

Define Global static cache rules

'*' => Array ('{$ _ SERVER. REQUEST_URI | md5}') // This is a special case. Operations on any module are applicable.

Static rules

Static rules are used to define the name of the static file to be generated. do not conflict with the definition of static rules.

Use System Variables

{$_×××|function}//_GET、_REQUEST、_SERVER、_SESSION、_COOKIE{$_GET.name} {$_SERVER.REQUEST_URI|md5}

Use framework-Defined variables
Copy codeThe Code is as follows:
{: Module}/{: controller }_{: action} // {: module}, {: controller}, and {: action} indicates the current Module name, controller name, and operation name.

Use the _ GET variable

{Var | function} // {id} is equivalent to {$ _ GET. id}

Use functions directly

{| Function} // {| time}. The time function is used as the file name after obtaining the time.

Hybrid Definition

'{Id}, {name | md5}' // a character other than {} is treated as a string. If "/" is included, a directory is automatically created. {: Module}/{: action }_{ id} // The operation name _id.shtml file is created in the static directory.

Dynamic Cache

[S method data cache]

Cache Initialization

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

Currently, the system supports the following cache types: 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 will be read as the default type. Similarly, if the prefix parameter is not input, the DATA_CACHE_PREFIX parameter value of the configuration file will be read. If the expire parameter is not input, the DATA_CACHE_TIME configuration value will be read as the default value.

For the global cache method, we generally recommend that you add the prefix parameter to distinguish different applications to avoid confusion.

Set Cache

Data caching supports cache queues. Simply put, you can limit the number of caches. You only need to specify the length parameter during initialization.

S ('name', $ value); S ('name', $ value, 300); // The cached data is 300 seconds ('name', $ value, array ('type' => 'file', 'expire '=> 300 )); // change the previous caching method or more parameters S (array ('type' => 'xcache', 'length' => 100, 'expire '=> 60); // The system only caches the last 100 cached data records.

Read Cache

$ Value = S ('name'); // If the cache ID does not exist or has expired, false is returned; otherwise, the cache value is returned.

Delete Cache

S('name',null);

Note: When using each type of cache, ThinkPHP needs to load the corresponding driver file and set the corresponding configuration.

Use instance

$ User = M ('user'); $ value = S ('LIST'); if (empty ($ value) {$ list = $ User-> select (); S ('LIST', $ list, 3600); echo 'this is the file directly read from the database '; dump ($ list );} else {echo 'this is a cache file'; dump ($ value );}

[Quick cache]

The system also provides a fast cache method F that can be used for faster operations. However, method F does not have a validity period, and method F supports Different Storage types. If it is a file type, the data is stored in the DATA_PATH directory by default.

Cache Data quickly

F ('data', $ data); F ('data', $ Data, TEMP_PATH); // cache data quickly, save to the specified directory F ('user/data', $ data); // The F method supports automatic creation of cache subdirectories and caching Data under the DATA_PATH directory. If the User subdirectory does not exist, is created automatically.

Obtain cache data

$Data = F('data');

Delete cache data

F('data',NULL);

[Query cache]

The query cache function supports all databases and all cache methods and validity periods.

When using the query cache, you only need to call the cache method of the Model class.

$Model->cache(true)->where('status=1')->select();

If cache (true) is used, a query cache with a unique identifier is generated based on the current query conditions and other information. If the key is specified, the query cache named key is generated directly.

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

By default, the cache mode is set by the DATA_CACHE_TYPE parameter (the default value is File, indicating File-based Cache). The cache validity period is the time set by the DATA_CACHE_TIME parameter, you can also specify the cache method and validity period for querying the cache separately.

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

If the cache key is specified, you can directly obtain the cached content from the external query using the S method.

$value = S('cache_name');

In addition to the select method, the query cache also supports the find and getField Methods and Their Derivative methods (including statistical and dynamic query methods ).

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

This article is mainly from the official documentation. If you have any questions, please refer to ThinkPHP3.2 official documentation-Cache

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.