在 Java 5.0 提供了 java.util.concurrent(简称JUC)包,在此包中增加了在并发编程中很常用的工具类, 用于定义类似于线程的自定义子系统,包括线程池,异步 IO 和轻量级任务框架;还提供了设计用于多线程上下文中 的 Collection 实现等;
业务:利用jdk JUC, java.util.concurrent里的类实现定时缓存,缓存可以设置过期,过期可以定时清缓存。对于业务数据来说,如果缓存有数据,就直接读缓存(内存),缓存没数据才读数据库,读取之后要将数据再丢到缓存,代码参考:
ps:本代码例子比较简单,只适合业务比较简单的数据缓存,主要是通过ConcurrentMap保证线程安全,ScheduledExecutorService用于创建定时线程池
import com.extra.login.cas.client.util.CasParamKeyEnum;
import com.extra.login.cas.client.util.CasPropertiesLoader;
import com.common.utils.ApplicationContextHolder;
import com.webConfig.service.WebConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.*;
/**
* <pre>
* 获取配置数据,并加上local缓存
* </pre>
*
* <pre>
* @author mazq
* 修改记录
* 修改后版本: 修改人: 修改日期: 2020/08/26 16:59 修改内容:
* </pre>
*/
@Component
public class FilterCondition {
Logger LOG = LoggerFactory.getLogger(FilterCondition.class);
private static String CACHE_KEY = "CAS_LOGIN_MODE";
// private static int EXPIRE = 24 * 60 * 60 * 1000;
private static int EXPIRE = Integer.parseInt(CasPropertiesLoader.getValue(CasParamKeyEnum.CACHE_LOCALE_EXPIRE.getCasParamKey()));
/**
* 读取配置数据,加上缓存
* @Author mazq
* @Date 2020/09/07 17:52
*
*/
public static boolean isCasLoginMode() {
WebConfigService webConfigService = (WebConfigService) ApplicationContextHolder.getApplicationContext().getBean("webConfigService");
Boolean casModeFlagCache = CacheManager.get(CACHE_KEY , Boolean.class);
if (casModeFlagCache != null) { // 缓存有数据,返回缓存
return casModeFlagCache;
}
casModeFlagCache = webConfigService.getConfigFlag("${hzCasLogin_Boolean}");// 缓存没数据,重现读取数据库
CacheManager.put(CACHE_KEY, casModeFlagCache, EXPIRE); // 将数据库数据再丢到缓存
return casModeFlagCache;
}
static class CacheManager {
private static ConcurrentMap<String, CacheWrapper> CACHE;
private static ScheduledExecutorService executor;// 定时器线程池
static {
CACHE = new ConcurrentHashMap<String, CacheWrapper>();
executor = Executors.newSingleThreadScheduledExecutor();
}
public static void put(String key, Object data) {
put(key, data, 0);
}
public static void put(final String key, Object value, int expire) {
CACHE.remove(key);// 将原来的缓存数据先remove
if (expire > 0) { // 有配置expire
Future future = executor.schedule(new Runnable() {
@Override
public void run() {
CACHE.remove(key);
}
}, expire, TimeUnit.MILLISECONDS);
CACHE.put(key, new CacheWrapper(value, future));
} else {
CACHE.put(key, new CacheWrapper(value, null));
}
}
public static <T> T get(String key, Class<T> clazz) {
return clazz.cast(get(key));
}
public static Object get(String key) {
CacheWrapper cacheWrapper = (CacheWrapper) CACHE.get(key);
return cacheWrapper == null ? null : cacheWrapper.getValue();
}
public static int size() {
return CACHE.size();
}
public static Object remove(String key) {
CacheWrapper cacheWrapper = CACHE.remove(key);
if (cacheWrapper == null) return null;
//清除原键值对定时器
Future future = cacheWrapper.getFuture();
if (future != null) future.cancel(true);
return cacheWrapper.getValue();
}
}
static class CacheWrapper {
private Object value;
private Future future;
public CacheWrapper(Object value, Future future) {
this.value = value;
this.future = future;
}
public Object getValue() {
return value;
}
public Future getFuture() {
return future;
}
}
public static void main(String[] args) throws InterruptedException {
String key = "key";
CacheManager.put(key, "testValue", 1000);
System.out.println("key:" + key + ", value:" + CacheManager.get(key));
Thread.sleep(2000);
System.out.println("key:" + key + ", value:" + CacheManager.get(key));
}
}