JAVA中基于Map实现缓存工具类(一)

3 篇文章 0 订阅

最近在跟网易云课堂的老师学习,把老师讲的案例自己实践了一遍,分享出来,希望对初学者有所帮助,话不多说,上代码……

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

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值