This is an automated email from the ASF dual-hosted git repository.

gongchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git


The following commit(s) were added to refs/heads/master by this push:
     new 6f9ee2f4e [improve] Optimize CacheService and add relevant unit test 
(#2912)
6f9ee2f4e is described below

commit 6f9ee2f4e660e5cc6d4613c2bbae86493672ad7a
Author: nullwli <[email protected]>
AuthorDate: Sun Dec 29 23:52:36 2024 +0800

    [improve] Optimize CacheService and add relevant unit test (#2912)
    
    Co-authored-by: LLWW04 <[email protected]>
    Co-authored-by: tomsun28 <[email protected]>
---
 .../apache/hertzbeat/common/cache/CaffeineCacheServiceImpl.java  | 7 ++++++-
 .../org/apache/hertzbeat/common/cache/CommonCacheService.java    | 9 ++++++++-
 .../org/apache/hertzbeat/common/cache/CaffeineCacheTest.java     | 6 ++++++
 3 files changed, 20 insertions(+), 2 deletions(-)

diff --git 
a/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/cache/CaffeineCacheServiceImpl.java
 
b/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/cache/CaffeineCacheServiceImpl.java
index b87b47272..716335062 100644
--- 
a/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/cache/CaffeineCacheServiceImpl.java
+++ 
b/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/cache/CaffeineCacheServiceImpl.java
@@ -50,7 +50,12 @@ public class CaffeineCacheServiceImpl<K, V> implements 
CommonCacheService<K, V>
     }
 
     @Override
-    public V put(K key, V value) {
+    public void put(K key, V value) {
+        cache.put(key, value);
+    }
+
+    @Override
+    public V putAndGetOld(K key, V value) {
         V oldValue = cache.getIfPresent(key);
         cache.put(key, value);
         return oldValue;
diff --git 
a/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/cache/CommonCacheService.java
 
b/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/cache/CommonCacheService.java
index 15be0cc0d..0cf0d407d 100644
--- 
a/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/cache/CommonCacheService.java
+++ 
b/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/cache/CommonCacheService.java
@@ -33,9 +33,16 @@ public interface CommonCacheService<K, V> {
      * set cache
      * @param key key
      * @param value value
+     */
+    void put(K key, V value);
+
+    /**
+     * set cache and return oldValue if present
+     * @param key key
+     * @param value value
      * @return old value
      */
-    V put(K key, V value);
+    V putAndGetOld(K key, V value);
 
     /**
      * if contain cache by key
diff --git 
a/hertzbeat-common/src/test/java/org/apache/hertzbeat/common/cache/CaffeineCacheTest.java
 
b/hertzbeat-common/src/test/java/org/apache/hertzbeat/common/cache/CaffeineCacheTest.java
index 5546329e2..b60570da6 100644
--- 
a/hertzbeat-common/src/test/java/org/apache/hertzbeat/common/cache/CaffeineCacheTest.java
+++ 
b/hertzbeat-common/src/test/java/org/apache/hertzbeat/common/cache/CaffeineCacheTest.java
@@ -63,6 +63,12 @@ class CaffeineCacheTest {
         for (int i = 0; i < 10; i++) {
             Assertions.assertNull(cacheService.get(key + i));
         }
+        // test new method : cacheService.putAndGetOld(key,newValue)
+        String oldValue = "oldOne";
+        String newValue = "newOne";
+        cacheService.put(key, oldValue);
+        Assertions.assertEquals(oldValue, cacheService.putAndGetOld(key, 
newValue));
+        Assertions.assertEquals(newValue, cacheService.get(key));
     }
 
     @Test


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to