From 6a5b6c137eb09c36103808b62533e0e668779b9d Mon Sep 17 00:00:00 2001
From: fst1996 <2411194573@qq.com>
Date: Fri, 15 Sep 2023 16:20:42 +0800
Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.gitignore | 47 +++++++++++++
pom.xml | 46 ++++++++++++
src/main/java/com/bawei/cache/BaseCache.java | 61 ++++++++++++++++
.../com/bawei/cache/annotation/CacheRole.java | 22 ++++++
.../com/bawei/cache/aspect/CacheRuleAsp.java | 70 +++++++++++++++++++
.../com/bawei/cache/db/BaseDatabaseCache.java | 25 +++++++
...ot.autoconfigure.AutoConfiguration.imports | 1 +
7 files changed, 272 insertions(+)
create mode 100644 .gitignore
create mode 100644 pom.xml
create mode 100644 src/main/java/com/bawei/cache/BaseCache.java
create mode 100644 src/main/java/com/bawei/cache/annotation/CacheRole.java
create mode 100644 src/main/java/com/bawei/cache/aspect/CacheRuleAsp.java
create mode 100644 src/main/java/com/bawei/cache/db/BaseDatabaseCache.java
create mode 100644 src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..05d7c76
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,47 @@
+######################################################################
+# Build Tools
+
+.gradle
+/build/
+!gradle/wrapper/gradle-wrapper.jar
+
+target/
+!.mvn/wrapper/maven-wrapper.jar
+
+######################################################################
+# IDE
+
+### STS ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+
+### IntelliJ IDEA ###
+.idea
+*.iws
+*.iml
+*.ipr
+
+### JRebel ###
+rebel.xml
+### NetBeans ###
+nbproject/private/
+build/*
+nbbuild/
+dist/
+nbdist/
+.nb-gradle/
+target/
+
+######################################################################
+# Others
+*.log
+*.xml.versionsBackup
+*.swp
+
+!*/build/*.java
+!*/build/*.html
+!*/build/*.xml
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..32d5f93
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,46 @@
+
+
+
+ menghang-common
+ com.bawei
+ 3.6.0
+
+ 4.0.0
+
+ 3.6.0
+ menghang-common-cache
+
+
+
+ menghang-public
+ 梦航-public
+ http://192.168.111.130:8081/repository/maven-public/
+
+
+
+
+
+ menghang-releases
+ 梦航-releases
+ http://192.168.111.130:8081/repository/maven-releases/
+
+
+
+
+ 8
+ 8
+ UTF-8
+
+
+
+
+
+
+ com.bawei
+ menghang-common-redis
+ 3.6.0
+
+
+
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