mirror of https://github.com/halo-dev/halo
chore: remove hazelcast cache store. (#1076)
parent
d60a940a62
commit
fc5742eae8
|
@ -38,7 +38,7 @@ configurations {
|
||||||
bootJar {
|
bootJar {
|
||||||
manifest {
|
manifest {
|
||||||
attributes("Implementation-Title": "Halo Application",
|
attributes("Implementation-Title": "Halo Application",
|
||||||
"Implementation-Version": archiveVersion)
|
"Implementation-Version": version)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,7 +67,6 @@ ext {
|
||||||
githubApiVersion = "1.84"
|
githubApiVersion = "1.84"
|
||||||
powermockVersion = "1.6.6"
|
powermockVersion = "1.6.6"
|
||||||
powermockApiMockito2 = "2.0.7"
|
powermockApiMockito2 = "2.0.7"
|
||||||
hzVersion = "3.12"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
@ -119,7 +118,6 @@ dependencies {
|
||||||
|
|
||||||
implementation "org.iq80.leveldb:leveldb:$levelDbVersion"
|
implementation "org.iq80.leveldb:leveldb:$levelDbVersion"
|
||||||
implementation "redis.clients:jedis:$jedisVersion"
|
implementation "redis.clients:jedis:$jedisVersion"
|
||||||
implementation "com.hazelcast:hazelcast-all:$hzVersion"
|
|
||||||
runtimeOnly "com.h2database:h2:$h2Version"
|
runtimeOnly "com.h2database:h2:$h2Version"
|
||||||
runtimeOnly "mysql:mysql-connector-java"
|
runtimeOnly "mysql:mysql-connector-java"
|
||||||
|
|
||||||
|
|
|
@ -1,130 +0,0 @@
|
||||||
package run.halo.app.cache;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
||||||
import com.hazelcast.client.HazelcastClient;
|
|
||||||
import com.hazelcast.client.config.ClientConfig;
|
|
||||||
import com.hazelcast.client.config.ClientConnectionStrategyConfig;
|
|
||||||
import com.hazelcast.client.config.ClientNetworkConfig;
|
|
||||||
import com.hazelcast.client.config.ConnectionRetryConfig;
|
|
||||||
import com.hazelcast.config.GroupConfig;
|
|
||||||
import com.hazelcast.core.HazelcastInstance;
|
|
||||||
import com.hazelcast.core.IMap;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
|
||||||
import org.springframework.util.Assert;
|
|
||||||
import run.halo.app.config.properties.HaloProperties;
|
|
||||||
import run.halo.app.utils.JsonUtils;
|
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* hazelcast cache store
|
|
||||||
* Create by turgay can on 2020/08/27 10:28
|
|
||||||
*/
|
|
||||||
@Slf4j
|
|
||||||
public class HazelcastStore extends AbstractStringCacheStore {
|
|
||||||
|
|
||||||
private static final int ONE_SECOND_AS_MILLIS = 1000;
|
|
||||||
private static final String DEFAULT_MAP = "haloMap";
|
|
||||||
|
|
||||||
private HazelcastInstance hazelcastInstance;
|
|
||||||
|
|
||||||
public HazelcastStore(HaloProperties haloProperties) {
|
|
||||||
super.haloProperties = haloProperties;
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostConstruct
|
|
||||||
public void init() {
|
|
||||||
if (hazelcastInstance != null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
final ClientConfig config = new ClientConfig();
|
|
||||||
final GroupConfig groupConfig = config.getGroupConfig();
|
|
||||||
final String hazelcastGroupName = haloProperties.getHazelcastGroupName();
|
|
||||||
groupConfig.setName(hazelcastGroupName);
|
|
||||||
|
|
||||||
final ClientNetworkConfig network = config.getNetworkConfig();
|
|
||||||
final List<String> hazelcastMembers = haloProperties.getHazelcastMembers();
|
|
||||||
network.setAddresses(hazelcastMembers);
|
|
||||||
|
|
||||||
configureClientRetryPolicy(config);
|
|
||||||
|
|
||||||
log.info("Hazelcast client instance starting::GroupName={}::Members={}", hazelcastGroupName, hazelcastMembers);
|
|
||||||
this.hazelcastInstance = HazelcastClient.newHazelcastClient(config);
|
|
||||||
log.info("Hazelcast client instance started");
|
|
||||||
} catch (Exception ex) {
|
|
||||||
log.error("init hazelcast error ", ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void configureClientRetryPolicy(ClientConfig config) {
|
|
||||||
ConnectionRetryConfig retryConfig = new ConnectionRetryConfig();
|
|
||||||
retryConfig.setEnabled(true);
|
|
||||||
retryConfig.setInitialBackoffMillis(haloProperties.getInitialBackoffSeconds() * ONE_SECOND_AS_MILLIS);
|
|
||||||
|
|
||||||
config.getConnectionStrategyConfig()
|
|
||||||
.setReconnectMode(ClientConnectionStrategyConfig.ReconnectMode.ON)
|
|
||||||
.setConnectionRetryConfig(retryConfig);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
Optional<CacheWrapper<String>> getInternal(String key) {
|
|
||||||
Assert.hasText(key, "Cache key must not be blank");
|
|
||||||
final IMap<String, String> defaultHaloMap = getDefaultStringMap();
|
|
||||||
final String v = defaultHaloMap.get(key);
|
|
||||||
return StringUtils.isBlank(v) ? Optional.empty() : jsonToCacheWrapper(v);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
void putInternal(String key, CacheWrapper<String> cacheWrapper) {
|
|
||||||
putInternalIfAbsent(key, cacheWrapper);
|
|
||||||
try {
|
|
||||||
getDefaultStringMap().set(key, JsonUtils.objectToJson(cacheWrapper));
|
|
||||||
Date ttl = cacheWrapper.getExpireAt();
|
|
||||||
if (ttl != null) {
|
|
||||||
getDefaultStringMap().setTtl(key, ttl.getTime(), TimeUnit.MILLISECONDS);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.warn("Put cache fail json2object key: [{}] value:[{}]", key, cacheWrapper);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
Boolean putInternalIfAbsent(String key, CacheWrapper<String> cacheWrapper) {
|
|
||||||
Assert.hasText(key, "Cache key must not be blank");
|
|
||||||
Assert.notNull(cacheWrapper, "Cache wrapper must not be null");
|
|
||||||
try {
|
|
||||||
final IMap<String, String> defaultHaloMap = getDefaultStringMap();
|
|
||||||
if (defaultHaloMap.containsKey(key)) {
|
|
||||||
log.warn("Failed to put the cache, because the key: [{}] has been present already", key);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
Date ttl = cacheWrapper.getExpireAt();
|
|
||||||
if (ttl != null) {
|
|
||||||
defaultHaloMap.set(key, JsonUtils.objectToJson(cacheWrapper), ttl.getTime(), TimeUnit.MILLISECONDS);
|
|
||||||
}
|
|
||||||
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(String key) {
|
|
||||||
Assert.hasText(key, "Cache key must not be blank");
|
|
||||||
final IMap<String, String> defaultHaloMap = getDefaultStringMap();
|
|
||||||
defaultHaloMap.delete(key);
|
|
||||||
log.debug("Removed key: [{}]", key);
|
|
||||||
}
|
|
||||||
|
|
||||||
private IMap<String, String> getDefaultStringMap() {
|
|
||||||
return hazelcastInstance.getMap(DEFAULT_MAP);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -58,9 +58,6 @@ public class HaloConfiguration {
|
||||||
case "redis":
|
case "redis":
|
||||||
stringCacheStore = new RedisCacheStore(this.haloProperties);
|
stringCacheStore = new RedisCacheStore(this.haloProperties);
|
||||||
break;
|
break;
|
||||||
case "hazelcast":
|
|
||||||
stringCacheStore = new HazelcastStore(this.haloProperties);
|
|
||||||
break;
|
|
||||||
case "memory":
|
case "memory":
|
||||||
default:
|
default:
|
||||||
//memory or default
|
//memory or default
|
||||||
|
|
|
@ -52,8 +52,4 @@ logging:
|
||||||
|
|
||||||
halo:
|
halo:
|
||||||
download-timeout: 5m
|
download-timeout: 5m
|
||||||
cache: memory #hazelcast
|
cache: memory
|
||||||
##hazelcast configs##
|
|
||||||
#hazelcastMembers: 127.0.0.1:22055 #127.0.0.1:5701 #10.4.5.100:5701,10.4.5.101:5701,10.4.5.102:5701
|
|
||||||
#hazelcastGroupName: tt-mps-hz-cluster #halo-hz-cluster
|
|
||||||
#initialBackoffSeconds: 5
|
|
|
@ -1,68 +0,0 @@
|
||||||
package run.halo.app.cache;
|
|
||||||
|
|
||||||
import cn.hutool.core.date.DateTime;
|
|
||||||
import cn.hutool.core.date.DateUtil;
|
|
||||||
import com.hazelcast.core.HazelcastInstance;
|
|
||||||
import com.hazelcast.core.IMap;
|
|
||||||
import org.apache.commons.lang3.time.DateUtils;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Disabled;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
|
||||||
import org.mockito.InjectMocks;
|
|
||||||
import org.mockito.Mock;
|
|
||||||
import org.mockito.junit.jupiter.MockitoExtension;
|
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
@Disabled("Due to project test run exclusion")
|
|
||||||
@ExtendWith(MockitoExtension.class)
|
|
||||||
class HazelcastStoreTest {
|
|
||||||
|
|
||||||
@InjectMocks
|
|
||||||
private HazelcastStore hazelcastStore;
|
|
||||||
|
|
||||||
@Mock
|
|
||||||
private HazelcastInstance hazelcastInstance;
|
|
||||||
|
|
||||||
private IMap<Object, Object> haloMap;
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
public void initEach() {
|
|
||||||
haloMap = hazelcastInstance.getMap("haloMap");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void should_getInternal_For_Key1() {
|
|
||||||
final DateTime createAt = DateUtil.date();
|
|
||||||
final Date expireAt = DateUtils.addMinutes(createAt, 5);
|
|
||||||
final String value = "{ \"data\": {\"name\": \"halo\"}, \"expireAt\": \"" + expireAt + "\", \"createAt\": \"" + createAt + "\" }";
|
|
||||||
when(haloMap.get("key1")).thenReturn(value);
|
|
||||||
|
|
||||||
final Optional<CacheWrapper<String>> optionalWrapperValue1 = hazelcastStore.getInternal("key1");
|
|
||||||
|
|
||||||
final CacheWrapper<String> wrapperValue1 = optionalWrapperValue1.get();
|
|
||||||
assertNotNull(optionalWrapperValue1);
|
|
||||||
|
|
||||||
assertEquals("{\"name\": \"halo\"}", wrapperValue1.getData());
|
|
||||||
assertEquals(DateUtil.formatDate(createAt), DateUtil.formatDate(wrapperValue1.getCreateAt()));
|
|
||||||
assertEquals(DateUtil.formatDate(expireAt), DateUtil.formatDate(wrapperValue1.getExpireAt()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void putInternal() {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void putInternalIfAbsent() {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void delete() {
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue