Skip to content

Commit

Permalink
optimize caffeine cache (#875)
Browse files Browse the repository at this point in the history
  优 caffeine cache

  移动cache到common模块

  修改clear实现

  修改clear实现

  perfect comment

  add cache test

---------

Co-authored-by: hujiaofen <hujiaofen@2dfire.com>
  • Loading branch information
Ceilzcx and hujiaofen authored Apr 13, 2023
1 parent a967e79 commit a9f8aff
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 106 deletions.
8 changes: 8 additions & 0 deletions common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<jetcd-core.version>0.5.11</jetcd-core.version>
<protobuf-java-util.version>3.19.1</protobuf-java-util.version>
<tencentcloud-sdk-java-sms.version>3.1.648</tencentcloud-sdk-java-sms.version>
<caffeine.version>2.9.3</caffeine.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -113,5 +114,12 @@
<artifactId>tencentcloud-sdk-java-sms</artifactId>
<version>${tencentcloud-sdk-java-sms.version}</version>
</dependency>
<!-- caffeine-->
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<!-- 3.x版本支持Java11及以上, 因此这边还是使用2.x的版本-->
<version>${caffeine.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
* limitations under the License.
*/

package org.dromara.hertzbeat.manager.cache;
package org.dromara.hertzbeat.common.cache;

import java.time.Duration;

/**
*
Expand All @@ -24,12 +26,14 @@
public class CacheFactory {
private CacheFactory() {}

private static final ICacheService<String, Object> NOTICE_CACHE =
new CaffeineCacheServiceImpl<>(200, 1000, Duration.ofDays(1), false);

/**
* 获取默认的cache
* todo 后续优化
* 获取notice模块的cache
* @return caffeine cache
*/
public static ICacheService getCache() {
return new CaffeineCacheServiceImpl();
public static ICacheService<String, Object> getNoticeCache() {
return NOTICE_CACHE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.dromara.hertzbeat.common.cache;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

import java.time.Duration;

/**
*
* @since 4/2/2023
*/
public class CaffeineCacheServiceImpl<K, V> implements ICacheService<K, V> {
private final Cache<K, V> cache;

public CaffeineCacheServiceImpl(final int initialCapacity, final long maximumSize, final Duration expireAfterWrite, final boolean useWeakKey) {
if (useWeakKey) {
this.cache = Caffeine.newBuilder()
.weakKeys()
.initialCapacity(initialCapacity)
.maximumSize(maximumSize)
.expireAfterWrite(expireAfterWrite)
.build();
} else {
this.cache = Caffeine.newBuilder()
.initialCapacity(initialCapacity)
.maximumSize(maximumSize)
.expireAfterWrite(expireAfterWrite)
.build();
}
}

@Override
public V get(K key) {
return cache.getIfPresent(key);
}

@Override
public V put(K key, V value) {
V oldValue = cache.getIfPresent(key);
cache.put(key, value);
return oldValue;
}

@Override
public boolean containsKey(K key) {
return cache.asMap().containsKey(key);
}

@Override
public V remove(K key) {
V value = cache.getIfPresent(key);
this.cache.invalidate(key);
this.cache.cleanUp();
return value;
}

@Override
public boolean clear() {
this.cache.invalidateAll();
this.cache.cleanUp();
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,47 @@
* limitations under the License.
*/

package org.dromara.hertzbeat.manager.cache;
package org.dromara.hertzbeat.common.cache;

/**
* common cache
*
* @since 4/2/2023
*/
public interface ICacheService {

/**
* get cache by key
* @param key key
* @return cache object
*/
Object get(Object key);
public interface ICacheService<K, V> {

/**
* get cache by key use clazz
* @param key key
* @param clazz clazz
* @return object
* @param <T> t
*/
<T> T get(Object key, Class<T> clazz);
V get(K key);

/**
* set cache
* @param key key
* @param value value
* @return old value
*/
void put(Object key, Object value);
V put(K key, V value);

/**
* if contain cache by key
* @param key key
* @return true is contain
*/
boolean containsKey(Object key);
boolean containsKey(K key);

/**
* remove cache
* @param key key
* @return old value
*/
V remove(K key);

/**
* clear cache
* @return is clear success
*/
void remove(Object key);
boolean clear();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.dromara.hertzbeat.common.cache;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.time.Duration;

/**
*
* @since 13/4/2023
*/
class CaffeineCacheTest {
private ICacheService<String, String> cacheService;

@BeforeEach
void setUp() {
cacheService = new CaffeineCacheServiceImpl<>(10, 100, Duration.ofMillis(3000), false);
}

@Test
void testCache() throws InterruptedException {
String key = "key";
String value = "value";

// test get & put
cacheService.put(key, value);
Assertions.assertEquals(value, cacheService.get(key));
Assertions.assertTrue(cacheService.containsKey(key));

// test remove
cacheService.remove(key);
Assertions.assertNull(cacheService.get(key));

// test expire time
cacheService.put(key, value);
Thread.sleep(3000);
Assertions.assertNull(cacheService.get(key));
Assertions.assertNull(cacheService.get(key));

// test clear
for (int i = 0; i < 10; i++) {
cacheService.put(key + i, value);
}
cacheService.clear();
for (int i = 0; i < 10; i++) {
Assertions.assertNull(cacheService.get(key + i));
}
}

}
8 changes: 0 additions & 8 deletions manager/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

<properties>
<alerter.version>1.0</alerter.version>
<caffeine.version>2.9.3</caffeine.version>
<collector.version>1.0</collector.version>
<common.version>1.0</common.version>
<h2.version>2.1.214</h2.version>
Expand Down Expand Up @@ -137,13 +136,6 @@
<version>${h2.version}</version>
<scope>runtime</scope>
</dependency>
<!-- caffeine-->
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<!-- 3.x版本支持Java11及以上, 因此这边还是使用2.x的版本-->
<version>${caffeine.version}</version>
</dependency>
</dependencies>

<build>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

package org.dromara.hertzbeat.manager.service.impl;

import org.dromara.hertzbeat.common.cache.CacheFactory;
import org.dromara.hertzbeat.common.cache.ICacheService;
import org.dromara.hertzbeat.common.entity.alerter.Alert;
import org.dromara.hertzbeat.common.util.CommonConstants;
import org.dromara.hertzbeat.manager.cache.CacheFactory;
import org.dromara.hertzbeat.manager.cache.ICacheService;
import org.dromara.hertzbeat.manager.component.alerter.DispatcherAlarm;
import org.dromara.hertzbeat.manager.dao.NoticeReceiverDao;
import org.dromara.hertzbeat.manager.dao.NoticeRuleDao;
Expand Down Expand Up @@ -113,11 +113,11 @@ public void deleteNoticeRule(Long ruleId) {
@SuppressWarnings("unchecked")
public List<NoticeReceiver> getReceiverFilterRule(Alert alert) {
// use cache
ICacheService commonCache = CacheFactory.getCache();
List<NoticeRule> rules = (List<NoticeRule>) commonCache.get(CommonConstants.CACHE_NOTICE_RULE);
ICacheService<String, Object> noticeCache = CacheFactory.getNoticeCache();
List<NoticeRule> rules = (List<NoticeRule>) noticeCache.get(CommonConstants.CACHE_NOTICE_RULE);
if (rules == null) {
rules = noticeRuleDao.findNoticeRulesByEnableTrue();
commonCache.put(CommonConstants.CACHE_NOTICE_RULE, rules);
noticeCache.put(CommonConstants.CACHE_NOTICE_RULE, rules);
}

// The temporary rule is to forward all, and then implement more matching rules: alarm status selection, monitoring type selection, etc.
Expand Down Expand Up @@ -193,7 +193,7 @@ public boolean sendTestMsg(NoticeReceiver noticeReceiver) {
}

private void clearNoticeRulesCache() {
ICacheService cache = CacheFactory.getCache();
cache.remove(CommonConstants.CACHE_NOTICE_RULE);
ICacheService<String, Object> noticeCache = CacheFactory.getNoticeCache();
noticeCache.remove(CommonConstants.CACHE_NOTICE_RULE);
}
}

0 comments on commit a9f8aff

Please sign in to comment.