| | |
| | | |
| | | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| | | import com.matrix.core.tools.LogUtil; |
| | | import com.matrix.core.tools.StringUtils; |
| | | import lombok.Data; |
| | | |
| | | import java.util.Iterator; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.util.Set; |
| | | import java.util.concurrent.*; |
| | | import java.util.concurrent.atomic.AtomicLong; |
| | | import java.util.stream.Collectors; |
| | | |
| | | public class LocalCache { |
| | | |
| | |
| | | |
| | | private static ConcurrentMap<String, Value> localCache = new ConcurrentHashMap(60); |
| | | |
| | | /** |
| | | * 根据key匹配多个缓存值 |
| | | * |
| | | * @param key |
| | | * @param <T> |
| | | * @return |
| | | */ |
| | | public static <T> Map<String, T> getValues(String key) { |
| | | return localCache.entrySet().stream() |
| | | .filter(item -> StringUtils.isMatch(key, item.getKey())) |
| | | .map(Map.Entry::getValue) |
| | | .filter(item -> Objects.nonNull(item.value)) |
| | | .collect(Collectors.toMap(Value::getKey, item -> (T) item.value)); |
| | | } |
| | | |
| | | /** |
| | | * 获取本地缓存 |
| | |
| | | * @return |
| | | */ |
| | | public static <T> T get(String key) { |
| | | return (T) localCache.get(key); |
| | | Value value = localCache.get(key); |
| | | if (Objects.nonNull(value)) { |
| | | return (T) value.value; |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 删除缓存 |
| | | * |
| | | * @param key |
| | | * @param <T> |
| | | * @return |
| | | */ |
| | | public static <T> T remove(String key) { |
| | | Value value = localCache.remove(key); |
| | | if (Objects.nonNull(value)) { |
| | | return (T) value.value; |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 批量删除缓存 |
| | | * |
| | | * @param key |
| | | * @return |
| | | */ |
| | | public static int batchRemove(String key) { |
| | | int count = 0; |
| | | Set<Map.Entry<String, Value>> entries = localCache.entrySet(); |
| | | Iterator<Map.Entry<String, Value>> iterator = entries.iterator(); |
| | | while (iterator.hasNext()) { |
| | | Map.Entry<String, Value> next = iterator.next(); |
| | | if (StringUtils.isMatch(key, next.getKey())) { |
| | | remove(next.getKey()); |
| | | count++; |
| | | } |
| | | } |
| | | return count; |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param value |
| | | */ |
| | | public static void save(String key, Object value) { |
| | | if (null != localCache.put(key, buildValue(value))) { |
| | | if (null != localCache.put(key, buildValue(key, value))) { |
| | | LogUtil.debug("覆盖原有缓存{}", key); |
| | | } |
| | | } |
| | |
| | | * @param timeOut 毫秒 |
| | | */ |
| | | public static void save(String key, Object value, long timeOut) { |
| | | if (null != localCache.put(key, buildValue(value, timeOut))) { |
| | | if (null != localCache.put(key, buildValue(key, value, timeOut))) { |
| | | LogUtil.debug("覆盖原有缓存{}", key); |
| | | } |
| | | startClearThread(); |
| | | } |
| | | |
| | | /** |
| | | * 重置缓存失效时间 |
| | | * @param key |
| | | */ |
| | | public static void resetExpire(String key) { |
| | | Objects.requireNonNull(key); |
| | | Value value = localCache.get(key); |
| | | if(Objects.nonNull(value)){ |
| | | value.getCreateTime().set(System.currentTimeMillis()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | continue; |
| | | } |
| | | |
| | | boolean isTimeOut = (System.currentTimeMillis() - next.getValue().createTime) > next.getValue().timeOut; |
| | | boolean isTimeOut = (System.currentTimeMillis() - next.getValue().getCreateTime().longValue()) > next.getValue().timeOut; |
| | | if (isTimeOut) { |
| | | Value removed = localCache.remove(next.getKey()); |
| | | LogUtil.debug("清除过期对象:{}", removed.value); |
| | |
| | | } |
| | | |
| | | |
| | | private static Value buildValue(Object value) { |
| | | return buildValue(value, 0); |
| | | private static Value buildValue(String key, Object value) { |
| | | return buildValue(key, value, 0); |
| | | } |
| | | |
| | | |
| | | private static Value buildValue(Object value, long timeOut) { |
| | | private static Value buildValue(String key, Object value, long timeOut) { |
| | | Value instances = new Value(); |
| | | instances.createTime = System.currentTimeMillis(); |
| | | instances.createTime = new AtomicLong(System.currentTimeMillis()); |
| | | instances.key = key; |
| | | instances.value = value; |
| | | instances.timeOut = timeOut; |
| | | return instances; |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 缓存对象 |
| | | */ |
| | | @Data |
| | | static class Value { |
| | | |
| | | /** |
| | | * 过期时间,0 表示不过期,单位毫秒 |
| | | */ |
| | | private long timeOut = 0; |
| | | /** |
| | | * 缓存key |
| | | */ |
| | | private String key; |
| | | /** |
| | | * 缓存值 |
| | | */ |
| | |
| | | /** |
| | | * 缓存创建时间 |
| | | */ |
| | | private long createTime; |
| | | private AtomicLong createTime; |
| | | |
| | | |
| | | } |