thinkphp3.1 Cache configuration Settings Read

Source: Internet
Author: User
Tags configuration settings delete cache

The following yellow Word is the code I use:

Set cache name Zgg, content is Zongguagua, effective time 60 seconds

Cache (' Zgg ', "Zongguagua", 60);

Read cache

if (Cache (' Zgg ')) {
echo Cache (' Zgg ');
}else{
Echo ' has lapsed ';
}

Another way of writing

Set the cache type to file, valid for 60 seconds, there are a lot of cache types, such as what Xcacha, it seems to install plug-ins, now with no need

Cache (Array (' type ' = ' File ', ' expire ' =>60));
Cache (' Zgg ', ' Zongguagua ');

Read cache

if (Cache (' Zgg ')) {
echo Cache (' Zgg ');
}else{
Echo ' has lapsed ';
}

The following is a excerpt of the content

In the project, a reasonable use of the cache for performance is of great help. Thinkphp provides a convenient way to cache, including data caching, static caching, and query caching, including file mode, APC, Db, Memcache, Shmop, Sqlite, Redis, Dynamic Data cache types, including Eaccelerator and XCache, as well as customizable static cache rules, and provides a quick way to access operations.

Data caching

In thinkphp cache operations, in general, do not need to directly manipulate the cache class, because the system built-in cache operations are encapsulated, the 3.1 version of the recommended data caching method is the cache method, the basic usage is:
(3.1. The 2 version of the cache method has been incorporated into the S method, so the cache method is no longer recommended, use the S method directly, and is consistent with the cache method usage described below )

1 Cache initialization
    1. Cache(array(' type '= 'xcache ',' expire '));
Copy Code

The parameters that can be supported by the cache initialization differ depending on the caching method, and the commonly used parameters are:

Expire Cache expiration Time (seconds)
Prefix Cache identity Prefixes
Type Cache type

The core version is only supported by the file cache, and other caching methods support the need to download a separate cache driver and put it under the extend/driver/cache/of the system directory, otherwise there will be an error message that does not support the cache type.

Some caching methods have their own special parameters, such as the memcache cache, and other parameters that need to be configured:

    1. The cache(array(' type ' = ' memcache ',' host ' = '192.168.1.10 ', ' port '=' 11211 ',' prefix '= 'think ',' expire '=60 ));
Copy Code

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

2 Cache Settings
    1. Cache(' A ',$value);
Copy Code

The data is cached according to the parameters at the time of the cache initialization, and parameters can be changed when the cache is set, for example:

    1. Cache(' A ',$value,+); Cache data 300 seconds
Copy Code

Even change the previous cache mode or more parameters:

    1. Cache(' A ',$value,array(' type '= ' file ',' expire '= >)); //cache data in file mode for 300 seconds
Copy Code

If you use the above array method to pass in the parameter when the cache is set, it will affect the subsequent cache access.

3 Cache Reads
    1. $value = cache(' a ');
Copy Code

The cache reads the values of the preceding cache settings, which are affected by the parameters passed in when the cache is initialized or the cache is set.
Returns False if the cache identity does not exist or has expired, otherwise the cached value is returned.

4 Cache deletion
    1. Cache(' A ',null);
Copy Code

Delete the cache data that the cache identifies as name.

If you want to switch the cache mode, you can do the cache initialization again or use the following method:

  1. $cache = cache(array(' type ' = ' XCache ',' prefix '= ' think ') ,' expire '=+));
  2. $cache, name = ' value '; //Set cache
  3. $value = $cache, name; //Get cache
  4. unset($cache,name); //Delete cache
Copy Code

