springboot使用j2cache缓存
1pom文件引入
<?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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>test_j2cache</artifactId>
<dependencies>
<dependency>
<groupId>net.oschina.j2cache</groupId>
<artifactId>j2cache-spring-boot2-starter</artifactId>
<version>2.7.0-release</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<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>
</dependency>
</dependencies>
</project>
2:配置文件
2.1application.properties配置文件配置
server.port=8080
#cache
#j2cache配置文件路径
j2cache.config-location=classpath:j2cache.properties
#开启spring cache支持
j2cache.open-spring-cache=true
2.2 j2cache.properties配置文件
j2cache.broadcast = lettuce
# jgroups properties
jgroups.channel.name = j2cache
j2cache.L1.provider_class = caffeine
j2cache.L2.provider_class = lettuce
# 缓存空对象 (default false)
#j2cache.default_cache_null_object = false
#redis缓存序列化方式,fst、kyro、json、fastjson、java
j2cache.serialization = fastjson
caffeine.properties = /caffeine.properties
lettuce.namespace =
lettuce.mode = single
lettuce.cluster_name = j2cache
lettuce.storage = generic
lettuce.channel = j2cache
lettuce.channel.host =
lettuce.scheme = redis
lettuce.hosts = 172.23.0.182:6379
lettuce.password =
lettuce.database = 3
lettuce.sentinelMasterId =
lettuce.maxTotal = 100
lettuce.maxIdle = 10
lettuce.numTestsPerEvictionRun = 10
lettuce.softMinEvictableIdleTimeMillis = 10
lettuce.minIdle = 10
lettuce.maxWaitMillis = 5000
lettuce.lifo = false
lettuce.minEvictableIdleTimeMillis = 60000
lettuce.timeout = 10000
lettuce.testOnBorrow = true
lettuce.testOnReturn = false
lettuce.testWhileIdle = true
lettuce.timeBetweenEvictionRunsMillis = 300000
lettuce.blockWhenExhausted = false
本地缓存Caffeine配置 caffeine.properties
#########################################
# Caffeine configuration
# [name] = size, xxxx[s|m|h|d]
#########################################
default=1000, 30m
testCache=10000, 6s
测试代码
package com.cache.redis;
import com.cache.redis.model.Person;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
@EnableCaching
public class DemoApplication {
public static void main(String[] args) {
ApplicationContext app = SpringApplication.run(DemoApplication.class, args);
UserService userService = app.getBean(UserService.class);
Person person = new Person(1L,"陈大侠","18","男");
System.out.println("插入用户,新增缓存");
userService.savePerson(person);
System.out.println("第一次获取用户,存在缓存就从缓存返回,不存在就从数据库取");
Person dbPerson = userService.getUserById(1L);
System.out.println(dbPerson);
System.out.println("第二次获取用户");
dbPerson = userService.getUserById(1L);
System.out.println(dbPerson);
System.out.println("更新用户,更新缓存");
person.setAge("88");
userService.savePerson(person);
System.out.println("第二次获取用户,看缓存是否有变化");
dbPerson = userService.getUserById(1L);
System.out.println(dbPerson);
System.out.println("删除用户,删除缓存");
userService.delPerson(1L);
System.out.println("第三次再次获取用户,看缓存是否有变化");
dbPerson = userService.getUserById(1L);
System.out.println(dbPerson);
}
}
package com.cache.redis;
import com.cache.redis.model.Person;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
/**
* @author: jujun chen
* @description:
* @date: 2019/2/14
*/
@Service
@CacheConfig(cacheNames = "person")
public class UserService {
private Person person = null;
@Cacheable(key = "#id")
public Person getUserById(Long id){
//如果没走缓存,会打印下面这句话
System.out.println("=>操作数据库,根据id获取用户信息");
return this.person;
}
@CachePut(key = "#person.id")
public Person savePerson(Person person){
System.out.println("=>操作数据库保存用户数据");
this.person = person;
return this.person;
}
@CacheEvict(key = "#id")
public void delPerson(Long id){
System.out.println("=>操作数据库删除用户数据");
person = null;
}
}
package com.cache.redis.model;
import java.io.Serializable;
/**
* @author: jujun chen
* @description:
* @date: 2019/2/14
*/
public class Person implements Serializable{
private Long id;
private String name;
private String age;
private String sex;
public Person() {
}
public Person(Long id, String name, String age, String sex) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", age='" + age + '\'' +
", sex='" + sex + '\'' +
'}';
}
}