-
Notifications
You must be signed in to change notification settings - Fork 999
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
优 caffeine cache 移动cache到common模块 修改clear实现 修改clear实现 perfect comment add cache test --------- Co-authored-by: hujiaofen <hujiaofen@2dfire.com>
- Loading branch information
Showing
8 changed files
with
169 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
common/src/main/java/org/dromara/hertzbeat/common/cache/CaffeineCacheServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
common/src/test/java/org/dromara/hertzbeat/common/cache/CaffeineCacheTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 0 additions & 71 deletions
71
manager/src/main/java/org/dromara/hertzbeat/manager/cache/CaffeineCacheServiceImpl.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters