In WEB development, the most effective way to cope with high traffic is to use the cache technology to effectively improve server load performance. in exchange for space, cache refers to a temporary file exchange zone, the computer puts the most commonly used files in the memory for temporary caching, just like moving tools and materials to the workbench, which is more convenient than getting them from the warehouse. Because the cache often uses RAM (non-permanent storage after power failure), the files will still be sent to the hard disk and other storage for permanent storage after busy. The largest cache in the computer is the memory, the fastest is the L1 and L2 cache on the CPU, the graphics card is the cache for GPU, the hard disk also has 16 m or 32 M Cache. Never regard the cache as a thing. it is a collectively referred to as a processing method!
In WEB development, the most effective way to cope with high traffic is to use the cache technology to effectively improve server load performance and exchange space for time.
The Internet is also a 80% conclusion. just like keywords in Baidu search, 20% of people search for of the content, therefore, you only need to store the 20% keyword content to quickly return the desired content to users in billions of records.
In this article, we will look at some of the memory storage methods commonly used in php web development.
1. general cache technology:
Data cache: the data cache here refers to the PHP Cache mechanism for database queries. each time you access the page, it first checks whether the cache data exists. if it does not exist, it connects to the database, obtain the data and serialize the query results and save them to the file. the same query results will be obtained directly from the cache table or file.
The most widely used example is the Discuz search function. The result ID is cached in a table and the table is searched in the cache next time when the same keywords are searched.
For example, when multiple tables are joined, an array generated in the appendix is saved to a field in the master table. if necessary, the array is decomposed, the advantage is read-only tables. The disadvantage is that two data synchronization steps are many more steps, and the database is always the bottleneck. Changing the disk speed is the key point.
2. page cache:
Each time you access the page, the system first checks whether the corresponding cached page file exists. if it does not exist, it connects to the database to obtain data, displays the page, and generates cache page files at the same time, in this way, the page file will play a role in the next visit. (The template engine and some common PHP Cache mechanism classes on the Internet usually have this function)
3. cache triggered by time:
Check whether the file exists and the timestamp is earlier than the set expiration time. if the modified timestamp of the file is greater than the current timestamp minus the expiration timestamp, use the cache; otherwise, update the cache.
4. content-triggered cache:
When data is inserted or updated, the PHP Cache is forcibly updated.
5. static cache:
The static cache mentioned here refers to static, which directly generates HTML, XML, and other text files. it is re-generated once when there is an update. this is suitable for pages that do not change much.
The above content is a code-level solution. I am not too lazy to change the content of the CP framework. the content is similar and easy to implement. it can be used together in several ways, however, the following content is a server-side cache solution. non-code-level solutions can only be achieved through multi-party cooperation.
6. memory cache:
Memcached is a high-performance, distributed memory object PHP Cache mechanism system that reduces database loads in Dynamic Applications and improves access speed.
7. php buffer:
There are eaccelerator, apc, phpa, and xcache. you don't need to talk about this. you can search for a bunch of them and read them by yourself. if you know what it is, OK.
8. MYSQL cache:
This is not code-level. the classic database uses this method. See the following running time, such as 0.09xxx.
9. reverse proxy-based Web cache:
For example, Nginx, SQUID, mod_proxy (apache2 and above are also divided into mod_proxy and mod_cache)
10. DNS round robin:
BIND is an open-source DNS server software, which is too big to be mentioned. you can search for it by yourself.
I know that chinacache and other major sites are doing this. Simply put, they are multiple servers and cache the same page or file on different servers, it is automatically resolved to the relevant server by north and south.
Why the cache technology? The reason is simple: improve efficiency. In program development, the primary way to obtain information is to query the database. In addition, it may also be through Web Services or some other method. no matter which method, in the face of a large number of concurrent accesses, they may all become efficiency bottlenecks. to solve these problems, many solutions are proposed, some of which use optimization software (such as APC, Eaccelerator, Zend Optimizer, and so on) to improve the running efficiency of the program, the rational use of these software can often improve the running efficiency of the program by an order of magnitude, but the premise is that you must control the host, so that you can install the software. if you are using a virtual host, you can only pray that your service provider has pre-installed an optimization software, otherwise, you must use PHP to implement the corresponding cache function. If this makes you feel at a loss, I believe the text below can give you some inspiration.
Many PHP programmers use gold partners such as Adodb + Smarty, so they should first look at how to use their cache functions.
First, let's take a look at the data cache function provided by adodb:
Include ('adodb. inc. php'); # load code common to adodb
$ ADODB_CACHE_DIR = '/usr/ADODB_cache ';
$ Conn = & ADONewConnection ('mysql'); # create a connection
$ Conn-> PConnect ('localhost', 'userid', '', 'agora '); # connect to MySQL, agora db
$ SQL = 'SELECT mermername, CustomerID from customer ';
$ Rs = $ conn-> CacheExecute (15, $ SQL );
?>
As shown above, each time data is queried, the corresponding results are serialized and saved to the file. in the future, the same query statement can be obtained from the cache file instead of directly querying the database.
Let's take a look at the page cache function provided by Smarty:
Require ('smarty. class. php ');
$ Smarty = new Smarty;
$ Smarty-> caching = true;
If (! $ Smarty-> is_cached ('index. tpl ')){
// No cache available, do variable assignments here.
$ Contents = get_database_contents ();
$ Smarty-> assign ($ contents );
}
$ Smarty-> display ('index. tpl ');
?>
As shown above, each time you access the page, the system first checks whether the corresponding cache exists. if it does not exist, it connects to the database and obtains the data. after the template variable value is assigned, the page is displayed, the cache file is generated at the same time, so that the cache file will play a role the next time you access it, instead of executing the if block data query statement. Of course, there are many things to consider in actual use, such as setting the validity period and cache group. for details, refer to the related sections on cache (caching) in the Smarty manual.
The two popular PHP component caching methods have different focuses. for the Adodb cache, it caches data and for the Smarty cache, it caches pages. There are many other components that provide the caching function (for example, PEAR: Cache_Lite). the specific analysis of the specific solution used in actual programming may also be used in combination.
One obvious advantage of using the built-in caching scheme of these components is that their implementation is transparent to the client. You only need to make the necessary settings (such as the cache time and cache directory), instead of having to think too much about the cache details, the system will automatically manage the cache according to the settings. However, its disadvantages are also obvious, because every request still needs to be parsed using PHP, and the efficiency is still greatly reduced compared with pure static, which still cannot meet the requirements in the face of large PVS. in this case, static caching is required because dynamic caching is not enough.
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.