Ehcache : 一个纯 Java 的进程内缓存框架,具有快速、精干的特点,是 Hibernate 中默认的 CacheProvider。主要的特性有:快速、简单、多种缓存策略、缓存数据有两级:内存和磁盘,因此无需担心容量问题、缓存数据会在虚拟机重启的过程中写入磁盘、可以通过 RMI、可插入 API 等方式进行分布式缓存等。
- 依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
- yaml
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/db1?useSSL=false&useUnicode=true&characterEncoding=UTF-8
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password:
cache:
type: ehcache
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
global-config:
db-config:
id-type: auto
logic-delete-value: 1
logic-not-delete-value: 0
mapper-locations: classpath*:mapper/*.xml
type-aliases-package: com.gui.ehcachetest.dataobject
logging:
level:
cn:
iocoder:
springboot:
lab21:
cache:
mapper: debug
与yaml 同目录创建Ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<cache name="users"
maxElementsInMemory="1000"
timeToLiveSeconds="60"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>
com.gui.ehcachetest.dataobject下创建 DO
@TableName(value = "users")
@Data
@ToString
public class UserDo {
private Integer id;
private String username;
private String password;
private Date createTime;
@TableLogic
private Integer deleted;
}
@Repository
@CacheConfig(cacheNames = "users")
public interface UserMapper extends BaseMapper<UserDo> {
@Cacheable(value = "users",key = "#id")
UserDo selectById(Integer id);
@CachePut(value = "users",key = "#userDo.id")
default UserDo insert0(UserDo userDo) {
this.insert(userDo);
return userDo;
}
@CacheEvict(key = "#id")
int deleteById(Integer id);
}
最后在启动类加上 @EnableCaching
就行了。
Redis
- pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.80</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.2.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
yaml
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/db1?useSSL=false&useUnicode=true&characterEncoding=UTF-8
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: admin111111
redis:
host: 127.0.0.1
port: 6379
password:
database: 0
timeout: 0
jedis:
pool:
max-active: 8
max-idle: 8
min-idle: 0
max-wait: -1
cache:
type: REDIS
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
global-config:
db-config:
id-type: AUTO
logic-delete-value: 1
logic-not-delete-value: 0
mapper-locations:
- classpath*:mapper/*.xml
type-aliases-package: com.gui.dataredis.dataobject
logging:
level:
com:
gui:
dataredis:
mapper: debug
其他的与 Ehcache 一致。
直接测试:
@Resource
private UserMapper userMapper;
@Resource
private CacheManager cacheManager;
@Test
public void testSelectById() {
UserDO userDO = userMapper.selectById(1);
if (userDO != null) {
System.out.println(userDO);
}
assert userDO != null;
Assert.notNull(new IErrorCode() {
@Override
public long getCode() {
return 0;
}
@Override
public String getMsg() {
return "缓存为空";
}
},
Objects.requireNonNull(cacheManager.getCache("users")).get(userDO.getId(), UserDO.class));
userDO = userMapper.selectById(1);
System.out.println(userDO);
}
@Test
public void testInsertUser() {
UserDO userDO = new UserDO();
userDO.setUsername(UUID.randomUUID().toString());
userDO.setPassword("cool");
userDO.setCreateTime(new Date());
userDO.setDeleted(0);
userMapper.insert0(userDO);
Assert.notNull(new IErrorCode() {
@Override
public long getCode() {
return 0;
}
@Override
public String getMsg() {
return "缓存为空";
}
},
Objects.requireNonNull(cacheManager.getCache("users")).get(userDO.getId(), UserDO.class));
}
@Test
public void testDelete() {
UserDO userDO = new UserDO();
userDO.setUsername(UUID.randomUUID().toString());
userDO.setPassword("cool");
userDO.setCreateTime(new Date());
userDO.setDeleted(0);
userMapper.insert0(userDO);
Assert.notNull(new IErrorCode() {
@Override
public long getCode() {
return 0;
}
@Override
public String getMsg() {
return "缓存为空";
}
},
Objects.requireNonNull(cacheManager.getCache("users")).get(userDO.getId(), UserDO.class));
userMapper.deleteById(userDO.getId());
Assert.isNull(new IErrorCode() {
@Override
public long getCode() {
return 0;
}
@Override
public String getMsg() {
return "缓存bu为空";
}
},
Objects.requireNonNull(cacheManager.getCache("users")).get(userDO.getId(), UserDO.class));
}
转载于:https://www.iocoder.cn/Spring-Boot/Cache/?github