File cache to memory cache method in PHP

  • 2020-05-10 17:47:37
  • OfStack

preface
As the name implies, file cache to memory cache is stored in the file data to memory to achieve disk operations into memory operations, which can greatly improve the speed of data access, and can achieve the distributed deployment of cache data. For an introduction to file caching and memory caching, refer to the definitions section.

The principle of
The principle of file cache to memory cache is to transfer the data in the file cache to memory, in order to realize global data sharing, to solve the problem of frequently loading files and loading data, using Memcache tools to achieve memory cache data.

Implementation mechanisms and steps
1. Check whether there is a memory cache in the file, if there is no load cache file
2. Load the cache file and get the data in the cache file
3. Write the data from the cache file to memory
4. Get data from memory and register the data as global variables
5. Return data

This process mainly deals with two problems, the first problem is the cache file loading, if you want to achieve file cache to memory cache, you need to have a unified file cache path scheduling service, to realize whether the file implements the memory cache mechanism. The second problem is how to register a global variable, either from a file or from memory. If you want to implement this, you need a mechanism to register a global variable.

The file cache path scheduling service is a simple but expensive implementation because you need to refactor the current file cache load path.

Normal file cache loading

require 'cache/config.php';

File cache path scheduling

require getCachePath('cache/config.php');

In the example above, the getCachePath() function implements the file cache path scheduling service.

Secondly, there is the problem of global variable registration, which needs to take into account the global universality of the data, so there are a lot of matters needing attention in the use. After research, it is found that there are two ways to register global variables as follows
1. Circular registration
 
foreach ( $vars as $k => $v ) { 
$GLOBALS[$k] = $v; 
} 

Advantage: you can keep the latest value normal in case of repeating the key
Cons: the size of the $vars array determines the execution time

2. Direct addition

$GLOBALS += $vars;

Advantages: no cycle, direct operation
Disadvantage: cannot write if the key exists

conclusion
File cache to memory cache mainly deals with file loading method and global variable registration. In the actual application process, special attention should be paid to the difference of variable names in each cache file. If the variable names are the same, data coverage may be caused in the loading process of multiple files.

In the concrete development practice, can use the memory cache to use the memory cache directly, at the same time, if there is a large number of file cache load, consider the implementation of the file cache to memory cache mechanism.


Noun explanation
File cache refers to the data obtained from the database is stored in the file, so that the next time to get the data does not need to get from the database but directly from the file, which can improve the data access speed, so in many open source program code can see the application of file cache.

Memory cache refers to the storage of data obtained from the database into memory. Currently, it is widely used, such as Memcache.

Related articles: