In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to use http Caching in SpringMVC". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "how to use http Caching in SpringMVC".
Cache is a very important function of HTTP protocol. Using Cache can greatly improve the performance of the application and reduce the network transmission of data.
Generally speaking, we cache static resources such as images, CSS,JS files, etc. Similarly, we can use HTTP Cache with Spring MVC to cache dynamic resources.
So when do you use caching of dynamic resources?
HTTP Cache can only be used if the resource is not updated frequently or if you know exactly when the resource will be updated.
HTTP Cache is implemented through request headers, and there are three main ways: expiration time, last update time, and Etag.
The expiration time is client-side authentication, and the last update time and Etag are server-side authentication.
Expiration time
There are two ways to expire, namely Cache-Control and Expires headers.
In Cache-Control, we can set its maxAge, and after that time, the resource will be requested again. As follows:
@ GetMapping ("/ {id}") ResponseEntity getProduct (@ PathVariable long id) {/ / … CacheControl cacheControl = CacheControl.maxAge (30, TimeUnit.MINUTES); return ResponseEntity.ok () .cacheControl (cacheControl) .body (product);}
We can also set the Expires property in Head. The time of Expires needs to be in a standard time format, as follows:
Sun, 06 Nov 1994 08:49:37 GMT; RFC 1123, updated by RFC 1123
Sunday, 06-Nov-94 08:49:37 GMT; RFC 850, obsoleted by RFC 1036
Sun Nov 6 08:49:37 1994; ANSI C's asctime () format
If you want to use it in java, refer to the following example:
@ GetMapping ("/ forecast") ResponseEntity getTodaysForecast () {/ /... ZonedDateTime expiresDate = ZonedDateTime.now () .with (LocalTime.MAX); String expires = expiresDate.format (DateTimeFormatter.RFC_1123_DATE_TIME); return ResponseEntity.ok () .header (HttpHeaders.EXPIRES, expires) .body (weatherForecast);}
If Cache-Control and Expires appear at the same time, Cache-Control is preferred.
Last-Modified
Its verification logic is that the client sets its If-Modified-Since according to the Last-Modified obtained from the last request, the server receives this property and compares it with the previous one, and if it is the same, it can return an empty body. As follows:
@ GetMapping ("/ {id}") ResponseEntity getProduct (@ PathVariable long id, WebRequest request) {Product product = repository.find (id); long modificationDate = product.getModificationDate () .toInstant () .toEpochMilli (); if (request.checkNotModified (modificationDate)) {return null;} return ResponseEntity.ok () .lastModified (modificationDate) .body (product);} ETag
The time of Last-Modified can only be accurate to seconds, and if you need more fine-grained, you need to use ETag.
ETag can be seen as the only token of a resource at the current time, and you can take the hash value of that resource as ETag.
Here is one implementation of it:
@ GetMapping ("/ {id}") ResponseEntity getProduct (@ PathVariable long id, WebRequest request) {Product product = repository.find (id); String modificationDate = product.getModificationDate (). ToString (); String eTag = DigestUtils.md5DigestAsHex (modificationDate.getBytes ()); if (request.checkNotModified (eTag)) {return null;} return ResponseEntity.ok () .eTag (eTag) .body (product);} Spring ETag filter
Spring provides a ShallowEtagHeaderFilter to automatically generate Etag for you based on the returned content.
@ Beanpublic FilterRegistrationBean filterRegistrationBean () {ShallowEtagHeaderFilter eTagFilter = new ShallowEtagHeaderFilter (); FilterRegistrationBean registration = new FilterRegistrationBean (); registration.setFilter (eTagFilter); registration.addUrlPatterns ("/ *"); return registration;}
Note that ETag calculations may affect performance.
The above is all the contents of the article "how to use http Caching in SpringMVC". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 261
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.