commit 9b342f76c403a819ceb28fb3f931da466afbe60d Author: ZhangXushuo <3508242435.com> Date: Tue Oct 17 19:07:26 2023 +0800 初始化 diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..a27726c --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..78bf31c --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..354accf --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,16 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..beb1148 --- /dev/null +++ b/pom.xml @@ -0,0 +1,28 @@ + + + + bawei-common + com.bawei + 3.6.0 + + 4.0.0 + + bawei-common-cache + + + 8 + 8 + UTF-8 + + + + + + + com.bawei + bawei-common-redis + + + diff --git a/src/main/java/com/bawei/cache/BaseCache.java b/src/main/java/com/bawei/cache/BaseCache.java new file mode 100644 index 0000000..0e1b2ca --- /dev/null +++ b/src/main/java/com/bawei/cache/BaseCache.java @@ -0,0 +1,61 @@ +package com.bawei.cache; + +import com.bawei.common.core.utils.thread.ThreadPool; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * @author DongZl + * @description: 缓存基准 + * @Date 2022-10-18 下午 02:57 + */ +public interface BaseCache { + + public static final long time = 2; + + @Autowired + public ThreadPool threadPool = null; + + /** + * 获取Redis的key 用来 对缓存进行操作 + * @param key 数据Id + * @return + */ + public String getKey(K key); + + /** + * 增/改 + * @param key 键 + * @param val 值 + * @return + */ + public boolean put(K key, V val); + + /** + * 删 + * @param key 键 + * @return + */ + public boolean remove(K key); + + /** + * 延迟删 + * @param key 键 + */ + public default void delayRemove(K key){ + ThreadPool.getThreadPool().delayExecute(time, new Runnable() { + @Override + public void run () { + remove(key); + } + }); + } + + + /** + * 查 + * @param key 键 + * @return val 值 + */ + public V get(K key); + +} diff --git a/src/main/java/com/bawei/cache/annotation/CacheRole.java b/src/main/java/com/bawei/cache/annotation/CacheRole.java new file mode 100644 index 0000000..3dd7044 --- /dev/null +++ b/src/main/java/com/bawei/cache/annotation/CacheRole.java @@ -0,0 +1,22 @@ +package com.bawei.cache.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * @author DongZl + * @description: 缓存权限 + * @Date 2022-10-19 上午 08:34 + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.METHOD, ElementType.TYPE }) +public @interface CacheRole { + + /** + * 允许操作缓存的微服务 + * @return + */ + public String serverName() ; +} diff --git a/src/main/java/com/bawei/cache/aspect/CacheRuleAsp.java b/src/main/java/com/bawei/cache/aspect/CacheRuleAsp.java new file mode 100644 index 0000000..5601ab2 --- /dev/null +++ b/src/main/java/com/bawei/cache/aspect/CacheRuleAsp.java @@ -0,0 +1,70 @@ +package com.bawei.cache.aspect; + +import com.bawei.cache.annotation.CacheRole; +import com.bawei.common.core.exception.ServiceException; +import com.bawei.common.core.utils.StringUtils; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.aspectj.lang.reflect.MethodSignature; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import java.lang.reflect.Method; + +/** + * @author DongZl + * @description: 缓存操作权限切面 + * @Date 2022-10-19 上午 08:36 + */ +@Aspect +@Component +public class CacheRuleAsp { + + /** + * 当前服务的名称 + */ + @Value("${spring.application.name}") + private String applicationName; + + + /** + * 定义AOP签名 (切入所有使用鉴权注解的方法) + */ + public static final String POINTCUT_SIGN = "@annotation(com.bawei.cache.annotation.CacheRole)"; + + /** + * 声明AOP切点 + */ + @Pointcut(POINTCUT_SIGN) + public void pointcut() + { + } + + /** + * 环绕切入 + * + * @param joinPoint 切面对象 + * @return 底层方法执行后的返回值 + * @throws Throwable 底层方法抛出的异常 + */ + @Around("pointcut()") + public Object around(ProceedingJoinPoint joinPoint) throws Throwable{ + // 注解鉴权 + MethodSignature signature = (MethodSignature) joinPoint.getSignature(); + // 获取注解的方法 + Method method = signature.getMethod(); + // 获取方法上面的注解 + CacheRole cacheRole = method.getAnnotation(CacheRole.class); + if (cacheRole != null){ + // 判断当前微服务是否要操作权限 + if (!cacheRole.serverName().equals(applicationName)){ + throw new ServiceException(StringUtils.format("当前服务[{}],无权限操作服务[{}]的缓存", + applicationName, + cacheRole.serverName())); + } + } + return joinPoint.proceed(); + } +} diff --git a/src/main/java/com/bawei/cache/db/BaseDatabaseCache.java b/src/main/java/com/bawei/cache/db/BaseDatabaseCache.java new file mode 100644 index 0000000..9667126 --- /dev/null +++ b/src/main/java/com/bawei/cache/db/BaseDatabaseCache.java @@ -0,0 +1,25 @@ +package com.bawei.cache.db; + +import com.bawei.cache.BaseCache; + +/** + * @author DongZl + * @description: 数据库缓存基类 + * @Date 2022-10-18 下午 03:05 + */ +public interface BaseDatabaseCache extends BaseCache { + + /** + * 通过ID获取数据库内数据 + * @param key 数据ID + * @return + */ + public V getData(K key); + + /** + * 基于Key进行刷新 + * @param key + * @return + */ + public boolean refreshData(K key); +} diff --git a/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 0000000..6d75df5 --- /dev/null +++ b/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +com.bawei.cache.aspect.CacheRuleAsp diff --git a/target/bawei-common-cache-3.6.0.jar b/target/bawei-common-cache-3.6.0.jar new file mode 100644 index 0000000..0b8a268 Binary files /dev/null and b/target/bawei-common-cache-3.6.0.jar differ diff --git a/target/classes/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/target/classes/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 0000000..6d75df5 --- /dev/null +++ b/target/classes/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +com.bawei.cache.aspect.CacheRuleAsp diff --git a/target/classes/com/bawei/cache/BaseCache$1.class b/target/classes/com/bawei/cache/BaseCache$1.class new file mode 100644 index 0000000..d0d4c5e Binary files /dev/null and b/target/classes/com/bawei/cache/BaseCache$1.class differ diff --git a/target/classes/com/bawei/cache/BaseCache.class b/target/classes/com/bawei/cache/BaseCache.class new file mode 100644 index 0000000..4e1bb2e Binary files /dev/null and b/target/classes/com/bawei/cache/BaseCache.class differ diff --git a/target/classes/com/bawei/cache/annotation/CacheRole.class b/target/classes/com/bawei/cache/annotation/CacheRole.class new file mode 100644 index 0000000..dae3f81 Binary files /dev/null and b/target/classes/com/bawei/cache/annotation/CacheRole.class differ diff --git a/target/classes/com/bawei/cache/aspect/CacheRuleAsp.class b/target/classes/com/bawei/cache/aspect/CacheRuleAsp.class new file mode 100644 index 0000000..27b1e7e Binary files /dev/null and b/target/classes/com/bawei/cache/aspect/CacheRuleAsp.class differ diff --git a/target/classes/com/bawei/cache/db/BaseDatabaseCache.class b/target/classes/com/bawei/cache/db/BaseDatabaseCache.class new file mode 100644 index 0000000..1e297f1 Binary files /dev/null and b/target/classes/com/bawei/cache/db/BaseDatabaseCache.class differ diff --git a/target/maven-archiver/pom.properties b/target/maven-archiver/pom.properties new file mode 100644 index 0000000..d6440df --- /dev/null +++ b/target/maven-archiver/pom.properties @@ -0,0 +1,5 @@ +#Generated by Maven +#Sun Sep 17 16:49:31 CST 2023 +version=3.6.0 +groupId=com.bawei +artifactId=bawei-common-cache diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst new file mode 100644 index 0000000..13991c4 --- /dev/null +++ b/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst @@ -0,0 +1,5 @@ +com\bawei\cache\BaseCache$1.class +com\bawei\cache\BaseCache.class +com\bawei\cache\db\BaseDatabaseCache.class +com\bawei\cache\annotation\CacheRole.class +com\bawei\cache\aspect\CacheRuleAsp.class diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst new file mode 100644 index 0000000..c3fc8d8 --- /dev/null +++ b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst @@ -0,0 +1,4 @@ +D:\ruoyi\bawei-common\bawei-common-cache\src\main\java\com\bawei\cache\aspect\CacheRuleAsp.java +D:\ruoyi\bawei-common\bawei-common-cache\src\main\java\com\bawei\cache\db\BaseDatabaseCache.java +D:\ruoyi\bawei-common\bawei-common-cache\src\main\java\com\bawei\cache\BaseCache.java +D:\ruoyi\bawei-common\bawei-common-cache\src\main\java\com\bawei\cache\annotation\CacheRole.java