mirror of
https://gitee.com/dromara/sms4j.git
synced 2025-12-06 17:08:40 +08:00
添加一个RedisUtil接口,用户可以自主改变redis实现方式
This commit is contained in:
parent
cc45b7974b
commit
2d41f3da4b
2
pom.xml
2
pom.xml
@ -48,7 +48,7 @@
|
||||
</scm>
|
||||
|
||||
<properties>
|
||||
<revision>2.1.0-SNAPSHOT</revision>
|
||||
<revision>2.2.0-SNAPSHOT</revision>
|
||||
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
|
||||
@ -0,0 +1,22 @@
|
||||
package org.dromara.sms4j.api.universal;
|
||||
|
||||
public interface RedisUtil {
|
||||
|
||||
/**
|
||||
* setOrTime
|
||||
* <p>设置带有过期时间的key
|
||||
* @param key redis的key
|
||||
* @param value redis 的value
|
||||
* @param time 过期时间(秒级单位)
|
||||
* @author :Wind
|
||||
*/
|
||||
public boolean setOrTime(String key, Object value, Long time);
|
||||
|
||||
/**
|
||||
* getByKey
|
||||
* <p>根据key获取redis中缓存的数据
|
||||
* @param key redis的key
|
||||
* @author :Wind
|
||||
*/
|
||||
public Object getByKey(String key);
|
||||
}
|
||||
@ -2,22 +2,22 @@ package org.dromara.sms4j.solon.aop;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.api.smsProxy.RestrictedProcess;
|
||||
import org.dromara.sms4j.api.universal.RedisUtil;
|
||||
import org.dromara.sms4j.comm.config.SmsConfig;
|
||||
import org.dromara.sms4j.comm.exception.SmsBlendException;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtil;
|
||||
import org.dromara.sms4j.solon.utils.RedisUtils;
|
||||
import org.noear.solon.core.AopContext;
|
||||
|
||||
@Slf4j
|
||||
public class SolonRestrictedProcess extends RestrictedProcess {
|
||||
|
||||
private RedisUtils redis;
|
||||
private RedisUtil redis;
|
||||
private static final Long minTimer = 60 * 1000L;
|
||||
private static final Long accTimer = 24 * 60 * 60 * 1000L;
|
||||
private static final String REDIS_KEY = "sms:restricted:";
|
||||
|
||||
public SolonRestrictedProcess(AopContext context){
|
||||
context.getBeanAsync(RedisUtils.class, bean->{
|
||||
context.getBeanAsync(RedisUtil.class, bean->{
|
||||
redis = bean;
|
||||
});
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package org.dromara.sms4j.solon.utils;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.api.universal.RedisUtil;
|
||||
import org.noear.solon.Solon;
|
||||
import org.redisson.api.RedissonClient;
|
||||
|
||||
@ -9,7 +10,7 @@ import java.util.Objects;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Slf4j
|
||||
public class RedisUtils {
|
||||
public class RedisUtils implements RedisUtil {
|
||||
|
||||
private RedissonClient redisTemplate;
|
||||
|
||||
|
||||
@ -5,7 +5,7 @@ import org.dromara.sms4j.api.smsProxy.RestrictedProcess;
|
||||
import org.dromara.sms4j.comm.config.SmsConfig;
|
||||
import org.dromara.sms4j.comm.exception.SmsBlendException;
|
||||
import org.dromara.sms4j.comm.utils.SmsUtil;
|
||||
import org.dromara.sms4j.starter.utils.RedisUtils;
|
||||
import org.dromara.sms4j.api.universal.RedisUtil;
|
||||
import org.dromara.sms4j.starter.utils.SpringUtil;
|
||||
|
||||
import java.util.Objects;
|
||||
@ -19,7 +19,7 @@ public class RestrictedProcessImpl extends RestrictedProcess {
|
||||
|
||||
@Override
|
||||
public SmsBlendException process(SmsConfig config,String args) throws Exception {
|
||||
RedisUtils redis = SpringUtil.getBean(RedisUtils.class);
|
||||
RedisUtil redis = SpringUtil.getBean(RedisUtil.class);
|
||||
if (Objects.isNull(redis)){
|
||||
throw new SmsBlendException("The redis tool could not be found");
|
||||
}
|
||||
|
||||
@ -10,6 +10,7 @@ import org.dromara.sms4j.comm.delayedTime.DelayedTime;
|
||||
import org.dromara.sms4j.comm.factory.BeanFactory;
|
||||
import org.dromara.sms4j.core.SupplierSqlConfig;
|
||||
import org.dromara.sms4j.starter.aop.RestrictedProcessImpl;
|
||||
import org.dromara.sms4j.api.universal.RedisUtil;
|
||||
import org.dromara.sms4j.starter.utils.ConfigUtil;
|
||||
import org.dromara.sms4j.starter.utils.RedisUtils;
|
||||
import org.dromara.sms4j.starter.utils.SpringUtil;
|
||||
@ -76,7 +77,10 @@ public class SmsAutowiredConfig {
|
||||
void init(){
|
||||
/* 如果配置中启用了redis,则注入redis工具*/
|
||||
if (BeanFactory.getSmsConfig().getRedisCache()){
|
||||
//如果用户没有实现RedisUtil接口则注入默认的实现
|
||||
if (!SpringUtil.interfaceExist(RedisUtil.class)){
|
||||
springUtil.createBean(RedisUtils.class);
|
||||
}
|
||||
SmsInvocationHandler.setRestrictedProcess(new RestrictedProcessImpl());
|
||||
log.debug("The redis cache is enabled for sms4j");
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package org.dromara.sms4j.starter.utils;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.sms4j.api.universal.RedisUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
@ -13,7 +14,7 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
||||
@Slf4j
|
||||
public class RedisUtils {
|
||||
public class RedisUtils implements RedisUtil {
|
||||
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
|
||||
@ -90,4 +90,13 @@ public class SpringUtil implements ApplicationContextAware {
|
||||
public void deleteBean(String beanName) {
|
||||
beanFactory.removeBeanDefinition(beanName);
|
||||
}
|
||||
|
||||
/**
|
||||
* interfaceExist
|
||||
* <p>判断容器中是否存在某类型的bean
|
||||
* @author :Wind
|
||||
*/
|
||||
public static boolean interfaceExist(Class<?>interfaceType) {
|
||||
return !applicationContext.getBeansOfType(interfaceType).isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user