Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement caching technology in PHP

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "how to achieve caching technology in PHP", the content is easy to understand, clear, hope to help you solve your doubts, let the editor lead you to study and learn "how to achieve caching technology in PHP" this article.

1. Full-page static cache means that all pages are generated into html static pages, which users visit directly when they visit, instead of going through the process of php server parsing. This method is common in CMS systems, such as dedecms

A more common implementation is to use output caching:

2, page partial caching this way is to cache the infrequent parts of a page statically, while frequently changing blocks are not cached and finally assembled together for display; you can use a way similar to ob_get_contents, or you can use page fragment caching strategies such as ESI to cache relatively static fragments in dynamic pages (ESI technology, please baidu, not detailed here).

This method can be used in merchandise pages such as shopping malls.

3. Data cache, as the name implies, is a way to cache data. For example, when a commodity information in a mall is requested with a commodity id, it will get data including store information, commodity information and so on. At this time, the data can be cached in a php file, and the file name contains the commodity id to create a unique label. The next time someone wants to view this item, they first directly call the information in the file instead of querying it in the database; in fact, the cache file is a php array and so on.

This method is used in the Ecmall Mall system.

4. Query caching actually has the same idea as data caching, that is, caching the data obtained by the query statement; cache the data obtained by the query in a file, and the next time you encounter the same query, you will directly transfer the data from this file instead of checking the database; but the cache file name here may need to use the query statement as the basis to establish a unique label.

Caching by time change

In fact, this is not a real caching method; the caching techniques of 2, 3 and 4 above generally use time change judgment; that is, you need to set a valid time for the cache file, within which time the same access will fetch the contents of the cache file first, but if you exceed the set cache time, you need to retrieve the data from the database and produce the latest cache file. For example, I set the home page of our mall to update every 2 hours.

5. Caching according to content change is not an independent caching technology and needs to be used together; that is, when the content of the database is modified, the cache file is updated immediately; for example, in a mall with a large flow of people, there are many goods, and the commodity table must be relatively large, and the pressure on this table is also heavy; we can cache the page of the product display page.

When the merchant modifies the information of the product in the background, click Save, and we update the cache file at the same time; then, when the buyer accesses the product information, he actually visits a static page instead of the database.

Just imagine, if the product page is not cached, then every time you visit a product, you have to check it in the database. If there are 100000 people browsing the product online, the server will be under great pressure.

6, memory cache mentioned this, perhaps the first thing that comes to mind is that Memcached;memcached is a high-performance distributed memory cache server. The purpose of general use is to reduce the number of database visits by caching database query results, so as to improve the speed and scalability of dynamic Web applications.

It is to cache the information that needs to be cached in the system memory, and when you need to get the information, it will be fetched directly to the memory. The more common way is key- > value.

7. After the apache cache module apache is installed, it is not allowed to be cache. If an external cache or squid server requires web acceleration, it needs to be set in htttpd.conf, of course, as long as the mod_cache module is activated when installing apache.

When installing Apache:. / configure-enable-cache-enable-disk-cache-enable-mem-cache

8. Php APC cache extension Php has an APC cache extension. Under windows is php_apc.dll. You need to load this module first, and then configure it in php.ini:

9. Opcode cache We know that the execution process of php can be shown in the following figure:

First of all, the php code is parsed into Tokens, and then compiled into Opcode code, and finally the Opcode code is executed to return the result; therefore, for the same php file, the Opcode code can be cached the first time, and the next time this page is executed, it will directly find the cached opcode code and perform the last step directly, without the need for the intermediate step. The more famous ones are XCache, Turck MM Cache, PHP Accelerator and so on.

The above is all the contents of the article "how to implement caching technology in PHP". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report