ConcurrentHashMap实现缓存功能

java 复制代码
public class CacheUtil<K, V>{
    private final ConcurrentHashMap<K, CacheEntry<V>> cache = new ConcurrentHashMap<>();
    private final long ttl; // 缓存过期时间,单位:毫秒

    // 缓存条目类
    private static class CacheEntry<V> {
        V value;
        long expirationTime;

        CacheEntry(V value, long ttl) {
            this.value = value;
            this.expirationTime = System.currentTimeMillis() + ttl;
        }

        boolean isExpired() {
            return System.currentTimeMillis() > expirationTime;
        }
    }

    public CacheUtil(long ttl) {
        this.ttl = ttl; // 设置缓存的过期时间
    }

    // 添加缓存
    public void put(K key, V value) {
        cache.put(key, new CacheEntry<>(value, ttl));
    }

    // 获取缓存
    public V get(K key) {
        CacheEntry<V> entry = cache.get(key);
        if (entry == null || entry.isExpired()) {
            // 如果条目不存在或已过期,则移除并返回 null
            cache.remove(key);
            return null;
        }
        return entry.value;
    }

    // 清除缓存
    public void remove(K key) {
        cache.remove(key);
    }

    // 清空整个缓存
    public void clear() {
        cache.clear();
    }

    // 检查缓存是否存在
    public boolean containsKey(K key) {
        return cache.containsKey(key) && !cache.get(key).isExpired();
    }
}
java 复制代码
@Component
public class DingDingJobAlarm implements JobAlarm {
// 创建一个缓存工具,设置 TTL 为 一天
    private final CacheUtil<String, List<XxlJobGroup>> cache = new CacheUtil<>(60 * 1000 * 60 * 24);

@Override
    public boolean doAlarm(XxlJobInfo info, XxlJobLog jobLog) {
String redisKey = "spy:xxljob:group:info";

try {

List<XxlJobGroup> xxlJobGroups = cache.get(redisKey);
            if (CollectionUtils.isEmpty(xxlJobGroups) || !cache.containsKey(redisKey)) {
                xxlJobGroups = xxlJobGroupDao.findAll();
                cache.put(redisKey, xxlJobGroups);
            }


}catch (Exception e) {
            log.error("xxlJob系统发送钉钉消息异常=", e);
        }
}

}

xxlJobGroups 就是我的目标数据

java 复制代码
package com.xxl.job.admin.core.model;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by xuxueli on 16/9/30.
 */
public class XxlJobGroup {

    private int id;
    private String appname;
    private String title;
    private int addressType;        // 执行器地址类型:0=自动注册、1=手动录入
    private String addressList;     // 执行器地址列表,多地址逗号分隔(手动录入)
    private Date updateTime;

    // registry list
    private List<String> registryList;  // 执行器地址列表(系统注册)
    public List<String> getRegistryList() {
        if (addressList!=null && addressList.trim().length()>0) {
            registryList = new ArrayList<String>(Arrays.asList(addressList.split(",")));
        }
        return registryList;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getAppname() {
        return appname;
    }

    public void setAppname(String appname) {
        this.appname = appname;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getAddressType() {
        return addressType;
    }

    public void setAddressType(int addressType) {
        this.addressType = addressType;
    }

    public String getAddressList() {
        return addressList;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public void setAddressList(String addressList) {
        this.addressList = addressList;
    }

}
相关推荐
DogDaoDao2 小时前
【GitHub】System Informer:Windows 平台最强开源系统监控与调试利器
windows·程序员·开源·github·开发工具·system informer
HashFlag2 小时前
Codex配置Skill
windows·ai·mac·codex
idolao2 小时前
RayLink_v8.1.6.8安装步骤详解(附RayLink远程控制与设备码连接教程)
windows
lingx_gps4 小时前
Windows 下安装领新北斗(TracSeek)车辆动态监控系统小白指南
windows·jt808·车辆监控·主动安全·jt1078·车辆定位
2601_958320575 小时前
【详细版教程】Windows/macOS/Linux 安装 OpenClaw 2.6.6 指南(包含安装包)
linux·运维·windows·macos·小龙虾·open claw一键安装
HackTwoHub5 小时前
可视化未授权访问批量探测工具、支持批量目标、并发扫描、SOCKS5 全局代理、CSV 导出
linux·windows·macos·网络安全·网络攻击模型
50万马克的面包6 小时前
C 语言第18讲:预处理详解
c语言·开发语言·windows
努力努力再努力wz7 小时前
【Qt 入门系列】从应用场景到开发环境:建立对 Qt 的第一层认知
c语言·开发语言·数据库·c++·b树·qt·缓存
无限进步_7 小时前
简单聊聊 C++ 中的 unordered_map 和 unordered_set
c语言·开发语言·数据结构·c++·windows·哈希算法·散列表
河阿里7 小时前
深入理解LRU缓存机制:从原理到应用(C++实现
开发语言·c++·缓存