mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 07:46:40 +08:00
缓存相关接口
This commit is contained in:
@@ -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<CacheCtlDto> list(@RequestBody CommonQuery request) {
|
||||
return this.service.list(request);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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<CacheCtlEntity, CacheCtlDto, CreateCacheCtlDto, UpdateCacheCtlDto> {}
|
||||
@@ -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<CacheCtlEntity> {}
|
||||
@@ -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<CacheCtlEntity, CacheCtlRepository> {
|
||||
@Autowired CacheManager cacheManager;
|
||||
@Autowired RedisTemplate<String, String> 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<CacheCtlDto> list(CommonQuery query) {
|
||||
|
||||
List<CacheCtlDto> 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<CacheCtlDto> keys(CacheCtlDto request) {
|
||||
|
||||
Set<String> 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user