forked from alibaba/nacos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor the cache to replace guava (alibaba#6520)
* Refactor the cache to replace guava * Refactor CacheBuilder * add unit test * Format code
- Loading branch information
Showing
12 changed files
with
729 additions
and
22 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
68 changes: 68 additions & 0 deletions
68
common/src/main/java/com/alibaba/nacos/common/cache/Cache.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,68 @@ | ||
/* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed 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 com.alibaba.nacos.common.cache; | ||
|
||
import java.util.concurrent.Callable; | ||
|
||
/** | ||
* Cache method collection definition. | ||
* @author zzq | ||
* @date 2021/7/30 | ||
*/ | ||
public interface Cache<K, V> { | ||
|
||
/** | ||
* Cache a pair of key value. If the key value already exists, the value will be overwritten. | ||
* @param key cache key | ||
* @param val cache value | ||
*/ | ||
void put(K key, V val); | ||
|
||
/** | ||
* Take the corresponding value from the cache according to the cache key. | ||
* @param key cache key | ||
* @return cache value | ||
*/ | ||
V get(K key); | ||
|
||
/** | ||
* Get the value in the cache according to the primary key, and put it into the cache after processing by the function. | ||
* @param key cache key | ||
* @param call a function, the return value of the function will be updated to the cache | ||
* @return cache value | ||
* @throws Exception callable function interface throw exception | ||
*/ | ||
V get(K key, Callable<? extends V> call) throws Exception; | ||
|
||
/** | ||
* Take the corresponding value from the cache according to the cache key, and remove this record from the cache. | ||
* @param key cache key | ||
* @return cache value | ||
*/ | ||
V remove(K key); | ||
|
||
/** | ||
* Clear the entire cache. | ||
*/ | ||
void clear(); | ||
|
||
/** | ||
* Returns the number of key-value pairs in the cache. | ||
* @return number of key-value pairs | ||
*/ | ||
int getSize(); | ||
} |
131 changes: 131 additions & 0 deletions
131
common/src/main/java/com/alibaba/nacos/common/cache/builder/CacheBuilder.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,131 @@ | ||
/* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed 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 com.alibaba.nacos.common.cache.builder; | ||
|
||
import com.alibaba.nacos.common.cache.Cache; | ||
import com.alibaba.nacos.common.cache.decorators.AutoExpireCache; | ||
import com.alibaba.nacos.common.cache.decorators.LruCache; | ||
import com.alibaba.nacos.common.cache.decorators.SynchronizedCache; | ||
import com.alibaba.nacos.common.cache.impl.SimpleCache; | ||
|
||
import java.util.Objects; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Cache builder. | ||
* @author zzq | ||
* @date 2021/7/30 | ||
*/ | ||
public class CacheBuilder<K, V> { | ||
|
||
private static final int DEFAULT_MAXIMUMSIZE = 1024; | ||
|
||
private static final int DEFAULT_INITIALIZE_CAPACITY = 1024; | ||
|
||
private static final int DEFAULT_EXPIRE_NANOS = -1; | ||
|
||
private long expireNanos = DEFAULT_EXPIRE_NANOS; | ||
|
||
private int maximumSize = DEFAULT_MAXIMUMSIZE; | ||
|
||
private int initializeCapacity = DEFAULT_INITIALIZE_CAPACITY; | ||
|
||
private boolean sync = false; | ||
|
||
private boolean lru = false; | ||
|
||
public static CacheBuilder builder() { | ||
return new CacheBuilder(); | ||
} | ||
|
||
/** | ||
* Set expiration time. | ||
*/ | ||
public CacheBuilder expireNanos(long duration, TimeUnit unit) { | ||
checkExpireNanos(duration, unit); | ||
this.expireNanos = unit.toNanos(duration); | ||
return this; | ||
} | ||
|
||
private void checkExpireNanos(long duration, TimeUnit unit) { | ||
if (duration < 0) { | ||
throw new IllegalArgumentException("duration cannot be negative"); | ||
} | ||
if (Objects.isNull(unit)) { | ||
throw new IllegalArgumentException("unit cannot be null"); | ||
} | ||
} | ||
|
||
/** | ||
* Set the maximum capacity of the cache pair. | ||
* @param maximumSize maximum capacity | ||
*/ | ||
public CacheBuilder<K, V> maximumSize(int maximumSize) { | ||
if (maximumSize < 0) { | ||
throw new IllegalArgumentException("size cannot be negative"); | ||
} | ||
this.maximumSize = maximumSize; | ||
return this; | ||
} | ||
|
||
/** | ||
* Set whether the cache method is synchronized. | ||
* @param sync if sync value is true, each method of the constructed cache is synchronized. | ||
*/ | ||
public CacheBuilder<K, V> sync(boolean sync) { | ||
this.sync = sync; | ||
return this; | ||
} | ||
|
||
/** | ||
* Does the constructed cache support lru. | ||
* @param lru If the cache built for true is an lru cache. | ||
*/ | ||
public CacheBuilder<K, V> lru(boolean lru) { | ||
this.lru = lru; | ||
return this; | ||
} | ||
|
||
/** | ||
* Set the initialize capacity of the cache pair. | ||
* @param initializeCapacity initialize capacity | ||
*/ | ||
public CacheBuilder<K, V> initializeCapacity(int initializeCapacity) { | ||
if (initializeCapacity < 0) { | ||
throw new IllegalArgumentException("initializeCapacity cannot be negative"); | ||
} | ||
this.initializeCapacity = initializeCapacity; | ||
return this; | ||
} | ||
|
||
/** | ||
* Build the cache according to the builder attribute. | ||
*/ | ||
public Cache<K, V> build() { | ||
Cache<K, V> cache = new SimpleCache(initializeCapacity); | ||
if (lru) { | ||
cache = new LruCache<>(cache, maximumSize); | ||
} | ||
if (expireNanos != -1) { | ||
cache = new AutoExpireCache(cache, expireNanos); | ||
} | ||
if (sync) { | ||
cache = new SynchronizedCache<>(cache); | ||
} | ||
return cache; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
common/src/main/java/com/alibaba/nacos/common/cache/builder/CacheItemProperties.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,35 @@ | ||
/* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed 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 com.alibaba.nacos.common.cache.builder; | ||
|
||
/** | ||
* Cache item's own attributes. | ||
* @author zzq | ||
* @date 2021/7/30 | ||
*/ | ||
public class CacheItemProperties { | ||
|
||
private long expireNanos; | ||
|
||
public long getExpireNanos() { | ||
return expireNanos; | ||
} | ||
|
||
public void setExpireNanos(long expireNanos) { | ||
this.expireNanos = expireNanos; | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
common/src/main/java/com/alibaba/nacos/common/cache/decorators/AutoExpireCache.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,90 @@ | ||
/* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed 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 com.alibaba.nacos.common.cache.decorators; | ||
|
||
import com.alibaba.nacos.common.cache.Cache; | ||
import com.alibaba.nacos.common.cache.builder.CacheItemProperties; | ||
|
||
import java.util.HashMap; | ||
import java.util.concurrent.Callable; | ||
|
||
/** | ||
* A wrapper that automatically expires the cache. | ||
* @author zzq | ||
* @date 2021/7/30 | ||
*/ | ||
public class AutoExpireCache<K, V> implements Cache<K, V> { | ||
|
||
private long expireNanos; | ||
|
||
private Cache<K, V> delegate; | ||
|
||
private HashMap<K, CacheItemProperties> keyProp = new HashMap<>(); | ||
|
||
public AutoExpireCache(Cache<K, V> delegate, long expireNanos) { | ||
this.expireNanos = expireNanos; | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public void put(K key, V val) { | ||
keyProp.put(key, cacheItemProperties()); | ||
this.delegate.put(key, val); | ||
} | ||
|
||
@Override | ||
public V get(K key) { | ||
if (keyProp.get(key) != null && isExpire(keyProp.get(key))) { | ||
this.keyProp.remove(key); | ||
this.delegate.remove(key); | ||
return null; | ||
} | ||
return this.delegate.get(key); | ||
} | ||
|
||
@Override | ||
public V get(K key, Callable<? extends V> call) throws Exception { | ||
return this.delegate.get(key, call); | ||
} | ||
|
||
@Override | ||
public V remove(K key) { | ||
keyProp.remove(key); | ||
return this.delegate.remove(key); | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
keyProp.clear(); | ||
this.delegate.clear(); | ||
} | ||
|
||
@Override | ||
public int getSize() { | ||
return this.delegate.getSize(); | ||
} | ||
|
||
private boolean isExpire(CacheItemProperties itemProperties) { | ||
return expireNanos != -1 && (System.nanoTime() - itemProperties.getExpireNanos() > expireNanos); | ||
} | ||
|
||
private CacheItemProperties cacheItemProperties() { | ||
CacheItemProperties cacheItemProperties = new CacheItemProperties(); | ||
cacheItemProperties.setExpireNanos(System.nanoTime()); | ||
return cacheItemProperties; | ||
} | ||
} |
Oops, something went wrong.