介绍
怎么在springboot中使用GuavaCache处理缓存?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
<强>使用GuavaCache可以快速建立缓存强>
1。需要在启动类上注解@EnableCaching
2。配置CacheManager
3。控制器上注解使用@Cacheable
砰的一声。xml
& lt; parent> ,,,& lt; groupId> org.springframework.boot ,,,& lt; artifactId> spring-boot-starter-parent ,,,& lt; version> 1.5.9.RELEASE & lt;才能/parent> & lt;才能properties> ,,,& lt; project.build.sourceEncoding> UTF-8 ,,,& lt; project.reporting.outputEncoding> UTF-8 ,,,& lt; java.version> 1.8 & lt;/java.version> & lt;才能/properties> & lt;才能dependencies> ,,,& lt; dependency> ,,,,,& lt; groupId> org.springframework.boot ,,,,,& lt; artifactId> spring-boot-starter-web ,,,& lt;/dependency> ,,,& lt; dependency> ,,,,,& lt; groupId> org.springframework ,,,,,& lt; artifactId> spring-context-support ,,,,,& lt; version> 4.3.9.RELEASE ,,,& lt;/dependency> ,,,& lt; dependency> ,,,,,& lt; groupId> com.google.guava ,,,,,& lt; artifactId> guava ,,,,,& lt; version> 18.0 & lt;/version> ,,,& lt;/dependency> & lt;才能/dependencies> & lt;才能build> ,,,& lt; plugins> ,,,,,& lt; plugin> ,,,,,,,& lt; groupId> org.apache.maven.plugins ,,,,,,,& lt; artifactId> maven-compiler-plugin ,,,,,,,& lt; configuration> ,,,,,,,,,& lt; source> 1.8 & lt;/source> ,,,,,,,,,& lt; target> 1.8 & lt;/target> ,,,,,,,,,& lt; encoding> UTF-8 ,,,,,,,& lt;/configuration> ,,,,,& lt;/plugin> ,,,& lt;/plugins> & lt;才能/build>
CacheConfig。java配置类
package application.config; import com.google.common.cache.CacheBuilder; import org.springframework.cache.CacheManager; import org.springframework.cache.guava.GuavaCache; import org.springframework.cache.support.SimpleCacheManager; import org.springframework.context.annotation.Configuration; import java.util.ArrayList; import 并不知道; import java.util.concurrent.TimeUnit; @ configuration public class  CacheConfig { public 才能;CacheManager 缓存管理器(){ ,,,GuavaCache GuavaCache =, new GuavaCache (“GuavaCacheAll",, CacheBuilder.newBuilder () ,,,.recordStats () ,,,.expireAfterWrite(10000年,TimeUnit.SECONDS) ,,,.build ()); ,,,List List =, new ArrayList (); ,,,list.add (guavaCache); ,,,SimpleCacheManager SimpleCacheManager =, new SimpleCacheManager (); ,,,simpleCacheManager.setCaches(列表); ,,,return simpleCacheManager; ,,} }
TestController。java控制器测试类
package application.controller; import org.springframework.cache.annotation.Cacheable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class  TestController { @RequestMapping才能(“/test")//关键是才能使用?取得参数,根据参数的名字作为缓存的关键,价值是使用的缓存列表中的那个,具体看配置类 @Cacheable才能(=value “GuavaCacheAll", key =,“# name") public 才能;String  tt (String 名称){ ,,,System.out.println (“tt"拷贝); ,,,return “名称:“+名称; ,,} }
应用程序。java springboot启动类
package 应用; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; null null null null null null null null null怎么在springboot中使用GuavaCache处理缓存