Skip to content

Commit

Permalink
Refactor the cache to replace guava (alibaba#6520)
Browse files Browse the repository at this point in the history
* Refactor the cache to replace guava

* Refactor CacheBuilder

* add unit test

* Format code
  • Loading branch information
ZZQ001010 authored Aug 3, 2021
1 parent c6c5cd4 commit 449480f
Show file tree
Hide file tree
Showing 12 changed files with 729 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@
package com.alibaba.nacos.client.config.impl;

import com.alibaba.nacos.client.utils.LogUtils;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.alibaba.nacos.common.cache.Cache;
import com.alibaba.nacos.common.cache.builder.CacheBuilder;
import com.google.common.util.concurrent.RateLimiter;
import org.slf4j.Logger;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
Expand All @@ -39,8 +37,15 @@ public class Limiter {

private static final int LIMIT_TIME = 1000;

private static final Cache<String, RateLimiter> CACHE = CacheBuilder.newBuilder().initialCapacity(CAPACITY_SIZE)
.expireAfterAccess(1, TimeUnit.MINUTES).build();
private static final Cache<String, RateLimiter> CACHE;

static {
CACHE = CacheBuilder.builder()
.expireNanos(1, TimeUnit.MINUTES)
.initializeCapacity(CAPACITY_SIZE)
.sync(true)
.build();
}

private static final String LIMIT_TIME_PROPERTY = "limitTime";

Expand Down Expand Up @@ -68,13 +73,8 @@ public class Limiter {
public static boolean isLimit(String accessKeyID) {
RateLimiter rateLimiter = null;
try {
rateLimiter = CACHE.get(accessKeyID, new Callable<RateLimiter>() {
@Override
public RateLimiter call() throws Exception {
return RateLimiter.create(limit);
}
});
} catch (ExecutionException e) {
rateLimiter = CACHE.get(accessKeyID, () -> RateLimiter.create(limit));
} catch (Exception e) {
LOGGER.error("create limit fail", e);
}
if (rateLimiter != null && !rateLimiter.tryAcquire(LIMIT_TIME, TimeUnit.MILLISECONDS)) {
Expand Down
68 changes: 68 additions & 0 deletions common/src/main/java/com/alibaba/nacos/common/cache/Cache.java
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();
}
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;
}
}
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;
}
}
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;
}
}
Loading

0 comments on commit 449480f

Please sign in to comment.