Introduction to Java Cache

67b917c574d74675b39dd44baf3d53af.jpg1. Cache

 

 

1. What is cache?

 

      Cache is hardware, which is a component in the CPU. The speed of CPU access to data is very fast. It can access and process one billion instructions and data in one second (term: CPU main frequency 1G), while the memory is much slower and faster. It is not bad if the memory can reach tens of megabytes. It can be seen how big the speed difference between the two is. The cache is an intermediate medium to solve the problem of the speed difference between the CPU and the memory. It reads data with a high CPU access frequency from the memory. Cache, thereby improving the efficiency of software execution.

 

  The above is the cache in the hardware. The definition of the cache in the software is more extensive, and there are more implementation methods, but the purpose is the same. It is to copy commonly used data to the memory for fast reading by the CPU (note: Through the principle of java, it can be seen that java cannot operate the cache, but it can suggest storing those data to the cache). The basic idea of ​​caching is to trade space for time. We know that the read and write speed of IO is slower than that of memory. Usually, the bottleneck of a web application occurs in the read and write of disk IO. Then, if we create a storage area in the memory and cache the data, when the browser arrives with a request, it will directly obtain the corresponding data from the memory, which can reduce the pressure on the server, and secondly, can improve The response speed of the request improves the user experience.

 

 

 

 

 

2. What are the commonly used ways to implement caching in java projects?

 

2.1 Database data cache

 

      Generally speaking, web application business logic business logic is relatively complex, and there are many databases. To obtain a complete data, it is often necessary to read the database multiple times, or use extremely complex and low-efficiency SQL query statements. In order to improve the performance of the query, the queried data is cached in the memory, and the next query is directly returned from the memory cache to improve the response efficiency.

 

For example, the caching mechanism of mysql, oracle, etc. can realize data caching. Sometimes for the same page request, the subsequent request time is significantly shorter than the first request, which is the role of database data caching. In addition, by adjusting the field type and index of the database, Sequences, etc. can optimize query efficiency.

 

 

 

2.2 Application layer cache

      Application layer caching refers to the caching we do at the code level. Through the code logic, the data or resources that have been requested are cached, and when the data is needed again, the available cached data is selected through logical processing. Application layer caching is mainly for caching a certain business method. Some business objects have complex logic, which may involve multiple database reads and writes or other expensive operations. Application layer caching can liberate complex business logic and reduce server pressure. .

 

 

 

2.3 Page caching (browser caching)

      In addition to IO, another major bottleneck of web applications is the rendering of page templates. Each request needs to obtain the corresponding model from the business logic layer and render it into corresponding HTML. Generally speaking, web applications have much greater requirements for reading data than updating data. In most cases, the HTML returned by a certain request is the same, so directly caching HTML is also a mainstream method of caching.

 

 

 

2.4 Proxy server caching

      The forward proxy needs to be configured by the user's browser, and the reverse proxy needs to configure the DNS server first, then accept the user's browsing request, and forward the request to the remote server. The reverse proxy server is an intermediate server between the browser and the source server. The browser first initiates a web request to the intermediate server, and after processing (such as authorization verification, cache matching, etc.), the request is forwarded to the source server. Proxy server caches work in much the same way that browsers do, only on a larger scale. It can be understood as a shared cache that not only serves one user, but generally serves a large number of users, so it is very effective in reducing corresponding time and bandwidth usage, and the same copy will be reused multiple times. Common reverse proxy servers: Nginx is software.

 

 

 

2.5 CDN cache

      CDN (ContentDeliveryNetwork content distribution network) cache, also called gateway cache, reverse proxy cache. The browser first initiates a web request to the CDN gateway, and the gateway server corresponds to one or more load balancing source servers, which will dynamically forward the request to the appropriate source server according to their load requests. Although the cache between source servers in this architecture load balancing cannot be shared, it has better scalability. A CDN is a network of clustered servers, not software.

 

 

 

2.6 Spring-based cache

 

As a mature java web framework, spring has a complete caching mechanism. At the same time, spring has not provided extensions for other cache implementations. Next, let us try the implementation of spring's database cache, application layer cache, and page cache in a simple student management system.

Guess you like

Origin blog.csdn.net/weixin_57763462/article/details/131484571