Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mingchiuli committed Oct 19, 2024
1 parent 0decf1b commit ce7d59d
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 134 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import org.chiu.micro.auth.config.CacheUserEvictRabbitConfig;
import org.chiu.micro.auth.constant.UserAuthMenuOperateMessage;
import org.chiu.micro.auth.wrapper.AuthWrapper;
import org.chiu.micro.common.cache.config.CommonCacheKeyGenerator;
import org.chiu.micro.common.lang.AuthMenuOperateEnum;
import org.redisson.api.RedissonClient;
import org.chiu.micro.auth.cache.config.CacheKeyGenerator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -30,7 +30,7 @@ public class UserRedisCacheEvictMessageListener {

private static final Logger log = LoggerFactory.getLogger(UserRedisCacheEvictMessageListener.class);

private final CacheKeyGenerator cacheKeyGenerator;
private final CommonCacheKeyGenerator commonCacheKeyGenerator;

private final RedissonClient redissonClient;

Expand All @@ -39,8 +39,8 @@ public class UserRedisCacheEvictMessageListener {
private static final String QUEUE = "user.auth.menu.change.queue.auth";


public UserRedisCacheEvictMessageListener(CacheKeyGenerator cacheKeyGenerator, RedissonClient redissonClient, RabbitTemplate rabbitTemplate) {
this.cacheKeyGenerator = cacheKeyGenerator;
public UserRedisCacheEvictMessageListener(CommonCacheKeyGenerator commonCacheKeyGenerator, RedissonClient redissonClient, RabbitTemplate rabbitTemplate) {
this.commonCacheKeyGenerator = commonCacheKeyGenerator;
this.redissonClient = redissonClient;
this.rabbitTemplate = rabbitTemplate;
}
Expand All @@ -58,7 +58,7 @@ public void handler(UserAuthMenuOperateMessage message, Channel channel, Message
try {
method = AuthWrapper.class.getMethod("getCurrentUserNav", String.class);
for (String role : roles) {
keys.add(cacheKeyGenerator.generateKey(method, role));
keys.add(commonCacheKeyGenerator.generateKey(method, role));
}
} catch (NoSuchMethodException e) {
log.error("some error", e);
Expand All @@ -71,7 +71,7 @@ public void handler(UserAuthMenuOperateMessage message, Channel channel, Message
try {
getAuthoritiesByRoleCodeMethod = AuthWrapper.class.getMethod("getAuthoritiesByRoleCode", String.class);
for (String role : roles) {
keys.add(cacheKeyGenerator.generateKey(getAuthoritiesByRoleCodeMethod, role));
keys.add(commonCacheKeyGenerator.generateKey(getAuthoritiesByRoleCodeMethod, role));
}
} catch (NoSuchMethodException e) {
log.error("some error", e);
Expand All @@ -80,7 +80,7 @@ public void handler(UserAuthMenuOperateMessage message, Channel channel, Message
Method getAllSystemAuthoritiesMethod;
try {
getAllSystemAuthoritiesMethod = AuthWrapper.class.getMethod("getAllSystemAuthorities");
keys.add(cacheKeyGenerator.generateKey(getAllSystemAuthoritiesMethod));
keys.add(commonCacheKeyGenerator.generateKey(getAllSystemAuthoritiesMethod));
} catch (NoSuchMethodException e) {
log.error("some error", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ public record BlogSensitiveContentRpcDto(

Long blogId,

List<SensitiveContentDto> sensitiveContent) implements Serializable {
List<SensitiveContentRpcDto> sensitiveContent) implements Serializable {

public static BlogSensitiveContentDtoBuilder builder() {
return new BlogSensitiveContentDtoBuilder();
}

public static class BlogSensitiveContentDtoBuilder {
private Long blogId;
private List<SensitiveContentDto> sensitiveContentDto;
private List<SensitiveContentRpcDto> sensitiveContentRpcDto;

public BlogSensitiveContentDtoBuilder blogId(Long blogId) {
this.blogId = blogId;
return this;
}

public BlogSensitiveContentDtoBuilder sensitiveContent(List<SensitiveContentDto> sensitiveContentDto) {
this.sensitiveContentDto = sensitiveContentDto;
public BlogSensitiveContentDtoBuilder sensitiveContent(List<SensitiveContentRpcDto> sensitiveContentRpcDto) {
this.sensitiveContentRpcDto = sensitiveContentRpcDto;
return this;
}

public BlogSensitiveContentRpcDto build() {
return new BlogSensitiveContentRpcDto(blogId, sensitiveContentDto);
return new BlogSensitiveContentRpcDto(blogId, sensitiveContentRpcDto);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.io.Serializable;

public record SensitiveContentDto(
public record SensitiveContentRpcDto(

Integer startIndex,

Expand Down Expand Up @@ -35,8 +35,8 @@ public SensitiveContentBuilder type(Integer type) {
return this;
}

public SensitiveContentDto build() {
return new SensitiveContentDto(startIndex, endIndex, type);
public SensitiveContentRpcDto build() {
return new SensitiveContentRpcDto(startIndex, endIndex, type);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package org.chiu.micro.exhibit.cache.config;

import org.chiu.micro.common.utils.JsonUtils;
import org.chiu.micro.common.cache.config.CommonCacheKeyGenerator;
import org.chiu.micro.exhibit.wrapper.BlogWrapper;
import org.chiu.micro.common.cache.Cache;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
Expand All @@ -23,44 +20,16 @@
public class CacheKeyGenerator {

private static final Logger log = LoggerFactory.getLogger(CacheKeyGenerator.class);
private final JsonUtils jsonUtils;

private final CommonCacheKeyGenerator commonCacheKeyGenerator;

@Value("${megalith.blog.blog-page-size}")
private int blogPageSize;

private static final String FIND_PAGE = "findPage";

public CacheKeyGenerator(JsonUtils jsonUtils) {
this.jsonUtils = jsonUtils;
}

public String generateKey(Method method, Object... args) {

Class<?> declaringType = method.getDeclaringClass();
String methodName = method.getName();

var params = new StringBuilder();
for (Object arg : args) {
if (Objects.nonNull(arg)) {
params.append("::");
if (arg instanceof String) {
params.append(arg);
} else {
params.append(jsonUtils.writeValueAsString(arg));
}
}
}

String className = declaringType.getSimpleName();
var annotation = method.getAnnotation(Cache.class);
String prefix = null;
if (Objects.nonNull(annotation)) {
prefix = annotation.prefix().getInfo();
}

return StringUtils.hasLength(prefix) ?
prefix + "::" + className + "::" + methodName + params :
className + "::" + methodName + params;
public CacheKeyGenerator(CommonCacheKeyGenerator commonCacheKeyGenerator) {
this.commonCacheKeyGenerator = commonCacheKeyGenerator;
}

public Set<String> generateHotBlogsKeys(Integer year, Long count, Long countYear) {
Expand All @@ -72,7 +41,7 @@ public Set<String> generateHotBlogsKeys(Integer year, Long count, Long countYear
Method method;
try {
method = BlogWrapper.class.getMethod(FIND_PAGE, Integer.class, Integer.class);
String key = generateKey(method, i, Integer.MIN_VALUE);
String key = commonCacheKeyGenerator.generateKey(method, i, Integer.MIN_VALUE);
keys.add(key);
} catch (NoSuchMethodException e) {
log.error("some exception happen...", e);
Expand All @@ -83,7 +52,7 @@ public Set<String> generateHotBlogsKeys(Integer year, Long count, Long countYear
Method method;
try {
method = BlogWrapper.class.getMethod(FIND_PAGE, Integer.class, Integer.class);
String key = generateKey(method, i, year);
String key = commonCacheKeyGenerator.generateKey(method, i, year);
keys.add(key);
} catch (NoSuchMethodException e) {
log.error("some exception happen...", e);
Expand All @@ -101,7 +70,7 @@ public Set<String> generateBlogKey(long countAfter, long countYearAfter, Integer
Method method;
try {
method = BlogWrapper.class.getMethod(FIND_PAGE, Integer.class, Integer.class);
String key = generateKey(method, i, Integer.MIN_VALUE);
String key = commonCacheKeyGenerator.generateKey(method, i, Integer.MIN_VALUE);
keys.add(key);
} catch (NoSuchMethodException e) {
log.error("some exception happen...", e);
Expand All @@ -112,7 +81,7 @@ public Set<String> generateBlogKey(long countAfter, long countYearAfter, Integer
Method method;
try {
method = BlogWrapper.class.getMethod(FIND_PAGE, Integer.class, Integer.class);
String key = generateKey(method, i, year);
String key = commonCacheKeyGenerator.generateKey(method, i, year);
keys.add(key);
} catch (NoSuchMethodException e) {
log.error("some exception happen...", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.chiu.micro.exhibit.cache.handler;

import org.chiu.micro.common.cache.config.CommonCacheKeyGenerator;
import org.chiu.micro.common.dto.BlogEntityRpcDto;
import org.chiu.micro.common.utils.KeyUtils;
import org.chiu.micro.exhibit.cache.config.CacheKeyGenerator;
Expand Down Expand Up @@ -28,17 +29,22 @@ public final class DeleteBlogCacheEvictHandler extends BlogCacheEvictHandler {


private static final Logger log = LoggerFactory.getLogger(DeleteBlogCacheEvictHandler.class);

private final CacheKeyGenerator cacheKeyGenerator;

private final CommonCacheKeyGenerator commonCacheKeyGenerator;

@Value("${megalith.blog.blog-page-size}")
private int blogPageSize;

public DeleteBlogCacheEvictHandler(RedissonClient redissonClient,
BlogHttpServiceWrapper blogHttpServiceWrapper,
CacheKeyGenerator cacheKeyGenerator,
RabbitTemplate rabbitTemplate) {
RabbitTemplate rabbitTemplate,
CommonCacheKeyGenerator commonCacheKeyGenerator) {
super(redissonClient, blogHttpServiceWrapper, rabbitTemplate);
this.cacheKeyGenerator = cacheKeyGenerator;
this.commonCacheKeyGenerator = commonCacheKeyGenerator;
}

@Override
Expand All @@ -55,31 +61,31 @@ public Set<String> redisProcess(BlogEntityRpcDto blogEntity) {
//博客对象本身缓存
try {
Method findByIdMethod = BlogWrapper.class.getMethod("findById", Long.class);
String findById = cacheKeyGenerator.generateKey(findByIdMethod, id);
String findById = commonCacheKeyGenerator.generateKey(findByIdMethod, id);
keys.add(findById);
} catch (NoSuchMethodException e) {
log.error(e.getMessage());
}

try {
Method getCountByYearMethod = BlogWrapper.class.getMethod("getCountByYear", Integer.class);
String getCountByYear = cacheKeyGenerator.generateKey(getCountByYearMethod, year);
String getCountByYear = commonCacheKeyGenerator.generateKey(getCountByYearMethod, year);
keys.add(getCountByYear);
} catch (NoSuchMethodException e) {
log.error(e.getMessage());
}

try {
Method statusMethod = BlogWrapper.class.getMethod("findStatusById", Long.class);
String status = cacheKeyGenerator.generateKey(statusMethod, id);
String status = commonCacheKeyGenerator.generateKey(statusMethod, id);
keys.add(status);
} catch (NoSuchMethodException e) {
log.error(e.getMessage());
}

try {
Method sensitiveMethod = BlogSensitiveWrapper.class.getMethod("findSensitiveByBlogId", Long.class);
String sensitive = cacheKeyGenerator.generateKey(sensitiveMethod, id);
String sensitive = commonCacheKeyGenerator.generateKey(sensitiveMethod, id);
keys.add(sensitive);
} catch (NoSuchMethodException e) {
log.error(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.chiu.micro.exhibit.cache.handler;

import org.chiu.micro.common.cache.config.CommonCacheKeyGenerator;
import org.chiu.micro.common.dto.BlogEntityRpcDto;
import org.chiu.micro.common.utils.KeyUtils;
import org.chiu.micro.exhibit.cache.config.CacheKeyGenerator;
Expand Down Expand Up @@ -27,13 +28,17 @@ public final class UpdateBlogCacheEvictHandler extends BlogCacheEvictHandler {
private static final Logger log = LoggerFactory.getLogger(UpdateBlogCacheEvictHandler.class);
private final CacheKeyGenerator cacheKeyGenerator;

private final CommonCacheKeyGenerator commonCacheKeyGenerator;


public UpdateBlogCacheEvictHandler(RedissonClient redissonClient,
BlogHttpServiceWrapper blogHttpServiceWrapper,
CacheKeyGenerator cacheKeyGenerator,
RabbitTemplate rabbitTemplate) {
RabbitTemplate rabbitTemplate,
CommonCacheKeyGenerator commonCacheKeyGenerator) {
super(redissonClient, blogHttpServiceWrapper, rabbitTemplate);
this.cacheKeyGenerator = cacheKeyGenerator;
this.commonCacheKeyGenerator = commonCacheKeyGenerator;
}

@Override
Expand All @@ -58,7 +63,7 @@ public Set<String> redisProcess(BlogEntityRpcDto blogEntity) {
//博客对象本身缓存
try {
Method findByIdAndVisibleMethod = BlogWrapper.class.getMethod("findById", Long.class);
String findByIdAndVisible = cacheKeyGenerator.generateKey(findByIdAndVisibleMethod, id);
String findByIdAndVisible = commonCacheKeyGenerator.generateKey(findByIdAndVisibleMethod, id);
keys.add(findByIdAndVisible);
} catch (NoSuchMethodException e) {
log.error(e.getMessage());
Expand All @@ -67,15 +72,15 @@ public Set<String> redisProcess(BlogEntityRpcDto blogEntity) {

try {
Method statusMethod = BlogWrapper.class.getMethod("findStatusById", Long.class);
String statusKey = cacheKeyGenerator.generateKey(statusMethod, id);
String statusKey = commonCacheKeyGenerator.generateKey(statusMethod, id);
keys.add(statusKey);
} catch (NoSuchMethodException e) {
log.error(e.getMessage());
}

try {
Method sensitiveMethod = BlogSensitiveWrapper.class.getMethod("findSensitiveByBlogId", Long.class);
String sensitive = cacheKeyGenerator.generateKey(sensitiveMethod, id);
String sensitive = commonCacheKeyGenerator.generateKey(sensitiveMethod, id);
keys.add(sensitive);
} catch (NoSuchMethodException e) {
log.error(e.getMessage());
Expand Down
Loading

0 comments on commit ce7d59d

Please sign in to comment.