An overview of the
Caching has become an essential part of a project. It is the best way to improve performance, such as reducing network I/O, reducing disk I/O, and making the project load faster.
The cache can be CPU cache, memory cache, or hard disk cache. Different caches have different query speeds (CPU cache is better than memory cache than hard disk cache).
Next, I will introduce you one by one.
Browser cache
The browser stores the requested page in the client cache. When the visitor visits the page again, the browser can directly read the data from the client cache, reducing the access to the server and speeding up the loading of the page.
Strong cache
Requests sent by users are directly obtained from the client cache without requests to the server.
Determines whether a strong Cache is hit based on Expires and cache-control.
The code is as follows:
header('Expires: '. gmdate('D, d M Y H:i:s', time() + 3600). ' GMT');
header("Cache-Control: max-age=3600"); // Valid for 3600 seconds
Copy the code
Cache-control can also set the following parameters:
-
Public: can be cached by all users (end user’s browser /CDN server)
-
Private: can only be cached by the end user’s browser
-
No-cache: no local cache is used
-
No-store: disables caching of data
Negotiate the cache
Requests sent by the user are sent to the server, which decides whether to use the client cache.
The code is as follows:
$last_modify = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
if (time() - $last_modify < 3600) {
header('Last-Modified: '. gmdate('D, d M Y H:i:s', $last_modify).' GMT');
The header (' HTTP / 1.1 304); //Not Modified
exit;
}
header('Last-Modified: '. gmdate('D, d M Y H:i:s').' GMT');
Copy the code
Impact of user operations on cache
practice | Expires | Last-Modified |
---|---|---|
Address enter | effective | effective |
Page jump | effective | effective |
A new window | effective | effective |
Forward/backward | effective | effective |
F5 to refresh | invalid | effective |
Ctrl + F5 to refresh | invalid | invalid |
The file cache
Data file cache
Cache the data with low update frequency and high read frequency into files.
For example, if multiple parts of the project use city data for three-level linkage, we can cache the city data into a file (city_data.json), which JS can read directly without requesting the back-end server.
The whole station is static
CMS (content management system), perhaps we are familiar with, such as early DEDE, PHPCMS, background can set static HTML, users when visiting the website read is static HTML, do not request back-end database, do not need Ajax request data interface, accelerate the loading speed of the website.
Static HTML has the following advantages:
-
Conducive to search engine inclusion (SEO)
-
Page opening speed is fast
-
Reduce server load
CDN cache
Content Delivery Network (CDN) Content Delivery Network.
When users visit the website, they automatically select the nearest CDN node content, without the need to request the source server, which speeds up the opening of the website.
The cache mainly includes static resources such as HTML, images, CSS, JS, and XML.
No cache
Memcached to cache
Memcached is a high-performance distributed memory caching server.
The general purpose is to cache database query results to reduce database access times and improve the speed and scalability of dynamic Web applications.
It can also be used to store data in various formats, including images, videos, files, etc.
Memcached supports only K/V data and does not support persistent storage.
Difference between Memcache and Memcached
-
Memcached starts at 0.2.0 and requires PHP >=5.2.0. Memcache requires PHP >=4.3.
-
Memcached was last published on December 24, 2018. Memcache was last published on April 07, 2013.
-
Memcached is based on libmemcached and Memcache is based on PECL extensions.
Think of Memcached as an updated version of Memcache.
PHP Memcached
http://www.php.net/manual/zh/book.memcached.php
Memcached is often compared to Redis, so let’s talk about Redis caching.
Redis cache
Redis is a high performance K/V database.
Redis largely compensates for Memcached K/V storage, such as List (linked List), Set (Set), Zset (ordered Set), and Hash (Hash). Data can be stored in memory or persisted to disk, and master/slave synchronization is supported.
In general, you can think of Redis as an expanded version of Memcached, heavier and more powerful.
Redis is mostly used in daily work.
Redis is available at http://www.redis.cn/
Directing a cache
MongoDB is a database based on distributed file storage. Written in C++ language.
Designed to provide scalable high-performance data storage solutions for WEB applications.
MongoDB is a product between relational database and non-relational database. Among non-relational databases, it has the most rich functions and is the most like relational database.
Directing a learning website: http://www.mongodb.org.cn
WEB server cache
Apache cache
Use mod_expires to specify the expiration time of the cache. You can cache HTML, images, JS, CSS, etc.
Open http.conf and open the module:
LoadModule expires_module modules/mod_expires .so
Copy the code
Specify cache expiration time:
<IfModule expires_module>
# enable cache
ExpiresActive on
# CSS cache (8640,000 seconds =10 days)
ExpiresByType text/css A8640000
# js cache
ExpiresByType application/x-javascript A8640000
ExpiresByType application/javascript A8640000
# HTML cache
ExpiresByType text/html A8640000
# image cache
ExpiresByType image/jpeg A8640000
ExpiresByType image/gif A8640000
ExpiresByType image/png A8640000
ExpiresByType image/x-icon A8640000
</IfModule>
Copy the code
Nginx cache
Use the expire parameter to specify the expiration time of the cache. You can cache HTML, images, JS, CSS, etc.
Open the nginx. Conf:
// For example:
The location ~ \. (GIF | JPG | jpeg | PNG | BMP | ico) ${# to join the new location
root html;
expires 1d; # specify cache time
}
Copy the code
Proxycachepath and proxy_cache.
Opcode cache
Opcode (Operate Code) Specifies the operation Code.
As soon as the PHP program finishes running, all memory is freed, variables in the program are destroyed, and each request has to be retranslated and executed, which can be slow.
When the interpreter has finished analyzing the script code, it generates intermediate code, also known as opcodes, that can be run directly.
The goal of opcodes is to avoid double compilation and reduce CPU and memory overhead.
The APC cache
Alternative PHP Cache (APC) Optional PHP Cache.
The goal of APC is to provide a free, open, and robust framework for caching and optimizing PHP intermediate code.
APC eliminates PHP dynamic parsing and compilation time, making PHP scripts faster to execute.
The APC extension was last released on September 3, 2012.
Interested in can understand, the official introduction: http://php.net/manual/zh/book.apc.php
eAccelerator
EAccelerator: A PHP opcode cache.
Interested can understand, the official introduction: http://eaccelerator.net/
XCache
XCache is a fast and stable PHP Opcode cache.
Interested in can understand, the official introduction: http://xcache.lighttpd.net/
summary
This article mainly introduces the browser cache, file cache, NoSQL cache, WEB server cache, Opcode cache.
Each type of cache can be explored in depth from Introduction -> Installation -> Use -> summary of application scenarios.
So you can think about, from the above, what kind of caches do we use in our work?
What other caches can we use to help our project?
Frequently asked questions about caching
If you have used caching, you must have encountered some headaches, such as data consistency, avalanche, hot data caching, cache monitoring and so on.
To list a few questions for you is a pure diversion.
When using caching in a project, do we choose Redis or Memcached, and why?
Here are some scenarios:
First, for example, to implement a simple log collection function or to send a large number of SMS, email function, the implementation method is to collect data in the queue, and then have a scheduled task to consume the queue, processing the things to do.
Use Redis lpush, RPOP or Rpush, LPOP directly.
/ / into the queue
$redis->lpush(key, value);
/ / out of the queue
$redis->rpop(key);
Copy the code
Memcached has no such data structure.
2. For example, if we want to store user information, such as ID, name, telephone number, age and height, how to store it?
Solution 1: Key => value
Key = userData User ID
Value = json_encode(user data)
When querying, the key is first retrieved and then jSON_decode is performed.
Scheme 2: Hash
Key = userData User ID
HashKey = name, value = xx
HashKey = phone, value = xx
HashKey = age, value = xx
HashKey = height, value = xx
When querying, you only need to retrieve the key.
/ / new
$redis->hSet(key, hashKey, value);
$redis->hSet(key, hashKey, value);
$redis->hSet(key, hashKey, value);
/ / edit
$redis->hSet(key, hashKey, value);
/ / query
$redis->hGetAll(key); // Query all attributes
$redis->hGet(key, hashKey); // Query an attribute
Copy the code
Plan two is better than Plan one.
Third, for example, social projects like Sina Weibo, personal center’s follow list and fan list, two-way follow list, and popular micro-blogs, and message subscription, etc.
All the above data structures provided by Redis can be used.
4. Memcached is stored only in memory, whereas Redis can be stored in memory or persisted to disk.
If the data in the requirement requires persistence, select Redis.
Individuals who do not use Memcached in their work are better than Redis at finding Memcached memory allocation by querying data.
By default, Memcached uses Slab Allocation to manage memory. According to the preset size, the allocated memory is divided into blocks of specific length to store key-value data records of the corresponding length. This completely resolves the memory fragmentation problem.
How do you ensure data consistency between the cache and the database?
Add data: Add data to the database first, then add data to the cache.
Edit data: delete the cached data, modify the data in the database, and then add it to the cache.
Delete data: Deletes cached data first, and then deletes data in the database.
Query data: first query the cache data, no, query the database, and then add to the cache.
Strong consistency is difficult to guarantee, such as transaction consistency, point-in-time consistency, end-point consistency, etc.
Let’s take it on a case-by-case basis.
What about cache penetration?
The user requests data that does not exist in the cache, causing the request to land directly on the database.
1. Set a regular Key value and verify whether the Key complies with the specification.
Ii. Interface current limiting, degradation, and fusing, please study ISTIO: https://istio.io/
Three, Bloom filter.
4. Set the empty cache and expiration time for non-existent key values. If the storage layer creates data, update the cache in time.
What about avalanches?
A mutex allows only one request to rebuild the index, while the other requests wait for the cache rebuild to complete and then retrieve data from the cache again.
Two, the dual cache policy, the original cache and copy cache, when the original cache invalidation request copy cache, the original cache invalidation time is set to short-term, the copy cache is set to long-term.
Already go up, pure throw turn lead jade, combine oneself circumstance, specific problem, specific analysis.
Recommended reading
-
System explanation – PHP WEB security defense
-
System description – SSO single sign-on
-
Having used Redis, I didn’t even know Rdb
This article is welcome to forward, forward please indicate the author and source, thank you!