Current location - Education and Training Encyclopedia - Education and training - Beijing IT Training shares 9 caching technologies commonly used in PHP applications.
Beijing IT Training shares 9 caching technologies commonly used in PHP applications.
First, the whole page is statically cached.

In other words, all the pages are generated as html static pages, which can be accessed directly by users without parsing by php server. This method is very common in CMS systems, such as dedecms;;

A common implementation method is to use an output cache:

Ob _ start () * * * * * Code to be run * * * * $ content = ob _ get _ contents (); * * * * Write the cached content to the html file * * * * ob _ end _ clean ();

Second, data caching.

As the name implies, it is a way to cache data; For example, when requesting commodity information in a shopping center with a commodity id, data including store information and commodity information will be obtained. At this time, these data can be cached in a php file, and the file name contains the product id to build a unique tag; Next time someone wants to check this product, first check the information in this file directly, without going to the database. In fact, the cache file caches php arrays and the like;

This method is applied to Ecmall mall system.

Third, query cache.

In fact, this is an idea with data caching, that is, caching according to query statements; Cache the data obtained by the query in a file, and the next time you encounter the same query, you will directly call out the data from this file first, and you will not check the database again; However, the cache file name here may need to establish a unique label according to the query statement;

Time varying cache

That is, you need to set an effective time for the cache file, during which the same access will take the contents of the cache file first, but after the set cache time, you need to get data from the database again to generate the latest cache file; For example, I set the homepage of our mall to be updated every 2 hours.

Fourth, page partial caching.

In this way, the parts of a page that do not change frequently are statically cached, while the blocks that change frequently are not cached, and finally assembled and displayed together; It can be implemented in a way similar to ob_get_contents, or a page fragment caching strategy such as ESI can be used to cache the relatively static fragment part of a dynamic page.

This method can be used in, for example, the product page in the mall;

Fifth, the opcode cache.

First, php code is parsed into tokens, then compiled into operation code, and finally the operation code is executed to return the results. Therefore, for the same php file, you can cache its opcode code the first time you run it, and the next time you execute this page, you can directly find the cached opcode code and directly execute the last step without intermediate steps.

Well-known ones are XCache, TurckMMCache, PHPAccelerator and so on.

6. Cache according to content changes.

This is not an independent caching technology and needs to be combined; That is, when the database content is modified, the cache file is updated immediately;

For example, in a shopping mall with a large flow of people, there are many commodities, and the list of commodities is bound to be relatively large, and the pressure on this table is also relatively heavy; We can cache the product display page;

When the merchant modifies the product information in the background, click Save and we will update the cache file at the same time; Then, when the buyer accesses this product information, he actually asks a static page, and he doesn't need to visit the database again;

Imagine that if the product page is not cached, then every time you visit a product, you have to query it in the database. If there are 65438+ ten thousand people browsing products online, the pressure on the server will be great;

Seven, memory cache

Speaking of which, maybe the first thing that comes to mind is Memcached;; Memcached is a high-performance distributed memory cache server. The general purpose 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.

Cache the information that needs to be cached in the system memory, and take it out directly from the memory when it needs to be obtained; The more common way is the key _> value model;

Connect($memcachehost, $memcacheport) or die ("could not connect"); $ memcache-& gt; Set('key',' cache content'); $ get = $ memcache-& gt; get($ key); //Get information? & gt

Eight, apache cache module

After apache is installed, caching is not allowed. Beijing IT Training believes that if the external cache or squid server needs web acceleration, it needs to be set in htttpd.conf, provided that the mod_cache module is activated when apache is installed.