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:
- Import ("Think. Util. Cache ");
- // Obtain the cache instance. the type parameter indicates that the cache type listed above is File by default.
- $ Cache = Cache: getInstance ($ type );
- // Write cache
- $ Cache-> set ($ name, $ value, $ expire );
- // Read the cache
- $ Cache-> get ($ name );
- // Delete cache
- $ Cache-> rm ($ name );
- // Clear the cache
- $ 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:
- // Write cache data
- S ($ name, $ value, $ expire );
- // Read the cached data
- S ($ name );
- // Delete cache
- S ($ name, NULL );
- // You can also specify other cache operations.
- 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.