java本地缓存

描述

Java提供了多种本地缓存的实现方式,这里演示使用ConcurrentHashMap做本地缓存

ConcurrentHashMap:ConcurrentHashMap是HashMap的线程安全版本,它通过使用锁分段技术来提高并发性能。ConcurrentHashMap适用于多线程环境下的本地缓存需求,可以提供更好的并发性能。

定义缓存对象类

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @desc 缓存数据, 包含缓存值,创建时间,过期时间
 **/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CacheValue {
    //缓存值
    Object value;
    //创建时间,时间戳
    Long createTime;
    //过期时间,默认永不过期,单位为s
    Long expireTime = -1L;
}

本地缓存操作类

import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @desc 本地缓存操作类
 **/
public class LocalCacheTemplate {
    private static final Logger log = LoggerFactory.getLogger(LocalCacheTemplate.class);
    private static final ConcurrentHashMap<String, CacheValue> cache = new ConcurrentHashMap<>();
    public static final int mill = 1000;//1000ms,毫秒转换为秒

    @SneakyThrows
    public static void main(String[] args) {
        set("name","yangjie",3);
        System.out.println("the result is " + get("name"));
        Thread.sleep(1000);
        System.out.println("the result is " + get("name"));
        Thread.sleep(3000);
        System.out.println("the result is " + get("name"));
        Thread.sleep(1000);
        System.out.println("the result is " + get("name"));
        Thread.sleep(1000);
        System.out.println("the result is " + get("name"));
    }

    /**
     * @desc 根据缓存key获取缓存值
     * @param key  缓存key
     * @return java.lang.Object 缓存值
     **/
    public static Object get(String key) {
        if (!cache.containsKey(key)) {
            return null;
        }
        CacheValue cacheValue = cache.get(key);
        if (isOverTime(cacheValue)) {
            log.debug("cache is over time");
            cache.remove(key);//删除
            return null;
        } else {
            return cacheValue.getValue();
        }
    }

    /**
     * @desc 根据缓存key获取缓存值
     * @param key  缓存key
     * @return CacheValue 缓存数据类
     **/
    public static CacheValue getCacheValue(String key) {
        if (!cache.containsKey(key)) {
            return null;
        }
        CacheValue cacheValue = cache.get(key);
        if (isOverTime(cacheValue)) {
            log.debug("cache is over time");
            cache.remove(key);//删除
            return null;
        } else {
            return cacheValue;
        }
    }

    /**
     * @desc 设置缓存值
     * @param key 缓存key
     * @param value 缓存值
     */
    public static void set(String key,Object value) {
        Long createTime = System.currentTimeMillis();
        Long expireTime = -1l;
        CacheValue cacheValue = new CacheValue();
        cacheValue.setValue(value);
        cacheValue.setCreateTime(createTime);
        cacheValue.setExpireTime(expireTime);
        cache.put(key,cacheValue);
    }

    /**
     * @desc 设置缓存值,并且设置过期时间单位为秒
     * @param key 缓存key
     * @param value 缓存值
     * @param expireTime 过期时间,单位为秒
     */
    public static void set(String key,Object value,long expireTime) {
        Long createTime = System.currentTimeMillis();
        CacheValue cacheValue = new CacheValue();
        cacheValue.setValue(value);
        cacheValue.setCreateTime(createTime);
        cacheValue.setExpireTime(expireTime);
        cache.put(key,cacheValue);
    }


    /**
     * @desc 判断是否过期
     * @param cacheValue 缓存数据值
     * @return boolean 是否过期
     **/
    private static boolean isOverTime(CacheValue cacheValue) {
        Long createTime = cacheValue.getCreateTime();
        Long expireTime = cacheValue.getExpireTime();
        if(expireTime.equals(-1L)){
            return false;
        }
        if ((System.currentTimeMillis() - createTime) / mill > expireTime) {
            return true;
        }
        return false;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值