Springboot 使用缓存cache
Springboot默认配置使用的是 ConcurrentMapCache,它将缓存数据存储在内存中的 ConcurrentHashMap 里
数据仅在应用运行期间存在,重启后丢失
使用方法:
1.main方法增加注解@EnableCache使能cache
package org.example.demo2;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@SpringBootApplication
@RestController
@EnableCaching
public class Demo2Application {@AutowiredTest1 test1;public static void main(String[] args) {SpringApplication.run(Demo2Application.class, args);}@GetMapping("/t1")public String t1() {return test1.test("dsffdsfdfs");}
}
2.写一个
package org.example.demo2;import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.Arrays;
@Component
public class Test1 {@Cacheable("test")public String test(String p0) {System.out.println("test");return "6666";}}
类,加一个方法,在方法上加上注解@Cacheable,这样他就会缓存方法的返回值,缓存后再调用将直接返回!
注意,该类需要交由容器管理,所以在调用处需要使用@Autowire注入
测试
. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot :: (v3.5.5)2025-09-16T16:52:34.061+08:00 INFO 19420 --- [demo2] [ main] org.example.demo2.Demo2Application : Starting Demo2Application using Java 21.0.1 with PID 19420 (F:\java\test1\demo2\target\classes started by FRT in F:\java\test1\demo2)
2025-09-16T16:52:34.066+08:00 INFO 19420 --- [demo2] [ main] org.example.demo2.Demo2Application : No active profile set, falling back to 1 default profile: "default"
2025-09-16T16:52:36.462+08:00 INFO 19420 --- [demo2] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http)
2025-09-16T16:52:36.498+08:00 INFO 19420 --- [demo2] [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2025-09-16T16:52:36.499+08:00 INFO 19420 --- [demo2] [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.44]
2025-09-16T16:52:36.673+08:00 INFO 19420 --- [demo2] [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2025-09-16T16:52:36.676+08:00 INFO 19420 --- [demo2] [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2477 ms
2025-09-16T16:52:37.289+08:00 DEBUG 19420 --- [demo2] [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : 3 mappings in 'requestMappingHandlerMapping'
2025-09-16T16:52:37.476+08:00 DEBUG 19420 --- [demo2] [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Patterns [/webjars/**, /**] in 'resourceHandlerMapping'
2025-09-16T16:52:37.526+08:00 DEBUG 19420 --- [demo2] [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : ControllerAdvice beans: 0 @ModelAttribute, 0 @InitBinder, 1 RequestBodyAdvice, 1 ResponseBodyAdvice
2025-09-16T16:52:37.622+08:00 DEBUG 19420 --- [demo2] [ main] .m.m.a.ExceptionHandlerExceptionResolver : ControllerAdvice beans: 0 @ExceptionHandler, 1 ResponseBodyAdvice
2025-09-16T16:52:37.825+08:00 INFO 19420 --- [demo2] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/'
2025-09-16T16:52:37.844+08:00 INFO 19420 --- [demo2] [ main] org.example.demo2.Demo2Application : Started Demo2Application in 4.713 seconds (process running for 5.56)
2025-09-16T16:52:41.663+08:00 INFO 19420 --- [demo2] [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2025-09-16T16:52:41.663+08:00 INFO 19420 --- [demo2] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2025-09-16T16:52:41.663+08:00 DEBUG 19420 --- [demo2] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Detected StandardServletMultipartResolver
2025-09-16T16:52:41.664+08:00 DEBUG 19420 --- [demo2] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Detected AcceptHeaderLocaleResolver
2025-09-16T16:52:41.664+08:00 DEBUG 19420 --- [demo2] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Detected FixedThemeResolver
2025-09-16T16:52:41.665+08:00 DEBUG 19420 --- [demo2] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Detected org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator@befc6f6
2025-09-16T16:52:41.665+08:00 DEBUG 19420 --- [demo2] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Detected org.springframework.web.servlet.support.SessionFlashMapManager@6a05501d
2025-09-16T16:52:41.666+08:00 DEBUG 19420 --- [demo2] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : enableLoggingRequestDetails='false': request parameters and headers will be masked to prevent unsafe logging of potentially sensitive data
2025-09-16T16:52:41.666+08:00 INFO 19420 --- [demo2] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 3 ms
2025-09-16T16:52:41.685+08:00 DEBUG 19420 --- [demo2] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : GET "/t1", parameters={}
2025-09-16T16:52:41.717+08:00 DEBUG 19420 --- [demo2] [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.example.demo2.Demo2Application#t1()
test
2025-09-16T16:52:41.769+08:00 DEBUG 19420 --- [demo2] [nio-8080-exec-1] m.m.a.RequestResponseBodyMethodProcessor : Using 'text/html', given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8, application/signed-exchange;v=b3;q=0.7] and supported [text/plain, */*, application/json, application/*+json]
2025-09-16T16:52:41.770+08:00 DEBUG 19420 --- [demo2] [nio-8080-exec-1] m.m.a.RequestResponseBodyMethodProcessor : Writing ["6666"]
2025-09-16T16:52:41.783+08:00 DEBUG 19420 --- [demo2] [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed 200 OK
2025-09-16T16:52:42.578+08:00 DEBUG 19420 --- [demo2] [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : GET "/t1", parameters={}
2025-09-16T16:52:42.579+08:00 DEBUG 19420 --- [demo2] [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.example.demo2.Demo2Application#t1()
2025-09-16T16:52:42.584+08:00 DEBUG 19420 --- [demo2] [nio-8080-exec-2] m.m.a.RequestResponseBodyMethodProcessor : Using 'text/html', given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8, application/signed-exchange;v=b3;q=0.7] and supported [text/plain, */*, application/json, application/*+json]
2025-09-16T16:52:42.584+08:00 DEBUG 19420 --- [demo2] [nio-8080-exec-2] m.m.a.RequestResponseBodyMethodProcessor : Writing ["6666"]
2025-09-16T16:52:42.585+08:00 DEBUG 19420 --- [demo2] [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Completed 200 OK
2025-09-16T16:52:43.496+08:00 DEBUG 19420 --- [demo2] [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : GET "/t1", parameters={}
2025-09-16T16:52:43.497+08:00 DEBUG 19420 --- [demo2] [nio-8080-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.example.demo2.Demo2Application#t1()
2025-09-16T16:52:43.502+08:00 DEBUG 19420 --- [demo2] [nio-8080-exec-3] m.m.a.RequestResponseBodyMethodProcessor : Using 'text/html', given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8, application/signed-exchange;v=b3;q=0.7] and supported [text/plain, */*, application/json, application/*+json]
2025-09-16T16:52:43.503+08:00 DEBUG 19420 --- [demo2] [nio-8080-exec-3] m.m.a.RequestResponseBodyMethodProcessor : Writing ["6666"]
2025-09-16T16:52:43.504+08:00 DEBUG 19420 --- [demo2] [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : Completed 200 OK
2025-09-16T16:52:44.238+08:00 DEBUG 19420 --- [demo2] [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : GET "/t1", parameters={}
2025-09-16T16:52:44.239+08:00 DEBUG 19420 --- [demo2] [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.example.demo2.Demo2Application#t1()
2025-09-16T16:52:44.241+08:00 DEBUG 19420 --- [demo2] [nio-8080-exec-4] m.m.a.RequestResponseBodyMethodProcessor : Using 'text/html', given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8, application/signed-exchange;v=b3;q=0.7] and supported [text/plain, */*, application/json, application/*+json]
2025-09-16T16:52:44.242+08:00 DEBUG 19420 --- [demo2] [nio-8080-exec-4] m.m.a.RequestResponseBodyMethodProcessor : Writing ["6666"]
2025-09-16T16:52:44.245+08:00 DEBUG 19420 --- [demo2] [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Completed 200 OK
只有第一次调用的时候打印方法里的test,以后再次调用将直接返回,跳过打印test
测试成功!