ConcurrentMap实现本地缓存,LocalCacheUtil.工具类

本文介绍了一个利用Java中ConcurrentMap实现的本地缓存工具类LocalCacheUtil,该工具类能够有效地管理本地缓存数据,并提供了一种简单的方法来设置、获取和删除缓存项。此外,还提供了一个具体的使用示例,展示了如何将数据库连接存储到本地缓存中。

ConcurrentMap实现本地缓存,LocalCacheUtil.工具类,可直接使用!

LocalCacheUtil.java

package org.jeecg.bigdata.common.dataSource.utils;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * local cache tool
 *
 * @author xuxueli 2018-01-22 21:37:34
 */
public class LocalCacheUtil {
    private static ConcurrentMap<String, LocalCacheData> cacheRepository = new ConcurrentHashMap<String, LocalCacheData>();   // 类型建议用抽象父类,兼容性更好;
    private static class LocalCacheData{
        private String key;
        private Object val;
        private long timeoutTime;
        public LocalCacheData() {
        }
        public LocalCacheData(String key, Object val, long timeoutTime) {
            this.key = key;
            this.val = val;
            this.timeoutTime = timeoutTime;
        }
        public String getKey() {
            return key;
        }
        public void setKey(String key) {
            this.key = key;
        }
        public Object getVal() {
            return val;
        }
        public void setVal(Object val) {
            this.val = val;
        }
        public long getTimeoutTime() {
            return timeoutTime;
        }
        public void setTimeoutTime(long timeoutTime) {
            this.timeoutTime = timeoutTime;
        }
    }


    /**
     * set cache
     *
     * @param key
     * @param val
     * @param cacheTime
     * @return
     */
    public static boolean set(String key, Object val, long cacheTime){
        // clean timeout cache, before set new cache (avoid cache too much)
        cleanTimeoutCache();
        // set new cache
        if (key==null || key.trim().length()==0) {
            return false;
        }
        if (val == null) {
            remove(key);
        }
        if (cacheTime <= 0) {
            remove(key);
        }
        long timeoutTime = System.currentTimeMillis() + cacheTime;
        LocalCacheData localCacheData = new LocalCacheData(key, val, timeoutTime);
        cacheRepository.put(localCacheData.getKey(), localCacheData);
        return true;
    }

    /**
     * remove cache
     *
     * @param key
     * @return
     */
    public static boolean remove(String key){
        if (key==null || key.trim().length()==0) {
            return false;
        }
        cacheRepository.remove(key);
        return true;
    }

    /**
     * get cache
     *
     * @param key
     * @return
     */
    public static Object get(String key){
        if (key==null || key.trim().length()==0) {
            return null;
        }
        LocalCacheData localCacheData = cacheRepository.get(key);
        if (localCacheData!=null && System.currentTimeMillis()<localCacheData.getTimeoutTime()) {
            return localCacheData.getVal();
        } else {
            remove(key);
            return null;
        }
    }

    /**
     * clean timeout cache
     *
     * @return
     */
    public static boolean cleanTimeoutCache(){
        if (!cacheRepository.keySet().isEmpty()) {
            for (String key: cacheRepository.keySet()) {
                LocalCacheData localCacheData = cacheRepository.get(key);
                if (localCacheData!=null && System.currentTimeMillis()>=localCacheData.getTimeoutTime()) {
                    cacheRepository.remove(key);
                }
            }
        }
        return true;
    }
}

测试使用:将数据库链接存到本地缓存(部分代码)

 if (null==LocalCacheUtil.get(jobDatasource.getName())) {
            getDataSource(jobDatasource);
        } else {
            this.connection = (Connection) LocalCacheUtil.get(jobDatasource.getName());
            if (!this.connection.isValid(500)) {
                LocalCacheUtil.remove(jobDatasource.getName());
                getDataSource(jobDatasource);
            }
        }
  LocalCacheUtil.set(jobDatasource.getName(), this.connection, 4 * 60 * 60 * 1000);
实现线程安全的本地缓存工具类有多种方式,以下进行详细介绍: ### 使用 ConcurrentHashMap 可以使用`ConcurrentHashMap`来实现一个简单的线程安全本地缓存。`ConcurrentHashMap`本身是线程安全的,但需要自己实现对象上限、过期策略以及清除策略。示例代码如下: ```java import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class SimpleLocalCache<K, V> { private final Map<K, CacheEntry<V>> cache = new ConcurrentHashMap<>(); private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); public SimpleLocalCache() { // 定期清理过期缓存 scheduler.scheduleAtFixedRate(this::cleanup, 1, 1, TimeUnit.MINUTES); } public void put(K key, V value, long expirationMillis) { long expirationTime = System.currentTimeMillis() + expirationMillis; cache.put(key, new CacheEntry<>(value, expirationTime)); } public V get(K key) { CacheEntry<V> entry = cache.get(key); if (entry != null && entry.isValid()) { return entry.getValue(); } else { cache.remove(key); return null; } } public void remove(K key) { cache.remove(key); } private void cleanup() { long now = System.currentTimeMillis(); cache.entrySet().removeIf(entry ->!entry.getValue().isValid(now)); } private static class CacheEntry<V> { private final V value; private final long expirationTime; public CacheEntry(V value, long expirationTime) { this.value = value; this.expirationTime = expirationTime; } public V getValue() { return value; } public boolean isValid() { return isValid(System.currentTimeMillis()); } public boolean isValid(long now) { return now < expirationTime; } } } ``` ### 使用 Caffeine Caffeine 是一个高性能的 Java 缓存库,它提供了丰富的功能,如过期策略、缓存淘汰策略等,并且是线程安全的。示例代码如下: ```java import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Caffeine; import java.util.concurrent.TimeUnit; public class CaffeineLocalCache<K, V> { private final Cache<K, V> cache; public CaffeineLocalCache(long expirationTime, int maximumSize) { cache = Caffeine.newBuilder() .expireAfterWrite(expirationTime, TimeUnit.SECONDS) .maximumSize(maximumSize) .build(); } public void put(K key, V value) { cache.put(key, value); } public V get(K key) { return cache.getIfPresent(key); } public void remove(K key) { cache.invalidate(key); } } ``` ### 使用 Guava Cache Guava Cache 是 Google 开源的一个本地缓存库,它也提供了线程安全的缓存实现。示例代码如下: ```java import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import java.util.concurrent.TimeUnit; public class GuavaLocalCache<K, V> { private final Cache<K, V> cache; public GuavaLocalCache(long expirationTime, int maximumSize) { cache = CacheBuilder.newBuilder() .expireAfterWrite(expirationTime, TimeUnit.SECONDS) .maximumSize(maximumSize) .build(); } public void put(K key, V value) { cache.put(key, value); } public V get(K key) { return cache.getIfPresent(key); } public void remove(K key) { cache.invalidate(key); } } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值