From 6374d56dcdea510fde275904b7f3bc9e10f81b36 Mon Sep 17 00:00:00 2001 From: guolanren Date: Fri, 5 Jun 2020 10:14:33 +0800 Subject: [PATCH] =?UTF-8?q?jetcache=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jetcache/README.md | 5 ++ jetcache/pom.xml | 50 +++++++++++++ .../name/guolanren/JetCacheApplication.java | 70 +++++++++++++++++++ .../name/guolanren/model/JetCacheUser.java | 31 ++++++++ .../guolanren/service/JetCacheService.java | 40 +++++++++++ .../src/main/resources/config/application.yml | 21 ++++++ 6 files changed, 217 insertions(+) create mode 100644 jetcache/README.md create mode 100644 jetcache/pom.xml create mode 100644 jetcache/src/main/java/name/guolanren/JetCacheApplication.java create mode 100644 jetcache/src/main/java/name/guolanren/model/JetCacheUser.java create mode 100644 jetcache/src/main/java/name/guolanren/service/JetCacheService.java create mode 100644 jetcache/src/main/resources/config/application.yml diff --git a/jetcache/README.md b/jetcache/README.md new file mode 100644 index 0000000..e9dc1c1 --- /dev/null +++ b/jetcache/README.md @@ -0,0 +1,5 @@ +# Spring Boot JetCache + +## 概述 + +Spring Boot JetCache 概述... \ No newline at end of file diff --git a/jetcache/pom.xml b/jetcache/pom.xml new file mode 100644 index 0000000..6036aba --- /dev/null +++ b/jetcache/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 2.1.6.RELEASE + + + + name.guolanren + jetcache + 1.0.0 + + + 1.8 + 2.6.0 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + com.alicp.jetcache + jetcache-starter-redis-lettuce + ${jetcache.version} + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + \ No newline at end of file diff --git a/jetcache/src/main/java/name/guolanren/JetCacheApplication.java b/jetcache/src/main/java/name/guolanren/JetCacheApplication.java new file mode 100644 index 0000000..f67ee12 --- /dev/null +++ b/jetcache/src/main/java/name/guolanren/JetCacheApplication.java @@ -0,0 +1,70 @@ +package name.guolanren; + +import com.alicp.jetcache.anno.config.EnableMethodCache; +import name.guolanren.model.JetCacheUser; +import name.guolanren.service.JetCacheService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * @author guolanren + */ +@SpringBootApplication +@EnableMethodCache(basePackages = "name.guolanren") +public class JetCacheApplication implements CommandLineRunner { + + private static final Logger logger = LoggerFactory.getLogger(JetCacheApplication.class); + + private long start; + + @Autowired + private JetCacheService jetCacheService; + + public static void main(String[] args) { + SpringApplication.run(JetCacheApplication.class, args); + } + + @Override + public void run(String... args) throws Exception { + logger.info("添加数据"); + jetCacheService.add(new JetCacheUser(1L, "guolanren")); + logger.info("================================="); + + logger.info("第一次查询,缓存结果"); + start = System.currentTimeMillis(); + JetCacheUser user = jetCacheService.get(1L); + logger.info("use time: {}, data: {}", System.currentTimeMillis() - start, user.toString()); + logger.info("================================="); + + + logger.info("缓存命中"); + start = System.currentTimeMillis(); + user = jetCacheService.get(1L); + logger.info("use time: {}, data: {}", System.currentTimeMillis() - start, user.toString()); + logger.info("================================="); + + logger.info("更新数据,更新缓存"); + jetCacheService.update(new JetCacheUser(1L, "guoyaozhan")); + logger.info("================================="); + + logger.info("缓存命中"); + start = System.currentTimeMillis(); + user = jetCacheService.get(1L); + logger.info("use time: {}, data: {}", System.currentTimeMillis() - start, user.toString()); + logger.info("================================="); + + logger.info("删除数据,删除缓存"); + jetCacheService.delete(1L); + logger.info("================================="); + + logger.info("重新查询"); + start = System.currentTimeMillis(); + user = jetCacheService.get(1L); + logger.info("use time: {}, data: {}", System.currentTimeMillis() - start, user.toString()); + } + +} diff --git a/jetcache/src/main/java/name/guolanren/model/JetCacheUser.java b/jetcache/src/main/java/name/guolanren/model/JetCacheUser.java new file mode 100644 index 0000000..db10335 --- /dev/null +++ b/jetcache/src/main/java/name/guolanren/model/JetCacheUser.java @@ -0,0 +1,31 @@ +package name.guolanren.model; + +import java.io.Serializable; + +/** + * @author guolanren + */ +public class JetCacheUser implements Serializable { + + private static final long serialVersionUID = 281911666095614238L; + private Long id; + private String name; + + public JetCacheUser(Long id, String name) { + this.id = id; + this.name = name; + } + + public Long getId() { + return id; + } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder("User{"); + sb.append("id=").append(id); + sb.append(", name='").append(name).append('\''); + sb.append('}'); + return sb.toString(); + } +} diff --git a/jetcache/src/main/java/name/guolanren/service/JetCacheService.java b/jetcache/src/main/java/name/guolanren/service/JetCacheService.java new file mode 100644 index 0000000..d555f3b --- /dev/null +++ b/jetcache/src/main/java/name/guolanren/service/JetCacheService.java @@ -0,0 +1,40 @@ +package name.guolanren.service; + +import com.alicp.jetcache.anno.CacheInvalidate; +import com.alicp.jetcache.anno.CacheUpdate; +import com.alicp.jetcache.anno.Cached; +import name.guolanren.model.JetCacheUser; +import org.springframework.stereotype.Service; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; + +/** + * @author guolanren + */ +@Service +public class JetCacheService { + + private ConcurrentHashMap store = new ConcurrentHashMap<>(); + + public void add(JetCacheUser user) { + store.put(user.getId(), user); + } + + @Cached(name = "CacheService.get:", key = "#id", expire = 3600) + public JetCacheUser get(Long id) throws InterruptedException { + TimeUnit.SECONDS.sleep(10); + return store.getOrDefault(id, new JetCacheUser(0L, "none")); + } + + @CacheUpdate(name = "CacheService.get:", key = "#user.id", value = "#result") + public JetCacheUser update(JetCacheUser user) { + store.put(user.getId(), user); + return user; + } + + @CacheInvalidate(name = "CacheService.get:", key="#id") + public void delete(Long id) { + store.remove(id); + } +} diff --git a/jetcache/src/main/resources/config/application.yml b/jetcache/src/main/resources/config/application.yml new file mode 100644 index 0000000..fa2e68a --- /dev/null +++ b/jetcache/src/main/resources/config/application.yml @@ -0,0 +1,21 @@ +jetcache: + statIntervalMinutes: 15 + areaInCacheName: false + local: + default: + type: linkedhashmap + keyConvertor: fastjson + remote: + default: + type: redis.lettuce + keyConvertor: fastjson + valueEncoder: java + valueDecoder: java + readFrom: slavePreferred + uri: + - redis://127.0.0.1:6379 + poolConfig: + minIdle: 5 + maxIdle: 20 + maxTotal: 50 + keyPrefix: 'jetcache:' \ No newline at end of file