Preface
As the name suggests, the file cache memory cache is to transfer the data stored in the file into memory, so that the disk operation into memory operation, which can greatly improve the speed of data access, and can realize the distributed deployment of cached data. For file caching and memory caching, refer to the explanation section of the noun.
principle
The principle of file cache transfer memory caching is to dump the data in the file cache into memory to realize the global sharing of data, solve the problem of frequently loading files and loading data, and implement the memory cache data with memcache tools.
implementation mechanism and steps
1, check the file for memory cache, if there is no load cache file
2, load the cached file and get the data in the cached file
3, writes the data in the cache file to the memory
4, get the data from memory and register the data as a global variable
5, return Data
This process is the most important to deal with two problems, the first problem is cache file loading, if you want to implement the file cache to memory cache, you need to have a unified file cache path scheduling service to implement whether the file implementation of memory caching mechanism. The second question is how to register a global variable, whether it's getting data from a file or getting it in memory, and if you need to implement universal, you need to have a mechanism for registering global variables.
File cache path Scheduling service This problem is simple to implement, but the cost is high, because the current file cache loading path needs to be refactored.
Normal file cache loading mode
Require ' cache/config.php ';
File Cache Path scheduling method
Require GetCachePath (' cache/config.php ');
In the example above, the GetCachePath () function implements the file cache path dispatch service.
The second is the registration of global variables, need to take into account the global universality of the data, so there are a lot of attention in use, after the study found that the following two kinds of registered global variable mode
1, Cycle registration
Copy Code code as follows:
foreach ($vars as $k => $v) {
$GLOBALS [$k] = $v;
}
Advantages: Repeat keys to ensure that the latest values are normal
Disadvantage: The size of the $vars array determines the execution time
2, Direct append
$GLOBALS + + $vars;
Advantages: No loops, direct operation
Disadvantage: If the key exists you cannot write
Summary
File cache to the memory cache is the most important processing file loading mode and global variable registration, in the actual application process, we should pay special attention to the different cache file variable name, if the variable name exists the same, it may be in multiple file loading process caused data coverage.
In the specific development practice, the memory cache can be used directly, and if there is a large number of file cache loading, consider the mechanism to implement the memory cache of file cache.
noun explanation
File caching refers to the data obtained from the database into the file, so that the next time to get the data from the database to get directly from the file, so you can improve the speed of access to data, so in many open source program code can see the application of file caching.
Memory cache refers to the data obtained from the database into memory, the current application is more extensive such as memcache.