ThinkPHP data cache

Source: Internet
Author: User
ThinkPHP's data Cache function should be a very big feature, and has undergone many improvements during the period, mainly completed by the Cache class, and many caching methods are supported in the factory mode, currently, the File mode, Db database mode, and Shmop shared memory mode are supported.

ThinkPHP's data Cache function should be a very big feature, and has undergone many improvements during the period, mainly completed by the Cache class, and many caching methods are supported in the factory mode, currently, the following are supported:

File method

Db database mode

Shmop shared memory

Eaccelerator (Turck MMcache fork)

Memcache Memched

Apc APC

Apachenote Apache note

Sqlite SQLite

Xcache

For users, you do not need to understand the implementation details of a specific caching method. Instead, you can use a public interface to write and read cache data, including setting the validity period, the cache scope can be used at all levels of the framework, including database query cache, data object cache, and application custom cache. The following describes how to use the cache class:

  1. Import ("Think. Util. Cache ");
  2. // Obtain the cache instance. the type parameter indicates that the cache type listed above is File by default.
  3. $ Cache = Cache: getInstance ($ type );
  4. // Write cache
  5. $ Cache-> set ($ name, $ value, $ expire );
  6. // Read the cache
  7. $ Cache-> get ($ name );
  8. // Delete cache
  9. $ Cache-> rm ($ name );
  10. // Clear the cache
  11. $ Cache-> clear ($ name );

Generally, the cache is automatically managed, and expired cache is automatically cleared. Therefore, you do not need to clear the cache manually. after version 0.9.8, the S method is added to facilitate cache operations. for example, the preceding operations can be simplified:

  1. // Write cache data
  2. S ($ name, $ value, $ expire );
  3. // Read the cached data
  4. S ($ name );
  5. // Delete cache
  6. S ($ name, NULL );
  7. // You can also specify other cache operations.
  8. S ($ name, $ value, $ expire, $ type)

ThinkPHP supports automatic data object caching, which can be used without the need to enable dynamic data caching. In fact, the pre-defined operation methods of ThinkPHP core Action classes use the automatic data object caching function in large quantities. the first time a Vo object is read, the cache is automatically generated, refresh the page repeatedly or when other users read the data object, they do not need to access the database again. when a user modifies the data object, the system automatically deletes the cached data object, so that the cache is automatically regenerated next time. the automatic cache is only applicable to data objects. the automatic cache update function cannot be completed for data list objects.

The framework also supports the cache read/write count statistics function.

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.