PHP File Cache to memory cache

Source: Internet
Author: User
Tags vars

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
Copy codeThe 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.

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.