org.dromara.jpom.common.commander.CacheObject Maven / Gradle / Ivy
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 Code Technology Studio
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.dromara.jpom.common.commander;
import cn.hutool.core.date.SystemClock;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* @author bwcx_jzy
* @since 2023/4/6
*/
public class CacheObject {
private final T value;
private final Long enterTime;
public CacheObject(T value) {
this.value = value;
this.enterTime = SystemClock.now();
}
private boolean isExpired() {
return (System.currentTimeMillis() - this.enterTime > TimeUnit.MINUTES.toMillis(10));
}
/**
* 添加到缓存对象中
*
* @param map map
* @param key 缓存的 key
* @param value 缓存的 value
* @param 缓存的 key
* @param 缓存的 value
*/
public static void put(Map> map, K key, V value) {
map.put(key, new CacheObject<>(value));
int size = map.size();
if (size > 100) {
// 清空过期的数据
Iterator>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry> next = iterator.next();
CacheObject nextValue = next.getValue();
if (nextValue.isExpired()) {
iterator.remove();
}
}
}
}
/**
* 获取缓存中的值
*
* @param map 缓存 map
* @param key 缓存的 key
* @param 缓存的 key
* @return value
*/
public static V get(Map> map, K key) {
CacheObject cacheObject = map.get(key);
if (cacheObject == null || cacheObject.isExpired()) {
map.remove(key);
return null;
}
return cacheObject.value;
}
}