优化别名逻辑.

This commit is contained in:
lijiahangmax
2024-02-29 23:25:00 +08:00
parent 44dd5a9079
commit 1c6a38d5d9
18 changed files with 304 additions and 354 deletions

View File

@@ -1,55 +0,0 @@
package com.orion.ops.module.infra.api;
import com.orion.ops.module.infra.entity.dto.data.DataAliasUpdateDTO;
import com.orion.ops.module.infra.enums.DataExtraTypeEnum;
import java.util.Map;
import java.util.concurrent.Future;
/**
* 数据别名 对外服务类
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-12-18 17:37
*/
public interface DataAliasApi {
/**
* 更新数据别名
*
* @param dto dto
* @param type type
* @return effect
*/
Integer updateDataAlias(DataAliasUpdateDTO dto, DataExtraTypeEnum type);
/**
* 查询数据别名
*
* @param userId userId
* @param type type
* @param relId relId
* @return aliasName
*/
String getDataAlias(Long userId, DataExtraTypeEnum type, Long relId);
/**
* 查询数据别名
*
* @param userId userId
* @param type type
* @return relId:aliasName
*/
Map<Long, String> getDataAlias(Long userId, DataExtraTypeEnum type);
/**
* 异步查询数据别名
*
* @param userId userId
* @param type type
* @return relId:aliasName
*/
Future<Map<Long, String>> getDataAliasAsync(Long userId, DataExtraTypeEnum type);
}

View File

@@ -7,6 +7,7 @@ import com.orion.ops.module.infra.enums.DataExtraTypeEnum;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
/**
* 数据拓展信息 对外服务类
@@ -69,6 +70,37 @@ public interface DataExtraApi {
*/
Map<Long, String> getExtraItemValues(DataExtraQueryDTO dto, DataExtraTypeEnum type);
/**
* 查询额外配置项 (查询缓存)
*
* @param userId userId
* @param type type
* @param item item
* @param relId relId
* @return value
*/
String getExtraItemValueByCache(Long userId, DataExtraTypeEnum type, String item, Long relId);
/**
* 查询额外配置项 (查询缓存)
*
* @param userId userId
* @param type type
* @param item item
* @return relId:value
*/
Map<Long, String> getExtraItemValuesByCache(Long userId, DataExtraTypeEnum type, String item);
/**
* 异步查询额外配置项 (查询缓存)
*
* @param userId userId
* @param type type
* @param item item
* @return value
*/
Future<Map<Long, String>> getExtraItemValuesByCacheAsync(Long userId, DataExtraTypeEnum type, String item);
/**
* 查询额外配置
*

View File

@@ -11,4 +11,6 @@ public interface DataExtraItems {
String ALIAS = "alias";
String COLOR = "color";
}

View File

@@ -1,42 +0,0 @@
package com.orion.ops.module.infra.entity.dto.data;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
/**
* 数据别名 更新请求业务对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-12-18 17:37
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "DataAliasUpdateDTO", description = "数据别名 创建请求业务对象")
public class DataAliasUpdateDTO implements Serializable {
private static final long serialVersionUID = 1L;
@NotNull
@Schema(description = "用户id")
private Long userId;
@NotNull
@Schema(description = "数据id")
private Long relId;
@NotNull
@Size(max = 32)
@Schema(description = "别名")
private String alias;
}

View File

@@ -1,63 +0,0 @@
package com.orion.ops.module.infra.api.impl;
import com.orion.ops.framework.common.utils.Valid;
import com.orion.ops.module.infra.api.DataAliasApi;
import com.orion.ops.module.infra.entity.dto.data.DataAliasUpdateDTO;
import com.orion.ops.module.infra.entity.request.data.DataAliasUpdateRequest;
import com.orion.ops.module.infra.enums.DataExtraTypeEnum;
import com.orion.ops.module.infra.service.DataAliasService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
/**
* 数据别名 对外服务实现类
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-12-18 17:37
*/
@Slf4j
@Service
public class DataAliasApiImpl implements DataAliasApi {
@Resource
private DataAliasService dataAliasService;
@Override
public Integer updateDataAlias(DataAliasUpdateDTO dto, DataExtraTypeEnum type) {
Valid.valid(dto);
DataAliasUpdateRequest update = DataAliasUpdateRequest.builder()
.userId(dto.getUserId())
.type(type.name())
.relId(dto.getRelId())
.alias(dto.getAlias())
.build();
return dataAliasService.updateDataAlias(update);
}
@Override
public String getDataAlias(Long userId, DataExtraTypeEnum type, Long relId) {
Valid.allNotNull(userId, relId);
return dataAliasService.getDataAlias(userId, type.name(), relId);
}
@Override
public Map<Long, String> getDataAlias(Long userId, DataExtraTypeEnum type) {
Valid.notNull(userId);
return dataAliasService.getDataAlias(userId, type.name());
}
@Override
@Async("asyncExecutor")
public Future<Map<Long, String>> getDataAliasAsync(Long userId, DataExtraTypeEnum type) {
Valid.notNull(userId);
return CompletableFuture.completedFuture(dataAliasService.getDataAlias(userId, type.name()));
}
}

