guava缓存使用
guava在springboot中使用
1.pom中引入包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.company</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>23.0</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.5.9.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
2.springboot项目
2.1 启动类
package com.company.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Hello world! * */ @SpringBootApplication public class App { public static void main(String[] args) { // System.out.println( "Hello World!" ); System.out.println("启动中..."); SpringApplication.run(App.class, args); System.out.println("启动成功"); } }
2.2 controller类中使用cache
package com.company.demo.controller; import java.util.Date; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; @RestController @RequestMapping("/cache") public class CacheController { static final Cache<String, String> cache = CacheBuilder.newBuilder().maximumSize(1000) .expireAfterWrite(3, TimeUnit.SECONDS).build(); @RequestMapping("/test") public void Index() { String cacheKey = "time"; // 使用方式1 // 获取如果为空赋值 String time = cache.getIfPresent(cacheKey); if (time == null) { time = new Date().toString(); cache.put(cacheKey, time); } System.out.println("time:" + time); // 强制过期 cache.invalidate(cacheKey); // 使用方式2 String time2 = cache.get(cacheKey, new Callable<String>() { @Override public String call() throws Exception { String time = new Date().toString(); return time; } }); System.out.println("time:" + time2); } }