package com.tievd.jyz.cache;
|
|
import cn.hutool.core.collection.ConcurrentHashSet;
|
import cn.hutool.core.collection.ListUtil;
|
import com.google.common.cache.Cache;
|
import com.google.common.cache.CacheBuilder;
|
|
import java.util.List;
|
import java.util.Set;
|
import java.util.concurrent.TimeUnit;
|
|
/**
|
* 时间和加油记录表对应关系临时缓存
|
* @author timi
|
*/
|
public class EventCodeRelOilCache {
|
|
/** 事件临时缓存,用于图片和视频和源事件的对应
|
* eventCode:oilRecordId */
|
private final static Cache<String, Set<Long>> EVENT_CODE_REL_OIL_RECORD_CACHE_MAP = CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES).build();
|
|
/**
|
* 压入
|
* @param eventCode
|
* @param oilRecordId
|
*/
|
public static void put(String eventCode,Long oilRecordId){
|
Set<Long> oilRecordIdSet = EVENT_CODE_REL_OIL_RECORD_CACHE_MAP.getIfPresent(eventCode);
|
if(oilRecordIdSet == null){
|
oilRecordIdSet = new ConcurrentHashSet<>();
|
EVENT_CODE_REL_OIL_RECORD_CACHE_MAP.put(eventCode,oilRecordIdSet);
|
}
|
oilRecordIdSet.add(oilRecordId);
|
}
|
|
/**
|
* 获取
|
* @param eventCode
|
* @return
|
*/
|
public static List<Long> get(String eventCode){
|
return ListUtil.toList(EVENT_CODE_REL_OIL_RECORD_CACHE_MAP.getIfPresent(eventCode));
|
}
|
|
/**
|
* 数量
|
* @param eventCode
|
* @return
|
*/
|
public static int count(String eventCode){
|
Set<Long> set = EVENT_CODE_REL_OIL_RECORD_CACHE_MAP.getIfPresent(eventCode);
|
if(set != null){
|
return set.size();
|
}
|
return 0;
|
}
|
|
/**
|
* 移除
|
* @param eventCode
|
* @return
|
*/
|
public static List<Long> remove(String eventCode){
|
List<Long> oilRecordIdList = ListUtil.toList(EVENT_CODE_REL_OIL_RECORD_CACHE_MAP.getIfPresent(eventCode));
|
EVENT_CODE_REL_OIL_RECORD_CACHE_MAP.invalidate(eventCode);
|
return oilRecordIdList;
|
}
|
|
}
|