As the name suggests, the file cache to memory cache is to convert the data stored in the file to the memory, so that disk operations can be converted to memory operations, which can greatly improve the data access speed, and distributed deployment of cache data. For more information about file caching and memory caching, see Glossary.
Preface As the name suggests, the file cache to memory cache is to convert the data stored in the file to the memory, so that disk operations can be converted to memory operations, which can greatly improve the data access speed, and distributed deployment of cache data. For more information about file caching and memory caching, see Glossary.
Principle The principle of transferring file cache to memory cache is to transfer data in the file cache to the memory to achieve global data sharing and solve the problem of frequent File loading and data loading, use the Memcache tool to cache data in memory.
Implementation mechanism and steps 1. check whether the file has a memory cache. if the file does not exist, load the cached file.
2. load the cached file and obtain the data in the cached file.
3. write the data in the cached file to the memory.
4. get data from memory and register the data as a global variable
5. return data
This process mainly deals with two problems. The first problem is cache file loading. to convert the file cache to the memory cache, a unified file cache path scheduling service is required, whether the file implements the memory cache mechanism. The second problem is how to register global variables, whether it is getting data from the file or in the memory, if you need to implement universal, you need a mechanism to register global variables.
The file cache path scheduling service is easy to implement, but the cost is relatively high, because the current file cache loading path needs to be reconstructed.
Normal file cache loading method
Require 'cache/config. php ';
File Cache path scheduling
Require getCachePath ('cache/config. php ');
In the preceding example, the getCachePath () function implements the file cache path scheduling service.
The second problem is the registration of global variables. we need to consider the global universality of data. Therefore, there are many precautions in use. after research, we found the following two ways to register global variables:
1. Loop registration The code is as follows:
Foreach ($ vars as $ k =>$ v ){
$ GLOBALS [$ k] = $ v;
}
Advantage: Duplicate Keys ensure that the latest values are normal.
Disadvantage: the size of the $ vars array determines the execution time.
2. directly append
$ GLOBALS + = $ vars;
Advantage: no loop, direct operation
Disadvantage: data cannot be written if a key exists.
Summary File cache to memory cache the most important way to process File loading and global variable registration, in the actual application process, pay special attention to the different variable names in each cache file, if the variable name exists the same, data may overwrite data when multiple files are loaded.
In specific development practices, you can use the memory cache to directly use the memory cache. if a large number of files are loaded using the cache, consider implementing the file cache-to-memory cache mechanism.
Glossary File Cache stores the data obtained from the database into a file, so that you do not need to obtain the data from the database for the next time and directly obtain the data from the file, in this way, the data access speed can be improved. Therefore, file cache applications can be viewed in many open-source program codes.
Memory cache refers to storing data obtained from the database into the memory. Currently, it is widely used, such as Memcache.
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.