最近在跟网易云课堂的老师学习,把老师讲的案例自己实践了一遍,分享出来,希望对初学者有所帮助,话不多说,上代码……
package com.study.map;
import sun.jvm.hotspot.ui.action.FindAction;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* 基于map的缓存工具类
* @author tongke
* @email tongkp@126.com
* @create 2020-07-20 21:53
*/
public class CacheProvider {
//存放缓存的集合
private final static Map<String, CacheData> cacheDatas = new ConcurrentHashMap<>();
//定时器线程池,用于消除过期缓存
private final static ScheduledThreadPoolExecutor EXECUTOR = new ScheduledThreadPoolExecutor(5);
/**
* 1 获取缓存数据
* @param key
* @param <T>
* @return
*/
public synchronized <T> T get(String key){
CacheData cacheData = cacheDatas.get(key);
return cacheData == null ? null : (T) cacheData.data;
}
//2 设置
/**
* 设置缓存 永不失效
* @param key
* @param value
*/
public synchronized void put(String key, Object value){
this.put(key, value, -1L);
}
/**
* 设置缓存,需要设置超时时间
* @param key
* @param value
* @param expire 时间单位:毫秒,-1表示永不超时
*/
public synchronized void put(String key, Object value, Long expire){
//清除原数据
cacheDatas.remove(key);
if(expire > 0){ //大于0才设置
EXECUTOR.schedule(new Runnable() {
@Override
public void run() {
//过期后清除缓存
synchronized (this){
cacheDatas.remove(key);
}
}
}, expire, TimeUnit.MILLISECONDS);
cacheDatas.put(key, new CacheData(value, expire));
}else {
cacheDatas.put(key, new CacheData(value, -1L));
}
}
/**
* 3 删除
* @param key
* @param <T>
* @return
*/
public synchronized <T> T remove(String key){
CacheData cacheData = cacheDatas.remove(key);
return cacheData == null ? null : (T) cacheData.data;
}
/**
* 4 总数
* @return
*/
public synchronized int size(){
return cacheDatas.size();
}
/**
* 缓存实体类
*/
public class CacheData{
//缓存数据
public Object data;
//失效时间
public Long expire;
public CacheData(Object data, Long expire) {
this.data = data;
this.expire = expire;
}
}
}
测试类:
package com.study.map;
import org.junit.jupiter.api.Test;
/**
* @author tongke
* @email tongkp@126.com
* @create 2020-07-20 22:24
*/
public class CacheTest {
@Test
public void test() throws InterruptedException {
CacheProvider provider = new CacheProvider();
String key = "id";
System.out.println("*********************** 不设置过期时间 ***********************");
provider.put(key, 123);
System.out.println("get key:" + key + ", value:" + provider.get(key));
System.out.println("remove key:" + key + ", value:" + provider.remove(key));
System.out.println("get key:" + key + ", value:" + provider.get(key));
System.out.println("*********************** 不设置过期时间 ***********************");
provider.put(key, 123456, 1000L);
System.out.println("get key:" + key + ", value:" + provider.get(key));
Thread.sleep(2000L);
System.out.println("get key:" + key + ", value:" + provider.get(key));
}
}
测试结果:
*********************** 不设置过期时间 ***********************
get key:id, value:123
remove key:id, value:123
get key:id, value:null
*********************** 设置了过期时间 ***********************
get key:id, value:123456
get key:id, value:null