View File

@@ -18,6 +18,8 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.stream.Collectors;
/**
@@ -83,6 +85,23 @@ public class DataExtraApiImpl implements DataExtraApi {
return dataExtraService.getExtraItemValues(request);
}
@Override
public String getExtraItemValueByCache(Long userId, DataExtraTypeEnum type, String item, Long relId) {
Valid.allNotNull(userId, type, item, relId);
return dataExtraService.getExtraItemValueByCache(userId, type.name(), item, relId);
}
@Override
public Map<Long, String> getExtraItemValuesByCache(Long userId, DataExtraTypeEnum type, String item) {
Valid.allNotNull(userId, type, item);
return dataExtraService.getExtraItemValuesByCache(userId, type.name(), item);
}
@Override
public Future<Map<Long, String>> getExtraItemValuesByCacheAsync(Long userId, DataExtraTypeEnum type, String item) {
return CompletableFuture.completedFuture(this.getExtraItemValuesByCache(userId, type, item));
}
@Override
public DataExtraDTO getExtraItem(DataExtraQueryDTO dto, DataExtraTypeEnum type) {
Valid.allNotNull(dto.getUserId(), dto.getRelId(), dto.getItem());

View File

@@ -15,9 +15,9 @@ import java.util.concurrent.TimeUnit;
*/
public interface DataExtraCacheKeyDefine {
CacheKeyDefine DATA_ALIAS = new CacheKeyBuilder()
.key("data:alias:{}:{}")
.desc("数据别名 ${userId} ${type}")
CacheKeyDefine DATA_EXTRA = new CacheKeyBuilder()
.key("data:extra:{}:{}:{}")
.desc("数据推展信息 ${userId} ${type} ${item}")
.type(String.class)
.struct(RedisCacheStruct.HASH)
.timeout(1, TimeUnit.DAYS)

View File

@@ -1,47 +0,0 @@
package com.orion.ops.module.infra.entity.request.data;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
/**
* 数据别名 更新请求对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-12-18 17:37
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "DataAliasUpdateRequest", description = "数据别名 更新请求对象")
public class DataAliasUpdateRequest implements Serializable {
private static final long serialVersionUID = 1L;
@NotNull
@Schema(description = "用户id")
private Long userId;
@NotNull
@Schema(description = "数据id")
private Long relId;
@NotBlank
@Size(max = 32)
@Schema(description = "数据类型")
private String type;
@Size(max = 32)
@Schema(description = "别名")
private String alias;
}

View File

@@ -1,43 +0,0 @@
package com.orion.ops.module.infra.service;
import com.orion.ops.module.infra.entity.request.data.DataAliasUpdateRequest;
import java.util.Map;
/**
* 数据别名 服务类
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-12-18 17:37
*/
public interface DataAliasService {
/**
* 更新数据别名
*
* @param request request
* @return effect
*/
Integer updateDataAlias(DataAliasUpdateRequest request);
/**
* 查询数据别名
*
* @param userId userId
* @param type type
* @param relId relId
* @return aliasName
*/
String getDataAlias(Long userId, String type, Long relId);
/**
* 查询数据别名
*
* @param userId userId
* @param type type
* @return relId:aliasName
*/
Map<Long, String> getDataAlias(Long userId, String type);
}

View File

@@ -64,6 +64,27 @@ public interface DataExtraService {
*/
Map<Long, String> getExtraItemValues(DataExtraQueryRequest request);
/**
* 查询额外配置项 (查询缓存)
*
* @param userId userId
* @param type type
* @param item item
* @param relId relId
* @return value
*/
String getExtraItemValueByCache(Long userId, String type, String item, Long relId);
/**
* 查询额外配置项 (查询缓存)
*
* @param userId userId
* @param type type
* @param item item
* @return relId:value
*/
Map<Long, String> getExtraItemValuesByCache(Long userId, String type, String item);
/**
* 查询额外配置
*

View File

@@ -1,82 +0,0 @@
package com.orion.ops.module.infra.service.impl;
import com.orion.lang.utils.Refs;
import com.orion.lang.utils.collect.Maps;
import com.orion.ops.framework.redis.core.utils.RedisMaps;
import com.orion.ops.framework.redis.core.utils.barrier.CacheBarriers;
import com.orion.ops.module.infra.constant.DataExtraItems;
import com.orion.ops.module.infra.define.cache.DataExtraCacheKeyDefine;
import com.orion.ops.module.infra.entity.request.data.DataAliasUpdateRequest;
import com.orion.ops.module.infra.entity.request.data.DataExtraQueryRequest;
import com.orion.ops.module.infra.entity.request.data.DataExtraSetRequest;
import com.orion.ops.module.infra.service.DataAliasService;
import com.orion.ops.module.infra.service.DataExtraService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Map;
import java.util.function.Function;
/**
* 数据别名 服务实现类
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023-12-18 17:37
*/
@Slf4j
@Service
public class DataAliasServiceImpl implements DataAliasService {
@Resource
private DataExtraService dataExtraService;
@Override
public Integer updateDataAlias(DataAliasUpdateRequest request) {
Long userId = request.getUserId();
String type = request.getType();
// 更新
DataExtraSetRequest update = new DataExtraSetRequest();
update.setUserId(userId);
update.setRelId(request.getRelId());
update.setType(type);
update.setItem(DataExtraItems.ALIAS);
update.setValue(Refs.json(request.getAlias()));
Integer effect = dataExtraService.setExtraItem(update);
// 删除缓存
RedisMaps.delete(DataExtraCacheKeyDefine.DATA_ALIAS.format(userId, type));
return effect;
}
@Override
public String getDataAlias(Long userId, String type, Long relId) {
return this.getDataAlias(userId, type).get(relId);
}
@Override
public Map<Long, String> getDataAlias(Long userId, String type) {
// 查询缓存
String key = DataExtraCacheKeyDefine.DATA_ALIAS.format(userId, type);
Map<String, String> entities = RedisMaps.entities(key);
if (Maps.isEmpty(entities)) {
// 查询数据库
DataExtraQueryRequest request = DataExtraQueryRequest.builder()
.userId(userId)
.type(type)
.item(DataExtraItems.ALIAS)
.build();
Map<Long, String> extras = dataExtraService.getExtraItemValues(request);
entities = Maps.map(extras, String::valueOf, Refs::unrefToString);
// 设置屏障 防止穿透
CacheBarriers.MAP.check(entities);
// 设置缓存
RedisMaps.putAll(key, DataExtraCacheKeyDefine.DATA_ALIAS, entities);
}
// 删除屏障
CacheBarriers.MAP.remove(entities);
// 转换
return Maps.map(entities, Long::valueOf, Function.identity());
}
}

View File

@@ -2,9 +2,14 @@ package com.orion.ops.module.infra.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.orion.lang.function.Functions;
import com.orion.lang.utils.collect.Lists;
import com.orion.lang.utils.collect.Maps;
import com.orion.ops.framework.common.constant.Const;
import com.orion.ops.framework.mybatis.core.query.ThenLambdaWrapper;
import com.orion.ops.framework.redis.core.utils.RedisMaps;
import com.orion.ops.framework.redis.core.utils.barrier.CacheBarriers;
import com.orion.ops.module.infra.dao.DataExtraDAO;
import com.orion.ops.module.infra.define.cache.DataExtraCacheKeyDefine;
import com.orion.ops.module.infra.entity.domain.DataExtraDO;
import com.orion.ops.module.infra.entity.request.data.DataExtraQueryRequest;
import com.orion.ops.module.infra.entity.request.data.DataExtraSetRequest;
@@ -15,6 +20,7 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
@@ -62,15 +68,29 @@ public class DataExtraServiceImpl implements DataExtraService {
insert.setItem(request.getItem());
insert.setValue(request.getValue());
dataExtraDAO.insert(insert);
// 删除缓存
RedisMaps.delete(DataExtraCacheKeyDefine.DATA_EXTRA.format(request.getUserId(), request.getType(), request.getItem()));
return insert.getId();
}
@Override
public Integer updateExtraValue(Long id, String value) {
// 查询数据
DataExtraDO data = this.getCacheSelectWrapper()
.eq(DataExtraDO::getId, id)
.then()
.get();
if (data == null) {
return Const.N_0;
}
DataExtraDO update = new DataExtraDO();
update.setId(id);
update.setValue(value);
return dataExtraDAO.updateById(update);
// 更新
int effect = dataExtraDAO.updateById(update);
// 删除缓存
RedisMaps.delete(DataExtraCacheKeyDefine.DATA_EXTRA.format(data.getUserId(), data.getType(), data.getItem()));
return effect;
}
@Override
@@ -78,8 +98,16 @@ public class DataExtraServiceImpl implements DataExtraService {
if (Maps.isEmpty(map)) {
return;
}
// 查询数据
List<DataExtraDO> list = this.getCacheSelectWrapper()
.in(DataExtraDO::getId, map.keySet())
.then()
.list();
if (list.isEmpty()) {
return;
}
// 批量更新
List<DataExtraDO> list = map.entrySet()
List<DataExtraDO> update = map.entrySet()
.stream()
.map(s -> {
DataExtraDO extra = new DataExtraDO();
@@ -87,7 +115,9 @@ public class DataExtraServiceImpl implements DataExtraService {
extra.setValue(s.getValue());
return extra;
}).collect(Collectors.toList());
dataExtraDAO.updateBatch(list);
dataExtraDAO.updateBatch(update);
// 删除缓存
this.deleteCache(list);
}
@Override
@@ -110,6 +140,37 @@ public class DataExtraServiceImpl implements DataExtraService {
);
}
@Override
public String getExtraItemValueByCache(Long userId, String type, String item, Long relId) {
return this.getExtraItemValuesByCache(userId, type, item).get(relId);
}
@Override
public Map<Long, String> getExtraItemValuesByCache(Long userId, String type, String item) {
// todo TEST
// 查询缓存
String key = DataExtraCacheKeyDefine.DATA_EXTRA.format(userId, type, item);
Map<String, String> entities = RedisMaps.entities(key);
if (Maps.isEmpty(entities)) {
// 查询数据库
DataExtraQueryRequest request = DataExtraQueryRequest.builder()
.userId(userId)
.type(type)
.item(item)
.build();
Map<Long, String> extras = this.getExtraItemValues(request);
entities = Maps.map(extras, String::valueOf, String::valueOf);
// 设置屏障 防止穿透
CacheBarriers.MAP.check(entities);
// 设置缓存
RedisMaps.putAll(key, DataExtraCacheKeyDefine.DATA_EXTRA, entities);
}
// 删除屏障
CacheBarriers.MAP.remove(entities);
// 转换
return Maps.map(entities, Long::valueOf, Function.identity());
}
@Override
public DataExtraDO getExtraItem(DataExtraQueryRequest request) {
return dataExtraDAO.of()
@@ -126,12 +187,64 @@ public class DataExtraServiceImpl implements DataExtraService {
@Override
public Integer deleteByUserId(Long userId) {
return dataExtraDAO.deleteByUserId(userId);
// 查询数据 TODO TEST
List<DataExtraDO> list = this.getCacheSelectWrapper()
.eq(DataExtraDO::getUserId, userId)
.then()
.list();
if (list.isEmpty()) {
return Const.N_0;
}
// 删除数据
int effect = dataExtraDAO.deleteByUserId(userId);
// 删除缓存
this.deleteCache(list);
return effect;
}
@Override
public Integer deleteByRelId(String type, Long relId) {
return dataExtraDAO.deleteByRelId(type, relId);
// 查询数据 TODO TEST
List<DataExtraDO> list = this.getCacheSelectWrapper()
.eq(DataExtraDO::getType, type)
.eq(DataExtraDO::getRelId, relId)
.then()
.list();
if (list.isEmpty()) {
return Const.N_0;
}
// 删除数据
int effect = dataExtraDAO.deleteByRelId(type, relId);
// 删除缓存
this.deleteCache(list);
return effect;
}
/**
* 获取查询缓存参数 wrapper 不查询 longtext 加速查询
*
* @return wrapper
*/
private ThenLambdaWrapper<DataExtraDO> getCacheSelectWrapper() {
return dataExtraDAO.of()
.createWrapper()
.select(DataExtraDO::getId, DataExtraDO::getUserId, DataExtraDO::getType, DataExtraDO::getItem);
}
/**
* 删除缓存
*
* @param list list
*/
private void deleteCache(List<DataExtraDO> list) {
if (Lists.isEmpty(list)) {
return;
}
List<String> keys = list.stream()
.map(s -> DataExtraCacheKeyDefine.DATA_EXTRA.format(s.getUserId(), s.getType(), s.getItem()))
.distinct()
.collect(Collectors.toList());
RedisMaps.delete(keys);
}
/**