初始化
commit
6a5b6c137e
|
@ -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
|
|
@ -0,0 +1,46 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>menghang-common</artifactId>
|
||||||
|
<groupId>com.bawei</groupId>
|
||||||
|
<version>3.6.0</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<version>3.6.0</version>
|
||||||
|
<artifactId>menghang-common-cache</artifactId>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>menghang-public</id>
|
||||||
|
<name>梦航-public</name>
|
||||||
|
<url>http://192.168.111.130:8081/repository/maven-public/</url>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
|
||||||
|
<distributionManagement>
|
||||||
|
<repository>
|
||||||
|
<id>menghang-releases</id>
|
||||||
|
<name>梦航-releases</name>
|
||||||
|
<url>http://192.168.111.130:8081/repository/maven-releases/</url>
|
||||||
|
</repository>
|
||||||
|
</distributionManagement>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>8</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<!-- 项目redis缓存 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.bawei</groupId>
|
||||||
|
<artifactId>menghang-common-redis</artifactId>
|
||||||
|
<version>3.6.0</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
|
@ -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<K, V> {
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
}
|
|
@ -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() ;
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<K,V> extends BaseCache<K,V> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过ID获取数据库内数据
|
||||||
|
* @param key 数据ID
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public V getData(K key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 基于Key进行刷新
|
||||||
|
* @param key
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public boolean refreshData(K key);
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
com.bawei.cache.aspect.CacheRuleAsp
|
Loading…
Reference in New Issue