缓存jar包引用
[!TIP|label: 单机缓存]
<dependency>
<groupId>com.ustcinfo.ishare.eip</groupId>
<artifactId>eip-admin-starter-cache-memory</artifactId>
<version>基础框架最新版本</version>
</dependency>
[!TIP|label: redis缓存]
<dependency>
<groupId>com.ustcinfo.ishare.eip</groupId>
<artifactId>eip-admin-starter-cache-redis</artifactId>
<version>基础框架最新版本</version>
</dependency>
[!TIP|label: redis缓存配置]
设置redis连接配置信息
修改工程中的 application-xxx.yml 配置文件,添加如下配置信息
admin:
cache:
redis:
host: 192.168.52.11
port: 6379
password: xxx
database: 0
timeout: 60000
max-active: -1
max-wait: -1
max-idle: 20
min-idle: 10
maxWaitMillis: 60000
testOnBorrow: false
testOnReturn: true
testWhileIdle: true
maxTotal: 100
redis连接参数说明
参数名 | 说明 |
---|---|
host | 单机redis的IP |
port | 单机redis的端口 |
password | 连接的密码,如果没有就不填写 |
database | 使用的DB下标,好像还有问题 |
timeout | 连接redis的超时时间 |
max-active | 连接池最大连接数(使用负值表示没有限制) |
max-wait | 连接池最大阻塞等待时间(使用负值表示没有限制) |
max-idle | 最大空闲连接个数 |
min-idle | 最小空闲连接个数 |
master | 哨兵模式下的master名称,如果填写了那么则认为使用哨兵模式,host以及port的配置将无效 |
nodes | 哨兵节点,格式为: ip:port,ip1:port1,ip2:port2 ,多个之间使用逗号隔开 |
如何使用
[!TIP|label: 定义缓存实体类]
@Data
public class EmpCache{
private String name;
private String addr;
}
[!TIP|label: 定义唯一标识]
每个缓存都有一个唯一的标识,在唯一标识的字段上使用 @AdminCacheKey 注解
@Data
public class EmpCache{
@AdminCacheKey
private String name;
private String addr;
}
[!TIP|label: 实现缓存接口]
必须实现AdminCacheable接口
@Data
public class EmpCache implements AdminCacheable {
@AdminCacheKey
private String name;
private String addr;
}
[!TIP|label: 创建缓存实体并赋值]
public void update(){
EmpCache emp = new EmpCache();
emp.setName("小明");
emp.setAddr("合肥");
}
[!TIP|label: 更新缓存]
@Autowired
private IAdminCache adminCache;
public void update(){
...
adminCache.put(emp);
}
[!TIP|label: 惊讶]
额… 这样就完成了分布式缓存
如何获取数据
[!TIP|label: 指定类型以及租户标识]
@Autowired
private IAdminCache adminCache;
...
{
// 这里指定获取的类型是 EmpCache,获取租户110下的数据
adminCache.get(EmpCache.class,'110','xxx');
}
[!TIP|label: 获取指定租户下指定类型的单个数据]
@Autowired
private IAdminCache adminCache;
public EmpCache get(Long companyId,String name){
EmpCache emp = adminCache.get(EmpCache.class,companyId,name);
return emp;
}
[!WARNING|label: 参数说明]
adminCache.get方法的第一个参数为缓存的类型;第二个参数为租户标识,必须与**@AdminCacheTenantId**注解一致;
第三个参数为缓存key,该key必须和@AdminCacheKey注解一致
[!TIP|label: 获取指定租户下指定类型的所有数据]
@Autowired
private IAdminCache adminCache;
public List<EmpCache> getAll(Long companyId){
return adminCache.getAll(EmpCache.class,companyId);
}
如何删除数据
[!TIP|label: 指定类型以及租户标识]
@Autowired
private IAdminCache adminCache;
...
{
// 这里指定删除的类型是 EmpCache,删除的是 110租户下数据
adminCache.remove(EmpCache.class,'110','xxx');
}
[!TIP|label: 删除指定租户下,指定类型的单个数据]
@Autowired
private IAdminCache adminCache;
public void remove(Long companyId,String name){
adminCache.remove(EmpCache.class,companyId,name);
}
[!WARNING|label: 参数说明]
adminCache.remove方法的第一个参数为缓存的类型;第二个参数为租户标识,必须与**@AdminCacheTenantId**注解一致;
第二个参数为缓存key,该key必须和@AdminCacheKey注解一致
[!TIP|label: 删除指定租户下,指定类型的所有数据]
@Autowired
private IAdminCache adminCache;
public void removeAll(Long companyId){
adminCache.removeAll(EmpCache.class,companyId);
}