2025-12-29 16:06:48 +08:00
|
|
|
package com.senmesh.common.cache;
|
2025-06-11 09:42:13 +08:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|