Simple analysis of thinkphp cache (F method) and dynamic caching (S-method) (routine collation) _php instance

Source: Internet
Author: User
Tags delete cache

The default caching method of the system is file caching, and we can define other caching methods in the project configuration file, for example, modifying the default caching method to XCache (of course, your environment needs to support XCache)

For cache directories under the file-mode cache because of the large number of file problems caused by too much cache data, thinkphp also gives a solution that enables the way Hashits directory caching can be enabled.

' Data_cache_subdir ' =>true

You can also set the level of a hash directory, such as

' Data_path_level ' =>2

You can automatically create a multi-tier subdirectory to cache based on the hash of the cached identity.

The S method supports cache expiration, in many cases, we may not need the concept of validity, or the use of the file way of caching can meet the requirements, so the system also provides a dedicated to file the way the fast caching method F method. The F method can only be used to cache simple data types, does not support expiration and cache objects, and is used as follows:

Cache data quickly, by default under Data_path directory
F (' data ', $data);
Cache data quickly and save to a specified directory
F (' Data ', $data, Temp_path);
F (' User/data ', $data);
Deleting cached data
F (' data ', NULL);
Get Cached data
$data =f (' data ');

Configuration file config.php

Dynamic caching, cached files exist in \runtime\temp
' Data_cache_type ' => ' file ',
' Data_cache_time ' => ' 3600 ',
' Data_cache_subdir ' =>true,//open subdirectories
' Data_cache_level ' =>3,//set the level of the subdirectory

Action file:

 Function view () {
    //$cache =cache::getinstance (' Caching mode ', ' 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 ', ' 3600 ', ' 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 read directly from the database ';  
      Dump ($list);
      Echo ' This is a cached file ';
      Dump ($value);

The first time you visit the following figure:

Refresh again after the following figure:

Here's a separate introduction to the fast caching of F methods in thinkphp

Caching with a file format satisfies the requirements, so the system also provides a fast caching method F method specifically for file mode

$path = ". /public/runtime/";
F ("Str/andy", $str, $path);

This puts the contents of the $STR string in the ... In the/public/runtime/str/andy.php file.

The contents of the andy.php document are as follows:


The following code is an overview of the thinkphp caching method s ()

The thinkphp F method can be used only for caching simple data types, not for expiration and caching of objects. The S () caching method supports the validity period, also known as the dynamic caching method, using the following example:

The code is as follows:

Copy Code code as follows:

To cache $data data using the data identity
S (' data ', $Data); The front is the cache label, followed by the cached data

The code is as follows:

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

The code is as follows:

Copy Code code as follows:

Deleting cached data
S (' data ', NULL); The identity name of the cache when the first parameter

The code is as follows:

$cache =s ($cachename);//Set cache mark
//To determine if there is this query cache if  
(! $cache) {//$cache is the cached indicator (each query corresponds to a cache i.e. 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 lifetime of the cache 
  S ($cachename, NULL);//Delete 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: 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.