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;
    }

}
相关推荐
joe02359 小时前
电脑安装 Win10 提示无法在当前分区上安装Windows的解决办法
windows·gpt·电脑·uefi
前端 贾公子9 小时前
vue-cli 模式下安装 uni-ui
前端·javascript·windows
小毛驴85010 小时前
redis 如何持久化
数据库·redis·缓存
Elastic 中国社区官方博客11 小时前
在 Windows 上使用 Docker 运行 Elastic Open Crawler
大数据·windows·爬虫·elasticsearch·搜索引擎·docker·容器
CIAS13 小时前
clonezilla 导出自动化恢复iso
linux·windows·clonezilla
墨菲安全14 小时前
Node.js Windows下路径遍历漏洞
windows·node.js·路径遍历漏洞
NoirSeeker15 小时前
在windows平台上基于OpenHarmony sdk编译三方库并暴露给ArkTS使用(详细)
c++·windows·arkts·鸿蒙·交叉编译
红藕香残玉簟秋15 小时前
【python学习】windows使用conda管理python虚拟环境
windows·python·conda
Kevinyu_16 小时前
Redisson
java·redis·缓存
代码老y17 小时前
Redis 生产实战 7×24:容量规划、性能调优、故障演练与成本治理 40 条军规
数据库·redis·缓存