You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
3.4 KiB
102 lines
3.4 KiB
//package com.ynxbd.wx.utils;
|
|
//
|
|
//
|
|
//import org.slf4j.Logger;
|
|
//import org.slf4j.LoggerFactory;
|
|
//import org.slf4j.MDC;
|
|
//import weixin.popular.support.ExpireKey;
|
|
//
|
|
//import java.util.ArrayList;
|
|
//import java.util.List;
|
|
//import java.util.Map;
|
|
//import java.util.concurrent.*;
|
|
//
|
|
//public class DefaultExpireKey implements ExpireKey {
|
|
//
|
|
// private static final Logger log = LoggerFactory.getLogger(DefaultExpireKey.class);
|
|
// private final Map<String, Integer> map = new ConcurrentHashMap<>();
|
|
//
|
|
// private Integer period = 60;
|
|
// private ScheduledExecutorService scheduledExecutorService;
|
|
//
|
|
// public DefaultExpireKey() {
|
|
// cleanExpireKey();
|
|
// }
|
|
//
|
|
// /**
|
|
// * @param period 清理key 间隔时间,默认60秒。
|
|
// */
|
|
// public DefaultExpireKey(int period) {
|
|
// this.period = period;
|
|
// cleanExpireKey();
|
|
// }
|
|
//
|
|
// private void cleanExpireKey() {
|
|
// log.info("创建keyList");
|
|
// if (scheduledExecutorService != null) {
|
|
// scheduledExecutorService.shutdownNow();
|
|
// }
|
|
// // 守护线程 自动清理过期key ,间隔时间60秒
|
|
// scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
|
|
//
|
|
// @Override
|
|
// public Thread newThread(Runnable arg0) {
|
|
// MDC.remove("ip");
|
|
// MDC.put("ip", "Keys守护线程");
|
|
//
|
|
// Thread thread = Executors.defaultThreadFactory().newThread(arg0);
|
|
// thread.setDaemon(true);
|
|
// return thread;
|
|
// }
|
|
// });
|
|
//
|
|
// scheduledExecutorService.scheduleWithFixedDelay(new Runnable() {
|
|
// @Override
|
|
// public void run() {
|
|
// MDC.remove("ip");
|
|
// MDC.put("ip", "定时任务");
|
|
// List<String> removeKey = new ArrayList<>();
|
|
// for (String key : map.keySet()) {
|
|
// Integer value = map.get(key);
|
|
// long current = System.currentTimeMillis() / 1000;
|
|
// if (value != null && value <= current) {
|
|
//// logger.info("delete:current=" + current + ", value=" + value);
|
|
// removeKey.add(key);
|
|
// }
|
|
//// else {
|
|
//// logger.info("remain:current=" + current + ", value=" + value);
|
|
//// }
|
|
// }
|
|
// for (String key : removeKey) {
|
|
// map.remove(key);
|
|
// }
|
|
// if (removeKey.size() > 0)
|
|
// log.info(String.format("clean %d keys, remain %d keys", removeKey.size(), map.size()));
|
|
// }
|
|
// }, 10, period, TimeUnit.SECONDS);
|
|
// }
|
|
//
|
|
// @Override
|
|
// public boolean add(String key, int expire) {
|
|
// log.info("add key=" + key);
|
|
// map.put(key, (int) (System.currentTimeMillis() / 1000) + expire);
|
|
// return true;
|
|
// }
|
|
//
|
|
// @Override
|
|
// public boolean add(String key) {
|
|
// return add(key, DEFAULT_EXPIRE);
|
|
// }
|
|
//
|
|
// @Override
|
|
// public boolean exists(String key) {
|
|
// Integer value = map.get(key);
|
|
// if (value == null) {
|
|
// return false;
|
|
// } else {
|
|
// return value > System.currentTimeMillis() / 1000;
|
|
// }
|
|
// }
|
|
//
|
|
//
|
|
//}
|
|
|