Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use php cache

Shulou Source: shulou.com Published: 2022-06-02 04:41:54 09月16日 Update

This article introduces the knowledge of "how to use php cache". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The standard flow of a website or an application is that the browser sends a request to the application server, and the application server makes some calculation and logical judgment before requesting the database. After receiving the request, the database returns the data to the application server after calculation. The application server calculates again and returns the data to the browser.

Then, with the increase of the complexity and concurrency of web business, the application server does more and more computing and logical processing, but the resources of the application server are limited, and the number of requests received and processed by the database per second is also limited. In order to provide as much throughput as possible in limited resources, it is necessary to reduce the amount of computation and shorten the request process (reducing the network Imax O or the hard disk IBO). At this point, cache (Cache) is used.

Php cache type

1. Data cache:

Data cache: the data cache here refers to the database query PHP cache mechanism. Every time you visit a page, you will first detect whether the corresponding cached data exists. If it does not exist, connect to the database, get the data, and serialize the query results and save them to a file. Later, the same query results will be obtained directly from the cache table or file.

The most widely used example is Discuz's search function, which caches the results ID into a table and searches the cache table the next time you search for the same keyword.

To take a common method, when multiple tables are associated, save the generated array of the contents in the schedule to a field of the main table, and decompose the array when needed. This advantage is to read only one table. The disadvantage is that the two data synchronization will be many more steps, the database is always the bottleneck, using the hard disk for speed is the key point.

2. Page cache:

Every time you visit a page, you will first check whether the corresponding cache page file exists. If it does not exist, connect to the database, get the data, display the page, and generate the cache page file at the same time, so that the page file will work the next time you visit it. (this feature is usually available in template engines and some PHP caching mechanism classes common on the Internet)

3. Time-triggered cache:

Check whether the file exists and the timestamp is less than the set expiration time. If the timestamp modified by the file is larger than the current timestamp minus the expiration timestamp, then the cache is used, otherwise the cache is updated.

4. Content trigger cache:

Forces the PHP cache mechanism to be updated when data is inserted or updated.

5. Static cache:

The static cache here refers to static, directly generate text files such as HTML or XML, and re-generate when there is an update, which is suitable for pages that do not change much, not to mention.

Instance expansion:

Memcached

Is a high-performance distributed in-memory object caching system for dynamic Web applications to reduce database load. It reduces the number of times to read the database by caching data and objects in memory, thus improving the speed of dynamic, database-driven websites. Memcached is based on a hashmap that stores key / value pairs.

/ / initialize $cache = new Memcache (); $cache- > pconnect (127.0.0.1, 11211); / / write $cache- > set (CACHE_PREFIX. $key, $value, MEMCACHE_COMPRESSED, $expire); (CACHE_PREFIX to avoid naming conflicts, it is best to add a prefix, MEMCACHE_COMPRESSED a tag, set to 0 to indicate no compression) / / read $cache- > get (CACHE_PREFIX. $key); / / delete $cache- > delete (CACHE_PREFIX. $key)

Redis:

Is an open source ANSI C language to write, support the network, memory-based and persistent log-type, Key-Value database, and provide multiple languages of API. Similar installation of php extension / / initialization $cache = new Redis ()

/ / initialize $cache = new\ Redis (); $cache- > connect (CACHE_HOSTNAME, CACHE_PORT); / / check the existence first, then write and set the valid time if ($cache- > exists ($key)) {$cache- > delete (CACHE_PREFIX. $key);} $cache- > set (CACHE_PREFIX. $key, serialize ($value); $cache- > expire (CACHE_PREFIX. $key, $expire); / / read $cache- > get (CACHE_PREFIX. $key); that's all for "how to use php Cache". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Cache data database file page time application time server service content update memory limited mechanism results website static search query Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Redmi Xiaomi Huawei Linux