这篇文章主要为大家展示了“Java中TimedCache带时间缓存工具类怎么用”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Java中TimedCache带时间缓存工具类怎么用”这篇文章吧。
我们在工作中会碰到需要使用带过期时间的缓存场景。但是使用redis有太重了,毕竟缓存的数据很小,放在内存够够的。hutools提供了TimedCache时间缓存工具,可以实现该场景。下面使用到该组件,并为了适配工作场景,对该工具类做优化升级。
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.4.6</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1.1-jre</version>
</dependency>
不多说了,上代码。
import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.thread.ThreadUtil;
/** @Author huyi @Date 2021/10/12 17:00 @Description: */
public class TimedCacheUtils {
private static final TimedCache<String, String> TIMED_CACHE = CacheUtil.newTimedCache(5000);
static {
/** 每5ms检查一次过期 */
TIMED_CACHE.schedulePrune(5);
}
/**
* 存入键值对,提供消逝时间
*
* @param key
* @param value
* @param timeout
*/
public static void put(String key, String value, Long timeout) {
/** 设置消逝时间 */
TIMED_CACHE.put(key, value, timeout);
}
/**
* 每次重新get一次缓存,均会重新刷新消逝时间
* @param key
* @return
*/
public static String get(String key) {
return TIMED_CACHE.get(key);
}
public static void main(String[] args) {
put("haha", "1", 3000L);
ThreadUtil.sleep(2000);
// if (TIMED_CACHE.containsKey("haha")) {
// System.out.println("aa");
// }
System.out.println("第1次结果:" + get("haha"));
ThreadUtil.sleep(2000);
System.out.println("第2次结果:" + get("haha"));
ThreadUtil.sleep(5000);
System.out.println("第3次结果:" + get("haha"));
// 取消定时清理
TIMED_CACHE.cancelPruneSchedule();
}
}
首先我们看一下执行的效果
说明:
1、设置的超时时间为3000毫秒,所以第一次打印在2秒钟,所以可以获取到值。
2、因为第一次打印调用了get方法,刷新了过期时间,所以依然可以获取到值。
3、第三次打印在5秒后,所以已经过期,无法获取到值,打印null。
那么,需要知道是否缓存还在可以使用containsKey方法。如下:
put("haha", "1", 3000L);
ThreadUtil.sleep(2000);
if (TIMED_CACHE.containsKey("haha")) {
System.out.println("第1次结果:缓存存在");
}
// System.out.println("第1次结果:" + get("haha"));
ThreadUtil.sleep(2000);
System.out.println("第2次结果:" + get("haha"));
ThreadUtil.sleep(5000);
System.out.println("第3次结果:" + get("haha"));
// 取消定时清理
TIMED_CACHE.cancelPruneSchedule();
执行结果如下:
我们在使用TimedCache会发现,一旦缓存过期我们并不能立马知道,很多工作场景中需要对缓存做监听回调。所以我升级了一下该工具类。
import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.thread.ThreadUtil;
import com.google.common.util.concurrent.*;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.text.MessageFormat;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
/** @Author huyi @Date 2021/10/12 10:57 @Description: 时间缓存工具 */
public class TimedCacheUtils {
private static final TimedCache<String, String> TIMED_CACHE = CacheUtil.newTimedCache(5000);
/** 线程池 */
private static final ExecutorService executorService = Executors.newCachedThreadPool();
private static final ListeningExecutorService listeningExecutorService =
MoreExecutors.listeningDecorator(executorService);
/** 回调方法映射 */
private static ConcurrentHashMap<String, Consumer<String>> callbackMap;
/**
* 存入键值对,添加过期时间,和消费回调
*
* @param key
* @param timeout
* @param consumer
*/
public static void put(String key, String value, Long timeout, Consumer<String> consumer) {
TIMED_CACHE.put(key, value, timeout);
addListen(key, consumer);
}
/**
* 获取缓存值
*
* @param key
* @return
*/
public static String get(String key) {
return TIMED_CACHE.get(key);
}
/**
* 删除缓存和回调映射
*
* @param key
*/
public static void remove(String key) {
callbackMap.remove(key);
TIMED_CACHE.remove(key);
}
/**
* 添加监听器
*
* @param key
* @param consumer
*/
public static void addListen(String key, Consumer<String> consumer) {
ListenableFuture<String> listenableFuture =
listeningExecutorService.submit(
() -> {
while (TIMED_CACHE.containsKey(key)) {
ThreadUtil.sleep(500);
}
return key;
});
Futures.addCallback(
listenableFuture,
new FutureCallback<String>() {
@Override
public void onSuccess(@Nullable String s) {
consumer.accept(s);
}
@Override
public void onFailure(Throwable throwable) {
throwable.printStackTrace();
}
},
listeningExecutorService);
}
public static void main(String[] args) {
put("haha", "1", 3000L, x -> System.out.println(MessageFormat.format("[{0}] - 缓存消逝", x)));
ThreadUtil.sleep(2000);
System.out.println(get("haha"));
ThreadUtil.sleep(2000);
System.out.println(get("haha"));
ThreadUtil.sleep(5000);
System.out.println(get("haha"));
// 关闭监听线程池
listeningExecutorService.shutdown();
}
}
以上是“Java中TimedCache带时间缓存工具类怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。