Skip to content

Commit

Permalink
jetcache模块
Browse files Browse the repository at this point in the history
  • Loading branch information
guolanren committed Jun 5, 2020
1 parent 6b3915d commit 6374d56
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 0 deletions.
5 changes: 5 additions & 0 deletions jetcache/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Spring Boot JetCache

## 概述

Spring Boot JetCache 概述...
50 changes: 50 additions & 0 deletions jetcache/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>name.guolanren</groupId>
<artifactId>jetcache</artifactId>
<version>1.0.0</version>

<properties>
<java.version>1.8</java.version>
<jetcache.version>2.6.0</jetcache.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.alicp.jetcache</groupId>
<artifactId>jetcache-starter-redis-lettuce</artifactId>
<version>${jetcache.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
70 changes: 70 additions & 0 deletions jetcache/src/main/java/name/guolanren/JetCacheApplication.java
Original file line number Diff line number Diff line change
@@ -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());
}

}
31 changes: 31 additions & 0 deletions jetcache/src/main/java/name/guolanren/model/JetCacheUser.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
40 changes: 40 additions & 0 deletions jetcache/src/main/java/name/guolanren/service/JetCacheService.java
Original file line number Diff line number Diff line change
@@ -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<Long, JetCacheUser> 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);
}
}
21 changes: 21 additions & 0 deletions jetcache/src/main/resources/config/application.yml
Original file line number Diff line number Diff line change
@@ -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:'

0 comments on commit 6374d56

Please sign in to comment.