If you set a cache prefix, the corresponding cache operation will only be identified for the cache prefix and does not affect other caches.
The data cache can support the cache queue, simply to limit the number of caches, just specify the length parameter at initialization time:

    1. Cache(array(' type '= 'xcache ',' length '= ' =', ' expire ' ( =));
Copy Code

When the length parameter is set, only the last 100 cached data is cached.

Fast Cache

If you just want to cache some simple data in a file, and there is no concept of expiration, then the system also provides a quick caching method F can be used for faster operation.
Cache data quickly, saved by default in the Data_path directory

    1. F(' data ',$Data);
Copy Code

Cache data quickly and save to the specified directory

    1. F(' data ',$Data,temp_path);
Copy Code

Get Cached data

    1. $Data = F(' Data ');
Copy Code

Delete cached data

    1. F(' data ',NULL);
Copy Code

The F method supports automatic creation of cache subdirectories, caches data under the Data_path directory, and automatically creates if the user subdirectory does not exist:

    1. F(' User/data ',$Data);
Copy Code

3.1.2 Version start the F method supports the use of the wildcard bulk deletion feature, using the following:

    1. F(' user/* ',NULL);
Copy Code

Represents the deletion of Data_path. ' The data cache under the user/' directory.
The system built-in data field information cache is a fast caching mechanism.

Query cache

For data queries that don't require high timeliness, we can use the query caching feature to improve performance without having to cache and get it ourselves using caching methods.
The query caching feature supports all databases and supports all caching methods and expiration dates.
When using the query cache, you only need to call the model class's cache method, for example:

    1. $Model,cache(true),select();
Copy Code

If the cache (true) is used, the query cache is generated at the same time as the query is based on the current query SQL, which is cached by default in the Data_cache_type parameter setting (the system default is file-cached) and the cache validity period is data _cache_time the time that the parameter is set, you can also make a separate cache method and validity period for the query cache:

    1. $Model,cache(true,"XCache"),select();
Copy Code

Indicates that the current query cache is cached in XCache and the cache is valid for 60 seconds.
The same query, if the cache method is not used, does not fetch or generate any caches, even if the cache method was previously called.
The query cache is for internal invocation only, and if you want the query cache to be open to other program calls, you can specify a key for the query cache, for example:

    1. $Model, Cache(' cache_name '),select();
Copy Code

The contents of the query cache can be obtained externally through the S method.

    1. $value = S(' cache_name ');
Copy Code

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). The application can choose the cache mode and the cache expiration date as needed.

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.
Static rules are defined in the following way:

  1. ' html_cache_on '= =true, //Turn on static cache
  2. ' Html_file_suffix ' = ' . shtml ', //Set static cache suffix to. shtml
  3. ' html_cache_rules '= array(
  4.      ' actionname (lowercase) ' =>  array ( ' static rules ' ,   ' static cache validity period ' ,  ' additional rules '  
  5.      ' modulename (lowercase) ' =>  array ( ' static rules ' ,   ' static cache validity period ' ,  ' additional rules '  
  6.      ' modulename (lowercase): actionname (lowercase) ' Span class= "PLN" > => array ( ' static rules ' ,  ' static cache expiration ' ,  ' additional rules '
  7. ' * ' = array(' static rules ', ' static cache expiration ', ' additional rules '),
  8. //... Static rules for more operations
  9. )
Copy Code

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.
The first is to define a global operation static rule, such as a static rule that defines all of the read operations:

    1. ' read ' +=array(' {ID} ', ' $')
Copy Code

where {ID} means take $_get[' id '] as a static cache file name, the second parameter represents cache for 60 seconds
The second is to define the global module static rules, for example, the static rules that define all the user modules are:

    1. ' User: '= =array(' User/{:action}_{id} ', ' + ')
Copy Code

where {: action} indicates the current operation name
The third is a static rule that defines the operation of a module, for example, we need to define the read operation of the blog module for static caching

    1. ' Blog:read '= =array(' {ID} ',0)
Copy Code

There are individual special rules, such as the definition of static rules for empty modules and empty operations, which can be used in the following way:

    1. ' Empty:index '= =array(' {: module}_{:action} ',0) //define static rules for empty modules
    2. ' user:_empty '= =array(' user/{:action} ',0) //static rules for defining empty operations
Copy Code

The fourth way is to define the global static cache rules, which are used in special cases, and the operation of any module is applicable, for example

    1. ' * '= =array(' {$_server. REQUEST_URI|MD5} '),
Copy Code

Caching based on the current URL

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, and that the wording can include the following:
1. Use system variables including _get _request _server _session _cookie
Format:
{$_xxx|function}

For example:

    1. {$_get. Name}
    2. {$_server. Request_uri| MD5}
Copy Code

2. Use frame-specific variables
For example: {: App}, {: group}, {: module}, and {: action} represent the current project name, group name, module name, and action name, respectively.

3. Use _get variable
{Var|function} means {ID} is actually equivalent to {$_get.id}

4. Direct use of functions
{|function} for example:

    1. {| time}
Copy Code

5, support mixed definitions, for example, we can define a static rule as:

    1. ' {id},{name|md5} '
Copy Code

Characters outside of {} are treated as strings, and directories are created automatically if they contain "/".
For example, define the following static rule:

    1. {:module}/{:Action}_{id}
Copy Code

A subdirectory of the module name is created under the static directory, and then the operation name _id.shtml file is written.
The static valid time unit is seconds. If not defined, it gets the setting value of the configuration parameter html_cache_time, which is defined as 0 to indicate a permanent cache.
Additional rules are typically used to perform function operations on static rules, such as

    1. ' read ' +=array(' Think{id},{name} ', ' $ ', ' MD5 ')
Copy Code

The post-translation static rule is md5 (' Think '. $_get[' id '). ', '. $_get[' name ']);
Configuration parameters related to static cache include:

    1. Html_cache_on whether to turn on the static cache function
    2. the html_file_suffix static file suffix convention configures the value to be . HTML
    3. Html_cache_time default static cache validity period defaults to override in static rule definition
Copy Code

Summarize

To be good at using Thinkphp's caching capabilities, it is more important to differentiate between when and how the cache is more effective. Cache is not omnipotent, no cache is absolutely impossible ^_^

thinkphp3.1 Cache configuration Settings Read

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.