# union-cache
**Repository Path**: doobo/union-cache
## Basic Information
- **Project Name**: union-cache
- **Description**: 基于springboot的注解式缓存,方便集成多种缓存(redis、MemCache)而不改变原有代码逻辑,防止雪崩等, 默认基于ConcurrentHashMap实现了本地缓存,通过实现ICacheService即可替换成redis或者MemCache缓存
- **Primary Language**: Java
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 4
- **Forks**: 1
- **Created**: 2020-05-23
- **Last Updated**: 2022-12-22
## Categories & Tags
**Categories**: cache-modules
**Tags**: None
## README
# union-cache
[](https://www.apache.org/licenses/LICENSE-2.0)
> 基于springboot的注解式缓存,方便集成多种缓存(redis、MemCache)而不改变原有代码逻辑,防止雪崩等,
> 默认基于ConcurrentHashMap实现了本地缓存,通过实现ICacheService即可替换成redis或者MemCache缓存
## 如何添加
```
com.github.doobo
union-cache
1.3
```
## 几种注解
```
//读缓存,先判断key是否有缓存,有就从缓存读取返回(不执行方法),否则就执行一次方法,并把结果写入缓存
@RCache(prefix = "index", key = "key", expiredTime = 10)
@RCache(prefix = CacheConfig.USER_PREFIX, key = "#key", expiredTime = CacheConfig.USER_EXPIRED_TIME, unless = "#result == null || !#result.ok")
//更新缓存,用于方法上,方法执行后,更新到缓存
@UCache(prefix = CacheConfig.USER_PREFIX, key = "#key", expiredTime = CacheConfig.USER_EXPIRED_TIME)
//删除缓存,方法执行后,删除缓存
@DCache(prefix = CacheConfig.USER_PREFIX, key = "#key", unless = "#result == null || !#result.ok")
```
## 简单使用
```java
@RestController
public class IndexController {
/**
* 每30秒钟,缓存失效,执行一次UUID
*/
@GetMapping
@RCache(prefix = "uuid", key = "key", expiredTime = 30)
public Map index(){
return Collections.singletonMap("key", UUID.randomUUID().toString());
}
/**
* 更缓存
*/
@GetMapping("update")
@UCache(prefix = "uuid", key = "#uuid", expiredTime = 10)
public Map update(String uuid){
return Collections.singletonMap("key", uuid);
}
/**
* 删除缓存
*/
@GetMapping("delete")
@DCache(prefix = "uuid", key = "key")
public Boolean delete(String key){
return Boolean.TRUE;
}
/**
* 批量删除
*/
@GetMapping("batch")
@DCache(prefix = "uuid", key = "*", batchClear = true)
public Boolean batchClear(){
return Boolean.TRUE;
}
}
```
## 本地缓存实现
```java
@Service
public class UnionLocalCacheService implements ICacheService{
@Override
public ResultTemplate setCache(UnionCacheRequest request) {
InMemoryCacheUtils.cache().add(request.getKey(), request.getValue(), TimeUnit.SECONDS.toMillis(request.getExpire()));
return ResultUtils.of(true);
}
@Override
public ResultTemplate