init
This commit is contained in:
31
src/main/java/com/newyetai/common/cache/DefaultCacheProvider.java
vendored
Normal file
31
src/main/java/com/newyetai/common/cache/DefaultCacheProvider.java
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
package com.newyetai.common.cache;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.collections.MapUtils;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
/**
|
||||
* @author sr on 2020-07-07
|
||||
*/
|
||||
public class DefaultCacheProvider implements ICacheProvider {
|
||||
|
||||
private Map<String, Long> numLimitMap = Maps.newConcurrentMap();
|
||||
|
||||
@Override
|
||||
public boolean outLimit(final String key, Long maxNumLimit) {
|
||||
Long currentNumLimit = null;
|
||||
final Object lockKey = key.intern();
|
||||
synchronized (lockKey) {
|
||||
currentNumLimit = MapUtils.getLong(numLimitMap, key, 0L);
|
||||
if (currentNumLimit >= maxNumLimit) {
|
||||
return true;
|
||||
}
|
||||
currentNumLimit++;
|
||||
numLimitMap.put(key, currentNumLimit);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user