一个对redission的封装,redission要比redis功能更强大
<dependency>
<groupId>cn.structured</groupId>
<artifactId>structure-redisson-starter</artifactId>
<version>${last.version}</version>
</dependency>
@Resource
private RedissonClient redissonClient;
RBucket<String> test = redissonClient.getBucket("test");
test.set("test redissonClient");
System.out.println("test = " + test.get());
@RequestMapping("/testLock")
@Lock(keys = "#key")
public void testLock(@RequestParam("key") String key) throws InterruptedException {
System.out.println("key = " + key);
Thread.sleep(10000L);
}
@RequestMapping("/testLock2")
@Lock(keys = "#testVO.id")
public void testLockObject(@RequestBody TestVO testVO ) throws InterruptedException {
System.out.println("key = " + testVO.getId());
Thread.sleep(10000L);
}
写缓存注解是对要要执行的方法返回的结果写入缓存中
@WCache(key = "#giftId",isObjCache = false,
map = @CMap(isMap = true ,mapKey = "ACCOUNT-GIFT:_#accountId"),
time = @CTime(isTime = true,time = 4,timeType = TimeUnit.HOURS))
@WCache 支持map缓存策略 list缓存策略,时间缓存策略,是否为对象缓存
读对象缓存注解通过代理实现无则写入,有则读取如果读取成功不会执行要代理的方法只有在写入缓存是会指定要代理的方法取得返回结果写入缓存中
@RCache(key = "GIFT-TYPE:_#giftType",time = 1)
集合缓存注解是对redisList存储结构封装list缓存注解 可以搭配 map结构和对象结构混合使用
Map缓存注解 是对redis-Map存储结构封装map缓存注解 可以搭配 list结构和对象结构混合使用
@RCacheMap(mapKey = "ACCOUNT-GIFT:_#accountId",key = "#giftId",isTime = true,time = 4,timeType = TimeUnit.HOURS)
读取存储在map中的单条数据
@RMapAllCache(mapKey = "ACCOUNT-GIFT:_#accountId",keyName = "giftId",time = @CTime(isTime = true,time = 4,timeType = TimeUnit.HOURS))
读取map中全部的数据
/**
* 写入缓存,同时构建 object 对象和 集合对象,以及map对象
* @param testVO
* @return
*/
@RequestMapping(value = "cache")
@WCache(key = "#testVO.id",isObjCache = true
,list = @CList(listKeyName = "test-list",isList = true,size = 100,time = @CTime(isTime = true,time = 10))
,map = @CMap(mapKey = "test-map",isMap = true,time = @CTime(isTime = true,time = 100))
)
public TestVO cache(TestVO testVO){
System.out.println("testVO = " + testVO);
return testVO;
}
/**
* 读缓存 如果没有读到则更新缓存 object
* @param id
* @return
*/
@RCache(key = "#id")
@RequestMapping("/getCache")
public TestVO getCache(@RequestParam("id") String id){
TestVO testVO = new TestVO();
testVO.setId(id);
testVO.setName("没有读取到");
System.out.println("需要写入缓存,如果缓存中有值则不会写入缓存");
return testVO;
}
/**
* map和list配合使用
* @param testVO
* @return
*/
@RequestMapping(value = "cacheMapList")
@WCache(key = "#testVO.id",isObjCache = false
,list = @CList(listKeyName = "cache-list",isList = true,size = 100
,time = @CTime(isTime = true,time = 10),mapKey = "cache-map",value = CList.ListType.MAP)
,map = @CMap(mapKey = "cache-map",isMap = true,time = @CTime(isTime = true,time = 100))
)
public TestVO cacheMapList(TestVO testVO){
System.out.println("testVO = " + testVO);
return testVO;
}
/**
* list配合object使用
* @param testVO
* @return
*/
@RequestMapping(value = "cacheList")
@WCache(key = "#testVO.id",isObjCache = true
,list = @CList(listKeyName = "cache-list-key",isList = true,size = 100
,time = @CTime(isTime = true,time = 10),value = CList.ListType.KEY))
public TestVO cacheList(TestVO testVO){
System.out.println("testVO = " + testVO);
return testVO;
}
/**
* list和map关联的数据结构
* 该结构需要手动补偿缓存值
* @return
*/
@RListCache(key = "cache-list",mapKey = "cache-map",value = CList.ListType.MAP)
public List<TestVO> getMapList(){
return new ArrayList<>();
}