diff --git a/src/main/java/cn/lihongjie/coal/cacheCtl/controller/CacheCtlController.java b/src/main/java/cn/lihongjie/coal/cacheCtl/controller/CacheCtlController.java new file mode 100644 index 00000000..fe4db233 --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/cacheCtl/controller/CacheCtlController.java @@ -0,0 +1,68 @@ +package cn.lihongjie.coal.cacheCtl.controller; + +import cn.lihongjie.coal.annotation.SysLog; +import cn.lihongjie.coal.base.dto.CommonQuery; +import cn.lihongjie.coal.base.dto.IdRequest; +import cn.lihongjie.coal.cacheCtl.dto.CacheCtlDto; +import cn.lihongjie.coal.cacheCtl.dto.CreateCacheCtlDto; +import cn.lihongjie.coal.cacheCtl.dto.UpdateCacheCtlDto; +import cn.lihongjie.coal.cacheCtl.service.CacheCtlService; + +import lombok.extern.slf4j.Slf4j; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/cacheCtl") +@SysLog(module = "缓存控制") +@Slf4j +public class CacheCtlController { + @Autowired private CacheCtlService service; + + @PostMapping("/create") + public CacheCtlDto create(@RequestBody CreateCacheCtlDto request) { + return this.service.create(request); + } + + @PostMapping("/update") + public CacheCtlDto update(@RequestBody UpdateCacheCtlDto request) { + return this.service.update(request); + } + + @PostMapping("/delete") + public Object delete(@RequestBody IdRequest request) { + this.service.delete(request); + return true; + } + + @PostMapping("/deleteKey") + public Object deleteKey(@RequestBody CacheCtlDto request) { + this.service.deleteKey(request); + return true; + } + + @PostMapping("/keys") + public Object keys(@RequestBody CacheCtlDto request) { + return this.service.keys(request); + } + + @PostMapping("/value") + public Object value(@RequestBody CacheCtlDto request) { + return this.service.value(request); + } + + @PostMapping("/getById") + public CacheCtlDto getById(@RequestBody IdRequest request) { + return this.service.getById(request.getId()); + } + + @PostMapping("/list") + public Page list(@RequestBody CommonQuery request) { + return this.service.list(request); + } +} diff --git a/src/main/java/cn/lihongjie/coal/cacheCtl/dto/CacheCtlDto.java b/src/main/java/cn/lihongjie/coal/cacheCtl/dto/CacheCtlDto.java new file mode 100644 index 00000000..9f379348 --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/cacheCtl/dto/CacheCtlDto.java @@ -0,0 +1,20 @@ +package cn.lihongjie.coal.cacheCtl.dto; + +import cn.lihongjie.coal.base.dto.CommonDto; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class CacheCtlDto extends CommonDto { + private String cacheName; + private String cacheKey; + private String cacheValue; + + public CacheCtlDto(String cacheName) { + this.cacheName = cacheName; + } +} diff --git a/src/main/java/cn/lihongjie/coal/cacheCtl/dto/CreateCacheCtlDto.java b/src/main/java/cn/lihongjie/coal/cacheCtl/dto/CreateCacheCtlDto.java new file mode 100644 index 00000000..b8fcffaf --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/cacheCtl/dto/CreateCacheCtlDto.java @@ -0,0 +1,12 @@ +package cn.lihongjie.coal.cacheCtl.dto; + +import cn.lihongjie.coal.base.dto.CommonDto; + +import lombok.Data; + +@Data +public class CreateCacheCtlDto extends CommonDto { + private String cacheName; + private String cacheKey; + private String cacheValue; +} diff --git a/src/main/java/cn/lihongjie/coal/cacheCtl/dto/UpdateCacheCtlDto.java b/src/main/java/cn/lihongjie/coal/cacheCtl/dto/UpdateCacheCtlDto.java new file mode 100644 index 00000000..0cafaf07 --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/cacheCtl/dto/UpdateCacheCtlDto.java @@ -0,0 +1,12 @@ +package cn.lihongjie.coal.cacheCtl.dto; + +import cn.lihongjie.coal.base.dto.CommonDto; + +import lombok.Data; + +@Data +public class UpdateCacheCtlDto extends CommonDto { + private String cacheName; + private String cacheKey; + private String cacheValue; +} diff --git a/src/main/java/cn/lihongjie/coal/cacheCtl/entity/CacheCtlEntity.java b/src/main/java/cn/lihongjie/coal/cacheCtl/entity/CacheCtlEntity.java new file mode 100644 index 00000000..80e48690 --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/cacheCtl/entity/CacheCtlEntity.java @@ -0,0 +1,16 @@ +package cn.lihongjie.coal.cacheCtl.entity; + +import cn.lihongjie.coal.base.entity.CommonEntity; + +import jakarta.persistence.Entity; + +import lombok.Data; + +@Data +@Entity +public class CacheCtlEntity extends CommonEntity { + + private String cacheName; + private String cacheKey; + private String cacheValue; +} diff --git a/src/main/java/cn/lihongjie/coal/cacheCtl/mapper/CacheCtlMapper.java b/src/main/java/cn/lihongjie/coal/cacheCtl/mapper/CacheCtlMapper.java new file mode 100644 index 00000000..b420535e --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/cacheCtl/mapper/CacheCtlMapper.java @@ -0,0 +1,18 @@ +package cn.lihongjie.coal.cacheCtl.mapper; + +import cn.lihongjie.coal.base.mapper.BaseMapper; +import cn.lihongjie.coal.base.mapper.CommonMapper; +import cn.lihongjie.coal.cacheCtl.dto.CacheCtlDto; +import cn.lihongjie.coal.cacheCtl.dto.CreateCacheCtlDto; +import cn.lihongjie.coal.cacheCtl.dto.UpdateCacheCtlDto; +import cn.lihongjie.coal.cacheCtl.entity.CacheCtlEntity; + +import org.mapstruct.Mapper; +import org.mapstruct.control.DeepClone; + +@Mapper( + componentModel = org.mapstruct.MappingConstants.ComponentModel.SPRING, + uses = {CommonMapper.class}, + mappingControl = DeepClone.class) +public interface CacheCtlMapper + extends BaseMapper {} diff --git a/src/main/java/cn/lihongjie/coal/cacheCtl/repository/CacheCtlRepository.java b/src/main/java/cn/lihongjie/coal/cacheCtl/repository/CacheCtlRepository.java new file mode 100644 index 00000000..f373c119 --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/cacheCtl/repository/CacheCtlRepository.java @@ -0,0 +1,9 @@ +package cn.lihongjie.coal.cacheCtl.repository; + +import cn.lihongjie.coal.base.dao.BaseRepository; +import cn.lihongjie.coal.cacheCtl.entity.CacheCtlEntity; + +import org.springframework.stereotype.Repository; + +@Repository +public interface CacheCtlRepository extends BaseRepository {} diff --git a/src/main/java/cn/lihongjie/coal/cacheCtl/service/CacheCtlService.java b/src/main/java/cn/lihongjie/coal/cacheCtl/service/CacheCtlService.java new file mode 100644 index 00000000..c1abcb9a --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/cacheCtl/service/CacheCtlService.java @@ -0,0 +1,110 @@ +package cn.lihongjie.coal.cacheCtl.service; + +import cn.lihongjie.coal.base.dto.CommonQuery; +import cn.lihongjie.coal.base.dto.IdRequest; +import cn.lihongjie.coal.base.service.BaseService; +import cn.lihongjie.coal.cacheCtl.dto.CacheCtlDto; +import cn.lihongjie.coal.cacheCtl.dto.CreateCacheCtlDto; +import cn.lihongjie.coal.cacheCtl.dto.UpdateCacheCtlDto; +import cn.lihongjie.coal.cacheCtl.entity.CacheCtlEntity; +import cn.lihongjie.coal.cacheCtl.mapper.CacheCtlMapper; +import cn.lihongjie.coal.cacheCtl.repository.CacheCtlRepository; +import cn.lihongjie.coal.exception.BizException; + +import com.google.common.base.Splitter; + +import lombok.extern.slf4j.Slf4j; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.CacheManager; +import org.springframework.core.convert.ConversionService; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +@Service +@Slf4j +public class CacheCtlService extends BaseService { + @Autowired CacheManager cacheManager; + @Autowired RedisTemplate redisTemplate; + @Autowired private CacheCtlRepository repository; + @Autowired private CacheCtlMapper mapper; + @Autowired private ConversionService conversionService; + + public CacheCtlDto create(CreateCacheCtlDto request) { + throw new BizException("不支持更新操作"); + } + + public CacheCtlDto update(UpdateCacheCtlDto request) { + + throw new BizException("不支持更新操作"); + } + + public void delete(IdRequest request) { + + cacheManager.getCache(request.getId()).invalidate(); + } + + public void deleteKey(CacheCtlDto request) { + + cacheManager.getCache(request.getId()).evictIfPresent(request.getCacheKey()); + } + + public CacheCtlDto getById(String id) { + CacheCtlEntity entity = repository.get(id); + + return mapper.toDto(entity); + } + + public Page list(CommonQuery query) { + + List collect = + cacheManager.getCacheNames().stream() + .map( + cacheName -> { + CacheCtlDto dto = new CacheCtlDto(cacheName); + dto.setId(cacheName); + + return dto; + }) + .collect(Collectors.toList()); + return new PageImpl<>( + collect, PageRequest.of(query.getPageNo(), query.getPageSize()), collect.size()); + } + + public List keys(CacheCtlDto request) { + + Set keys = redisTemplate.keys(request.getCacheName() + "::*"); + + return keys.stream() + .map( + x -> { + CacheCtlDto dto = new CacheCtlDto(request.getId()); + dto.setId(request.getId()); + dto.setCacheKey( + Splitter.on("::") + .trimResults() + .omitEmptyStrings() + .splitToList(x) + .get(1)); + return dto; + }) + .collect(Collectors.toList()); + } + + public CacheCtlDto value(CacheCtlDto request) { + + request.setCacheValue( + redisTemplate + .opsForValue() + .get(request.getCacheName() + "::" + request.getCacheKey())); + + return request; + } +}