mirror of https://github.com/halo-dev/halo
chore: remove redis cache store (#1190)
* chore: remove redis cache store. * chore: remove redis cache store. * chore: update build.gradle.pull/1203/head
parent
30e28b5257
commit
9951a83ad6
|
@ -63,12 +63,9 @@ ext {
|
|||
h2Version = "1.4.196"
|
||||
levelDbVersion = "0.12"
|
||||
annotationsVersion = "3.0.1u2"
|
||||
jedisVersion = "3.3.0"
|
||||
zxingVersion = "3.4.0"
|
||||
huaweiObsVersion = "3.19.7"
|
||||
githubApiVersion = "1.84"
|
||||
powermockVersion = "1.6.6"
|
||||
powermockApiMockito2 = "2.0.7"
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
@ -120,7 +117,6 @@ dependencies {
|
|||
implementation "com.google.zxing:core:$zxingVersion"
|
||||
|
||||
implementation "org.iq80.leveldb:leveldb:$levelDbVersion"
|
||||
implementation "redis.clients:jedis:$jedisVersion"
|
||||
runtimeOnly "com.h2database:h2:$h2Version"
|
||||
runtimeOnly "mysql:mysql-connector-java"
|
||||
|
||||
|
@ -133,8 +129,6 @@ dependencies {
|
|||
testImplementation("org.springframework.boot:spring-boot-starter-test") {
|
||||
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
|
||||
}
|
||||
// testImplementation group: 'org.powermock', name: 'powermock-api-mockito2', version: "$powermockApiMockito2"
|
||||
// testImplementation group: 'org.powermock', name: 'powermock-module-junit4', version: "$powermockVersion"
|
||||
|
||||
developmentOnly "org.springframework.boot:spring-boot-devtools"
|
||||
}
|
||||
|
|
|
@ -1,128 +0,0 @@
|
|||
package run.halo.app.cache;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
import redis.clients.jedis.HostAndPort;
|
||||
import redis.clients.jedis.JedisCluster;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
import run.halo.app.config.properties.HaloProperties;
|
||||
import run.halo.app.utils.JsonUtils;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
/**
|
||||
* Redis cache store.
|
||||
*
|
||||
* @author chaos
|
||||
*/
|
||||
@Slf4j
|
||||
public class RedisCacheStore extends AbstractStringCacheStore {
|
||||
/**
|
||||
* Cache container.
|
||||
*/
|
||||
private final static ConcurrentHashMap<String, CacheWrapper<String>> CACHE_CONTAINER = new ConcurrentHashMap<>();
|
||||
|
||||
private volatile static JedisCluster REDIS;
|
||||
|
||||
/**
|
||||
* Lock.
|
||||
*/
|
||||
private final Lock lock = new ReentrantLock();
|
||||
|
||||
public RedisCacheStore(HaloProperties haloProperties) {
|
||||
this.haloProperties = haloProperties;
|
||||
initRedis();
|
||||
}
|
||||
|
||||
private void initRedis() {
|
||||
JedisPoolConfig cfg = new JedisPoolConfig();
|
||||
cfg.setMaxIdle(2);
|
||||
cfg.setMaxTotal(30);
|
||||
cfg.setMaxWaitMillis(5000);
|
||||
Set<HostAndPort> nodes = new HashSet<>();
|
||||
for (String hostPort : this.haloProperties.getCacheRedisNodes()) {
|
||||
String[] temp = hostPort.split(":");
|
||||
if (temp.length > 0) {
|
||||
String host = temp[0];
|
||||
int port = 6379;
|
||||
if (temp.length > 1) {
|
||||
try {
|
||||
port = Integer.parseInt(temp[1]);
|
||||
} catch (Exception ex) {
|
||||
|
||||
}
|
||||
}
|
||||
nodes.add(new HostAndPort(host, port));
|
||||
}
|
||||
}
|
||||
if (nodes.isEmpty()) {
|
||||
nodes.add(new HostAndPort("127.0.0.1", 6379));
|
||||
}
|
||||
REDIS = new JedisCluster(nodes, 5, 20, 3, this.haloProperties.getCacheRedisPassword(), cfg);
|
||||
log.info("Initialized cache redis cluster: {}", REDIS.getClusterNodes());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
Optional<CacheWrapper<String>> getInternal(@NotNull String key) {
|
||||
Assert.hasText(key, "Cache key must not be blank");
|
||||
String v = REDIS.get(key);
|
||||
return StringUtils.isEmpty(v) ? Optional.empty() : jsonToCacheWrapper(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
void putInternal(@NotNull String key, @NotNull CacheWrapper<String> cacheWrapper) {
|
||||
putInternalIfAbsent(key, cacheWrapper);
|
||||
try {
|
||||
REDIS.set(key, JsonUtils.objectToJson(cacheWrapper));
|
||||
Date ttl = cacheWrapper.getExpireAt();
|
||||
if (ttl != null) {
|
||||
REDIS.pexpireAt(key, ttl.getTime());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("Put cache fail json2object key: [{}] value:[{}]", key, cacheWrapper);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
Boolean putInternalIfAbsent(@NotNull String key, @NotNull CacheWrapper<String> cacheWrapper) {
|
||||
Assert.hasText(key, "Cache key must not be blank");
|
||||
Assert.notNull(cacheWrapper, "Cache wrapper must not be null");
|
||||
try {
|
||||
if (REDIS.setnx(key, JsonUtils.objectToJson(cacheWrapper)) <= 0) {
|
||||
log.warn("Failed to put the cache, because the key: [{}] has been present already", key);
|
||||
return false;
|
||||
}
|
||||
Date ttl = cacheWrapper.getExpireAt();
|
||||
if (ttl != null) {
|
||||
REDIS.pexpireAt(key, ttl.getTime());
|
||||
}
|
||||
return true;
|
||||
} catch (JsonProcessingException e) {
|
||||
log.warn("Put cache fail json2object key: [{}] value:[{}]", key, cacheWrapper);
|
||||
}
|
||||
log.debug("Cache key: [{}], original cache wrapper: [{}]", key, cacheWrapper);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(@NotNull String key) {
|
||||
Assert.hasText(key, "Cache key must not be blank");
|
||||
REDIS.del(key);
|
||||
log.debug("Removed key: [{}]", key);
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void preDestroy() {
|
||||
}
|
||||
}
|
|
@ -17,7 +17,6 @@ import org.springframework.web.client.RestTemplate;
|
|||
import run.halo.app.cache.AbstractStringCacheStore;
|
||||
import run.halo.app.cache.InMemoryCacheStore;
|
||||
import run.halo.app.cache.LevelCacheStore;
|
||||
import run.halo.app.cache.RedisCacheStore;
|
||||
import run.halo.app.config.properties.HaloProperties;
|
||||
import run.halo.app.repository.base.BaseRepositoryImpl;
|
||||
import run.halo.app.utils.HttpClientUtils;
|
||||
|
@ -65,9 +64,6 @@ public class HaloConfiguration {
|
|||
case "level":
|
||||
stringCacheStore = new LevelCacheStore(this.haloProperties);
|
||||
break;
|
||||
case "redis":
|
||||
stringCacheStore = new RedisCacheStore(this.haloProperties);
|
||||
break;
|
||||
case "memory":
|
||||
default:
|
||||
//memory or default
|
||||
|
|
|
@ -34,8 +34,3 @@ halo:
|
|||
|
||||
# memory, level, redis
|
||||
cache: memory
|
||||
|
||||
# if cache = redis, you need to set the following options
|
||||
# cache-redis-nodes: ['127.0.0.1:6380', '127.0.0.1:6379']
|
||||
# cache-redis-password: 123456
|
||||
|
||||
|
|
Loading…
Reference in New Issue