设为首页 加入收藏

TOP

java手写多级缓存(一)
2019-09-03 01:47:50 】 浏览:25
Tags:java 手写 多级

 

多级缓存实现类,时间有限,该类未抽取接口,目前只支持两级缓存:JVM缓存(实现 请查看上一篇:java 手写JVM高性能缓存)、redis缓存(在spring 的 redisTemplate 基础实现)

package com.ws.commons.cache;

import java.util.function.Supplier;

import com.ws.commons.tool.ThreadTool;

/**
 * 多级缓存实现
 * 
 * @author 尘无尘
 *
 */
public class MultilevelCache {

    private MultilevelCache() {
    }

    private static final ICache FIRST_LEVE_LCACHE = LocalCache.instance();
    private static ICache secondCache;

    private static final String LOCK_PREFIX = "MUILCACHE_LOCK:";

    public static synchronized void init(ICache secondCache) {
        if (MultilevelCache.secondCache == null) {
            MultilevelCache.secondCache = secondCache;
        }
    }

    public static void put(String key, Object value, int timeOutSecond) {
        FIRST_LEVE_LCACHE.put(key, value, timeOutSecond);
        if (secondCache != null) {
            secondCache.put(key, value, timeOutSecond);
        }
    }

    /**
     * 提供数据,并缓存
     * 
     * @param key
     * @param supplier
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T getForload(String key, Supplier<T> supplier) {
        T data = FIRST_LEVE_LCACHE.get(key);
        if (data == null && secondCache != null) {
            data = (T) secondCache.get(key);
        }
        if (data != null) {
            return data;
        }

        synchronized (ThreadTool.buildLock(LOCK_PREFIX, key)) {
            data = FIRST_LEVE_LCACHE.get(key);
            if (data == null && secondCache != null) {
                data = (T) secondCache.get(key);
            }
            if (data != null) {
                return data;
            }

            data = supplier.get();
            if (secondCache != null) {
                secondCache.put(key, data);
            }
            FIRST_LEVE_LCACHE.put(key, data);
        }
        return data;
    }

    /**
     * 提供数据,并缓存一定的时间
     * 
     * @param key
     * @param timeOutSecond
     * @param supplier
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T getForload(String key, int timeOutSecond, Supplier<T> supplier) {
        T data = FIRST_LEVE_LCACHE.get(key);
        if (data == null && secondCache != null) {
            data = (T) secondCache.get(key);
        }
        if (data != null) {
            return data;
        }
        synchronized (ThreadTool.buildLock(LOCK_PREFIX, key)) {
            data = FIRST_LEVE_LCACHE.get(key);
            if (data == null && secondCache != null) {
                data = (T) secondCache.get(key);
            }
            if (data != null) {
                return data;
            }
            data = supplier.get();
            if (secondCache != null) {
                secondCache.put(key, data, timeOutSecond);
            }
            FIRST_LEVE_LCACHE.put(key, data, timeOutSecond);
        }
        return data;
    }

    @SuppressWarnings("unchecked")
    public static <T> T removeAndGet(String key) {
        T data = null;
        if (secondCache != null) {
            data = (T) secondCache.removeAndGet(key);
        }
        T data2 = FIRST_LEVE_LCACHE.removeAndGet(key);
        if (data == null) {
            data = data2;
        }
        return data;
    }

    public static void remove(String key) {
        if (secondCache != null) {
            secondCache.remove(key);
        }
        FIRST_LEVE_LCACHE.remove(key);
    }

    @SuppressWarnings("unchecked")
    public static <T> T get(String key) {
        T data = FIRST_LEVE_LCACHE.get(key);
        if (data == null && secondCache != null) {
            data = (T) secondCache.get(key);
        }
        return data;
    }

    public static void expire(String key, int timeOutSecond) {
        FIRST_LEVE_LCACHE.expire(key, timeOutSecond);
        if (secondCache != null) {
            secondCache.expire(key, timeOutSecond);
        }
    }

}

 

redis缓存实现类

packa
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇基于代理类实现Spring AOP 下一篇Redis的HelloWorld

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目