405 Star 3.7K Fork 1.2K

GVP京东零售 / hotkey

 / 详情

worker验证间隔时间超过一分钟的热键的与否的计算有误

待办的
任务
创建于  
2022-05-12 17:35
/**
     * @return void
     * @description worker验证间隔时间超过一分钟的热键的计算的准确性
     * @params [args]
     * @author wmh
     * @date 2022/5/12 16:38
     */
    public static void main(String[] args) {

        /**
         * 因CaffeineBuilder.buildAllKeyCache中设置的滑动窗口的缓存过期时间为一分钟(expireAfterWrite),
         * 滑动窗口可设置的最大间隔时间为10分钟(maxDuration),
         * 会造成超过1分钟间隔时间的热键的滑动窗口缓存丢失,从而造成统计有误
         * 是否(expireAfterWrite,maxDuration)两者保持一致会比较好
         */
        String key = "testKey";

        final Cache<String, Object> cache = CaffeineCacheHolder.getCache(null);
        SlidingWindow window = (SlidingWindow) cache.get(key, (Function<String, SlidingWindow>) k -> {
            System.out.println("new cache");
            KeyRule keyRule = new KeyRule(k, false, 65, 2, 10);
            return new SlidingWindow(keyRule.getInterval(), keyRule.getThreshold());
        });

        System.out.println("start = " + window.addCount(1));

        try {
            Thread.sleep(62000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //放开注释,因为持有滑动窗口对象,所以统计正确
//        System.out.println("result = " + window.addCount(1));

        //重新获取滑动窗口对象,会新创一个对象,丢失之前的数据
        window = (SlidingWindow) cache.get(key, (Function<String, SlidingWindow>) k -> {
            System.out.println("lost cache");
            KeyRule keyRule = new KeyRule(k, false, 65, 2, 10);
            return new SlidingWindow(keyRule.getInterval(), keyRule.getThreshold());
        });

        System.out.println("result = " + window.addCount(1));

        //程序无法退出,是因为CaffeineBuilder中的线程池未停止,建议添加停止入口
        //CaffeineBuilder.executorService.shutdown();
    

输入图片说明

评论 (1)

王敏红 创建了任务

我也有这样的疑惑,不知道这个问题是否有解决

登录 后才可以发表评论

状态
负责人
项目
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
预计工期 (小时)
参与者(2)
8763806 mr wenpan 1626348732
Java
1
https://gitee.com/jd-platform-opensource/hotkey.git
git@gitee.com:jd-platform-opensource/hotkey.git
jd-platform-opensource
hotkey
hotkey

搜索帮助