ConcurrentHashMap实现缓存功能

server/2024/10/21 5:00:11/
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()) {// 如果条目不存在或已过期,则移除并返回 nullcache.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();}
}
@Component
public class DingDingJobAlarm implements JobAlarm {
// 创建一个缓存工具,设置 TTL 为 一天private final CacheUtil<String, List<XxlJobGroup>> cache = new CacheUtil<>(60 * 1000 * 60 * 24);@Overridepublic 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 就是我的目标数据

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

http://www.ppmy.cn/server/133535.html

相关文章

028 elasticsearch索引管理-ElasticsearchRestTemplate

文章目录 pom.xmlapplication.ymlCubemallSearchApplication.javaRestClientTest.java使用ElasticsearchRestTemplate对象Blog.javaRestTemplateTest.java pom.xml <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-s…

OpenCV高级图形用户界面(14)交互式地选择一个或多个感兴趣区域函数selectROIs()的使用

操作系统&#xff1a;ubuntu22.04 OpenCV版本&#xff1a;OpenCV4.9 IDE:Visual Studio Code 编程语言&#xff1a;C11 算法描述 允许用户在给定的图像上选择多个 ROI。 该函数创建一个窗口&#xff0c;并允许用户使用鼠标来选择多个 ROI。控制方式&#xff1a;使用空格键或…

DFS算法经典题目: Leetcode 51.N皇后

DFS算法经典题目&#xff1a; Leetcode 51.N皇后 题目详情如下 这道题如果使用暴力解法的话&#xff0c;需要对N个皇后放在每个地方都进行枚举并判断是否可行&#xff0c;时间复杂度非常之高&#xff0c;肯定是过不了的&#xff0c;所以需要使用其他解法。 根据题目可以知道每…

text2sql: multi-agent实现思路MAC-SQL

MAC-SQL出自2023年12月的论文《MAC-SQL: A Multi-Agent Collaborative Framework for Text-to-SQL》(github)&#xff0c;它是用基于LLM的multi-agent来实现text2sql。 MAC-SQL的整体思路如论文图2所示&#xff0c;由Decomposer、Selector、Refiner三个agent组成&#xff0c;个…

目标检测系统中需要【重新训练模型】说明

上百种【基于YOLOv8/v10/v11的目标检测系统】目录&#xff08;pythonpyside6界面系统源码可训练的数据集也完成的训练模型&#xff09;-CSDN博客 目标检测系统操作说明【用户使用指南】&#xff08;pythonpyside6界面系统源码可训练的数据集也完成的训练模型&#xff09;-CSDN…

算法专题七: 分治归并

目录 1. 排序数组2. 交易逆序对的总数3. 计算右侧小于当前元素的个数4. 翻转对 1. 排序数组 算法思路: 本道题使用归并的思路进行排序, 先讲数组分为左右两个区间, 然后合并两个有序数组. class Solution {vector<int> tmp; public:vector<int> sortArray(vector&…

wifi、热点密码破解 - python

乐子脚本&#xff0c;有点小慢&#xff0c;试过多线程&#xff0c;系统 wifi 连接太慢了&#xff0c;需要时间确认&#xff0c;多线程的话系统根本反应不过来。 也就可以试试破解别人的热点&#xff0c;一般都是 123456 这样的傻鸟口令 # coding:utf-8 import pywifi from pyw…

python 打包为动态链接库(.so文件)

参考资料&#xff1a;https://medium.com/yeap0022/linux-compress-python-packages-into-shared-library-so-8342bffab001 注意事项&#xff1a;.py, .c后缀文件都可以删掉&#xff0c;cache也可以删掉&#xff0c;但是__init__.py文件不能删&#xff0c;建立的子文件夹也不能…