一、导入依赖
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.6.2</version>
</dependency>
二、缓存类
public class CustomCache<K, V> {
private Cache<K, V> cache;
public CustomCache() {
this.cache = Caffeine.newBuilder()
.expireAfterWrite(120, TimeUnit.SECONDS)
.build();
}
public Boolean setIfAbsent(K key, V value) {
return cache.asMap().putIfAbsent(key, value) == null;
}
}
三、调用
public boolean isMember(String duplicate_data_set_key, String data) {标签:set,key,redis,setIfAbsent,duplicate,isMember,data,模拟 From: https://www.cnblogs.com/liaoenqi/p/18206263
Boolean isMember = true;
if (customCache.setIfAbsent(SIXTY_SEC_DUPLICATE_DATA_PREFIX_KEY + duplicate_data_set_key,
duplicate_data_set_key)) {
isMember = false;
log.info("二分钟有效期警情数据:duplicate_data_set_key:{} data:{}", duplicate_data_set_key, data);
}
log.info(" 线程:{} redis是否存在 {} ", Thread.currentThread().getName(), isMember);
// 判断数据是否存在于Set中
return isMember != null && isMember;
}