diff --git a/build.gradle b/build.gradle index 64ee48d34..9009d2bdf 100644 --- a/build.gradle +++ b/build.gradle @@ -38,7 +38,7 @@ configurations { bootJar { manifest { attributes("Implementation-Title": "Halo Application", - "Implementation-Version": archiveVersion) + "Implementation-Version": version) } } @@ -67,7 +67,6 @@ ext { githubApiVersion = "1.84" powermockVersion = "1.6.6" powermockApiMockito2 = "2.0.7" - hzVersion = "3.12" } dependencies { @@ -119,7 +118,6 @@ dependencies { implementation "org.iq80.leveldb:leveldb:$levelDbVersion" implementation "redis.clients:jedis:$jedisVersion" - implementation "com.hazelcast:hazelcast-all:$hzVersion" runtimeOnly "com.h2database:h2:$h2Version" runtimeOnly "mysql:mysql-connector-java" diff --git a/src/main/java/run/halo/app/cache/HazelcastStore.java b/src/main/java/run/halo/app/cache/HazelcastStore.java deleted file mode 100644 index d9523ca37..000000000 --- a/src/main/java/run/halo/app/cache/HazelcastStore.java +++ /dev/null @@ -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 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> getInternal(String key) { - Assert.hasText(key, "Cache key must not be blank"); - final IMap defaultHaloMap = getDefaultStringMap(); - final String v = defaultHaloMap.get(key); - return StringUtils.isBlank(v) ? Optional.empty() : jsonToCacheWrapper(v); - } - - @Override - void putInternal(String key, CacheWrapper 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 cacheWrapper) { - Assert.hasText(key, "Cache key must not be blank"); - Assert.notNull(cacheWrapper, "Cache wrapper must not be null"); - try { - final IMap 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 defaultHaloMap = getDefaultStringMap(); - defaultHaloMap.delete(key); - log.debug("Removed key: [{}]", key); - } - - private IMap getDefaultStringMap() { - return hazelcastInstance.getMap(DEFAULT_MAP); - } -} diff --git a/src/main/java/run/halo/app/config/HaloConfiguration.java b/src/main/java/run/halo/app/config/HaloConfiguration.java index ff32235a7..6ceddb780 100644 --- a/src/main/java/run/halo/app/config/HaloConfiguration.java +++ b/src/main/java/run/halo/app/config/HaloConfiguration.java @@ -58,9 +58,6 @@ public class HaloConfiguration { case "redis": stringCacheStore = new RedisCacheStore(this.haloProperties); break; - case "hazelcast": - stringCacheStore = new HazelcastStore(this.haloProperties); - break; case "memory": default: //memory or default diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index cae9be2e5..f3c517aa1 100755 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -52,8 +52,4 @@ logging: halo: download-timeout: 5m - cache: memory #hazelcast - ##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 + cache: memory \ No newline at end of file diff --git a/src/test/java/run/halo/app/cache/HazelcastStoreTest.java b/src/test/java/run/halo/app/cache/HazelcastStoreTest.java deleted file mode 100644 index ba7a8ee8b..000000000 --- a/src/test/java/run/halo/app/cache/HazelcastStoreTest.java +++ /dev/null @@ -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 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> optionalWrapperValue1 = hazelcastStore.getInternal("key1"); - - final CacheWrapper 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() { - } -}