refactor: 缓存兼容redis单机模式

This commit is contained in:
fit2cloud-chenyw 2022-03-08 18:26:38 +08:00
parent 6ccc29ebfd
commit cb0e2b6a65
5 changed files with 127 additions and 11 deletions

View File

@ -280,6 +280,22 @@
<artifactId>json-path</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,23 @@
package io.dataease.commons.condition;
import io.dataease.commons.utils.CommonBeanFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class RedisStatusCondition implements Condition {
private static final String DEFAULT_TYPE = "ehcache";
private static final String TARGET_TYPE = "redis";
private static final String TYPE_KEY = "spring.cache.type";
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Environment environment = context.getEnvironment();
String ehcacheType = environment.getProperty(TYPE_KEY, String.class, DEFAULT_TYPE);
return StringUtils.equals(TARGET_TYPE, ehcacheType);
}
}

View File

@ -0,0 +1,27 @@
package io.dataease.config;
import io.dataease.commons.condition.RedisStatusCondition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
@Configuration
public class RedisConfig {
@Conditional({RedisStatusCondition.class})
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
redisTemplate.setDefaultSerializer(serializer);
return redisTemplate;
}
}

View File

@ -1,30 +1,46 @@
package io.dataease.listener.util;
import io.dataease.commons.utils.CommonBeanFactory;
import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.context.annotation.Configuration;
import java.util.Date;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import java.util.Date;
import java.util.concurrent.TimeUnit;
@Configuration
public class CacheUtils {
private static CacheManager manager;
private static CacheManager cacheManager;
@Autowired
public void setManager(CacheManager manager) {
CacheUtils.manager = manager;
private static CacheManager getCacheManager() {
if (cacheManager == null)
cacheManager = CommonBeanFactory.getBean(CacheManager.class);
return cacheManager;
}
public static Object get(String cacheName, Object key) {
if (getCacheManager() instanceof RedisCacheManager) {
org.springframework.cache.Cache cache = getCacheManager().getCache(cacheName);
if (null == cache) return null;
return cache.get(key);
}
Element element = cache(cacheName).get(key);
if (null == element) return null;
return element.getObjectValue();
}
public static void put(String cacheName, Object key, Object value, Integer ttl, Integer tti) {
if (getCacheManager() instanceof RedisCacheManager) {
RedisTemplate redisTemplate = (RedisTemplate) CommonBeanFactory.getBean("redisTemplate");
ValueOperations valueOperations = redisTemplate.opsForValue();
valueOperations.setIfPresent(cacheName + "::" + key , value );
return;
}
Element e = new Element(key, value);
//不设置则使用xml配置
if (ttl != null) {
@ -33,19 +49,35 @@ public class CacheUtils {
}
if (tti != null)
e.setTimeToIdle(tti);
cache(cacheName).put(e);
Cache cache = cache(cacheName);
if (null != cache)
cache.put(e);
}
public static boolean remove(String cacheName, Object key) {
if (getCacheManager() instanceof RedisCacheManager) {
org.springframework.cache.Cache cache = getCacheManager().getCache(cacheName);
if (null == cache) return false;
return cache.evictIfPresent(key);
}
return cache(cacheName).remove(key);
}
public static void removeAll(String cacheName) {
if (getCacheManager() instanceof RedisCacheManager) {
org.springframework.cache.Cache cache = getCacheManager().getCache(cacheName);
if (null == cache) return;
cache.clear();
return;
}
cache(cacheName).removeAll();
}
private static Cache cache(String cacheName) {
net.sf.ehcache.CacheManager cacheManager = ((EhCacheCacheManager) manager).getCacheManager();
if (getCacheManager() instanceof RedisCacheManager) {
return null;
}
net.sf.ehcache.CacheManager cacheManager = ((EhCacheCacheManager) getCacheManager()).getCacheManager();
if (!cacheManager.cacheExists(cacheName))
cacheManager.addCache(cacheName);
Cache cacheManagerCache = cacheManager.getCache(cacheName);
@ -57,6 +89,12 @@ public class CacheUtils {
long exp = (time - System.currentTimeMillis()) / 1000;
int intExp = (int)exp;
removeAll("lic_info");
if (getCacheManager() instanceof RedisCacheManager) {
RedisTemplate redisTemplate = (RedisTemplate) CommonBeanFactory.getBean("redisTemplate");
ValueOperations valueOperations = redisTemplate.opsForValue();
valueOperations.set("lic_info::lic", "lic", exp, TimeUnit.SECONDS);
return;
}
put("lic_info", "lic", "lic", intExp, intExp);
}
}

View File

@ -67,7 +67,7 @@ management.endpoints.web.exposure.include=*
#RSA非对称加密参数私钥
rsa.private_key=MIIBUwIBADANBgkqhkiG9w0BAQEFAASCAT0wggE5AgEAAkEA0vfvyTdGJkdbHkB8mp0f3FE0GYP3AYPaJF7jUd1M0XxFSE2ceK3k2kw20YvQ09NJKk+OMjWQl9WitG9pB6tSCQIDAQABAkA2SimBrWC2/wvauBuYqjCFwLvYiRYqZKThUS3MZlebXJiLB+Ue/gUifAAKIg1avttUZsHBHrop4qfJCwAI0+YRAiEA+W3NK/RaXtnRqmoUUkb59zsZUBLpvZgQPfj1MhyHDz0CIQDYhsAhPJ3mgS64NbUZmGWuuNKp5coY2GIj/zYDMJp6vQIgUueLFXv/eZ1ekgz2Oi67MNCk5jeTF2BurZqNLR3MSmUCIFT3Q6uHMtsB9Eha4u7hS31tj1UWE+D+ADzp59MGnoftAiBeHT7gDMuqeJHPL4b+kC+gzV4FGTfhR9q3tTbklZkD2A==
rsa.public_key=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANL378k3RiZHWx5AfJqdH9xRNBmD9wGD2iRe41HdTNF8RUhNnHit5NpMNtGL0NPTSSpPjjI1kJfVorRvaQerUgkCAwEAAQ==
spring.cache.type=ehcache
#spring.cache.type=ehcache
spring.cache.ehcache.config=classpath:/ehcache/ehcache.xml
#打印URL路径
#logging.level.org.springframework.web=trace
@ -85,6 +85,18 @@ server.compression.enabled=true
server.compression.mime-types=application/javascript,text/css,application/json,application/xml,text/html,text/xml,text/plain
server.compression.min-response-size=1024
#下面的配置新增到/opt/dataease/conf/dataease/properties
#spring.redis.database=0
#spring.redis.host=192.168.0.110
#spring.redis.port=6379
#spring.redis.password=DataEase_ZNB@REDIS
#spring.redis.timeout=10000
#spring.redis.lettuce.pool.max-active=8
#spring.redis.lettuce.pool.max-wait=-1
#spring.redis.lettuce.pool.max-idle=8
##spring.cache.type=redis
#spring.cache.type=ehcache