ArticleDirectory
- From PHP Group
- Use of memcache
- Distributed cache of memcache
- Replication of memcached
- Memcached monitoring
- Memcached Traversal
- Use of Tokyo tyrant
- Replication of Tokyo tyrant
- Status of Tokyo tyrant
- Tokyo tyrant Management
Cache from PHP Group
[Note:Copy an article about caching written on the company's wiki. Some of the content is not described in detail, but only outlines are listed. Some links are linked to the company's wiki, which may be inaccessible to Internet users.. But I basically introduced the common cache mode, and hope to help you.]
| Directory [Hide]
-
- 1 Abstract
-
- 2. Roles of cache and Cache
- 2.1 What is cache?
- 2.2 cache Function
- 3 cache type
- 3.1 database cache
- 3.2 Application Layer Cache
- 3.3 front-end cache
- 3.4 Client Cache
-
- 4. cache update and expiration
- 4.1 cache expiration
- 4.2 cache update
-
- 5. Introduction to cache tools
- 5.1 Database End
- 5.2 Application Layer
- 5.3 front-end
- 5.4 Client
-
- 6. How to evaluate cache Performance
- Speed: 6.1
- 6.2 Timeliness
- 6.3 hit rate
-
- 7. How to effectively use Cache
- 7.1 Where cache is required
- 7.2 select Cache
-
- 8. Use Cache
- 8.1 memcached
- 8.1.1 use of memcache
- 8.1.2 distributed cache of memcache
- 8.1.3 replication of memcached
- 8.1.4 memcached monitoring
- 8.1.5 memcached Traversal
- 8.2 Tokyo tyrant
- 8.2.1 use of Tokyo tyrant
- 8.2.2 replication of Tokyo tyrant
- 8.2.3 status of Tokyo tyrant
- 8.2.4 Management of Tokyo tyrant
- 9 cache storage
-
- 10 FAQs)
- 10.1 how to update the front-end cache
- 10.2 add memcache servers
-
- 11 subject
- 11.1 Phoenix BBS forum system
- 11.1.1 Analysis
- 11.1.2 optimization solution Analysis
- 11.1.3 Forum architecture Discussion
- 11.2 Phoenix Blog system
- 11.3 Phoenix Comment comment System
|
Summary
This article introduces the concept of cache and Its Role in web development, as well as how to use cache. Use the interactive product of Phoenix as an example for analysis.
Roles of cache and Cache
What is cache?
Cache is used to avoid frequent access to the primary storage (in general, it may be databases, structured disk files, remote network interfaces,ProgramInterfaces and so on provide the data returned) to obtain the data and create a temporary memory (cache) that is faster ). In general, the cache is smaller than the primary storage (not necessarily, it may be because the data structure of the storage is different, but the access speed is very fast), but the storage capacity is also relatively small, however, the access speed is very fast.
Cache Function
Cache is generally used
-
- . Store frequently accessed Data
- . Temporary storage of time-consuming computing results
- . Memory Cache reduces disk Io
Cache type
In web development, cache can be divided:
Database cache
-
-
This can be used for "space for time. For example, if you want to create a table to store the total number of data records of another table type, you can update the data table and the number of statistical records each time you update the data. When you need to obtain the number of data items of a certain type, you do not need to select count to query. You can directly query the statistical table, which improves the query speed and database performance.
Application Layer Cache
-
- The Application Layer cache has the greatest relationship with developers and is frequently used.
- . Cache the query results of the database to reduce the data pressure. This must be done on large websites.
- . Cache disk file data. For example, common data can be stored in the memory without reading the disk every time, especially intensive computing programs, such as word segmentation.
- Cache a time-consuming computing operation, such as data statistics.
- The Application Layer Cache architecture can also be divided into several types:
- . Embedded, that is, the cache and application are on the same machine. For example, single-host file caching and hashmap in Java for data caching. This cache speed is fast and there is no network consumption.
- . Distributed cache: the cached data is independent to different machines and requests data through the network. For example, memcache is commonly used.
-
Distributed cache can be divided into several types:
- Splitting data by application to different cache servers is a simple and practical method.
- . Store Data to different cache servers according to certain rules (such as hash, route, and so on ).
- In proxy mode, applications are transparently processed by the proxy when obtaining data, and the cache mechanism is handled by the proxy server.
Front-end cache
The front-end cache we mentioned here can be understood as the CDN technology in general use. The front-end Buffer technology, such as squid, is mainly used for static file types, such as slice, CSS, and Js, HTML and other static files.
Client Cache
The browser-side cache allows users to directly read data from the local cache after one request, but not from the server for the next request. This reduces the server load and speeds up user access.
Cache update and expiration
Here we will talk about the cache at the application layer. In the application layer cache, new data addition, data modification, data deletion, and other operations should be performed, and at some time, these operations need to take effect in a timely manner (because the cache is used, the cache may not be updated after modification, and the page does not change), so there is a concept of cache update and expiration.
Cache expiration
-
-
Cache expiration includes
- . Time expired
When caching data, we can specify the maximum data cache time. If this time is exceeded, we think the cache is invalid.
-
-
- . Rule-Based expiration
Some data is stored in the cache to indicate the data version. For example, the access time, Update Time, and data version information.
Then, compare the information to determine whether it has expired.
Cache update
-
-
- . Passive
-
When the cache becomes invalid, our application retrieves data from the primary storage and then places the data back in the cache.
- . Active
-
When data is updated, our application actively updates our cache content.
- . Passive and active combination (based on version)
-
When the data is updated, we update a mark that the data is updated. Then, update the data based on the "rule-based expiration" mentioned above.
Cache tools
Database End
- A common table uses a common table to cache statistics.
- Memory table
Application Layer
Common application-layer distributed cache containers, such as memcache, shared file server, memcachedb, and Tokyo tyrant. PHP also contains process-based caches such as x-Cache and APC. This cache is faster than distributed cache, but it is limited to a machine of the application. Java implements many caches, such as Oscache, jcache, and ehcached.
Front end
-
-
Common frontend services include squid, varnish cache, and ncache.
Varnish is a high-performance open-source HTTP accelerator. verdens gang (VG. no), Norway's largest online newspaper, uses three varnish instances to replace the original 12 squids, providing better performance than before.
Client
-
-
The Client Cache depends on the implementation of the browser. Currently, most browsers cache the corresponding files based on HTTP information.
How to evaluate cache Performance
Speed
-
-
Of course, one of our purposes of using cache is to speed up our applications. If the cache speed is slower, there is no need for caching. The cache speed should be very fast, because the cache only needs to retrieve a previously stored data. Therefore, the factors that affect the cache speed may be related to the storage method used by the cache, and the speed of the impression may be the network consumption of the distributed cache. Similarly, when the cache server has a high concurrency, it may be overwhelmed by bottlenecks and become slow.
For example, the response time of the post cache used by Phoenix forum is within a few seconds, that is, the response time is less than 1 ms, which is quite fast.
Timeliness
-
-
The important thing we need to use cache is to cache our data updates.
Timely. This is very important and is related to the correctness of page display and user experience.
Hit rate
-
-
The hit rate is directly related to the effect of caching. If the hit rate is too low, it is equivalent to not using the cache, or there is a problem with our cache rules.
How to effectively use Cache
Cache required
-
-
- . The page is very slow to open. A large amount of computing is required each time, and a time-consuming operation data is retrieved from the database.
- . Frequent read operations, each request must read data from the database, and these data updates are not very frequent, resulting in a high pressure on the database.
- . Frequent write operations (which need to be written to the database), such as page clicks and votes. These operations can temporarily use the cache and then regularly write to the database.
Select Cache
-
-
Cache is so useful. How can we choose so many cache products? Next we will focus on common PHP.
- . The access volume is not very large. There is only one Web Front-end machine, and File Cache can be used for the database content cache. This method is very simple and effective, and requires no special configuration on the server.
-
You can also use the memory cache provided by APC and X-cached, which is also very effective. It is faster than the disk cache, but because it is a memory cache, the cache capacity is limited.
- . The access volume is large. Generally, more than one or more Web Front-end machines are used. We generally use distributed cache. We generally choose memcache as our cache. memcache is a memory-based distributed cache with high speed and efficiency.
-
In fact, shared disk cache can also be used here. However, based on our current environment and requirements for reliability and high concurrency, we do not recommend or consider using shared disks as cache.
Use Cache
Memcached
Use of memcache
Distributed cache of memcache
Replication of memcached
-
-
Repcached is a group of patches that add the data replication function to memcached 1.2.x. the main objective is to build a redundant memcached system, and of course, fail over (Failover failure ). all memcached commands are supported. when the master node fails, the slave will be on top. complete data replication during connection.
Memcached monitoring
Memcached Traversal
Tokyo tyrant
Use of Tokyo tyrant
Replication of Tokyo tyrant
Status of Tokyo tyrant
Tokyo tyrant Management
Cache storage
Tokyo Cabinet
Berkeley DB
CDB
CDB is fast, but cannot be updated. PHP is supported.
APC/EA/xcache
FAQs)
How to update front-end cache
- The front-end cache is at the top of the application layer, which is generally invisible to applications. For example, Squid-controlled cache. As a result, a problem occurs. For example, there are many CSS and JS files on the homepage of our website. These CSS and JS files can be cached in Squid. For example, if we cache for 30 days, these files may not be updated once every 30 days. But what should we do when we need to update a CSS or JS file?
Add memcache servers