Java 本地缓存设计与实现:Cache(Google-Guava)& Map(JDK)

一、概要

1、设计目标:

 * 缓存 
 * 存储形式:键值对; 
 * 主要功能:存、取、清除单个、清除全部...

二、实现

1、实现思路

定义一个接口(ICache),来规范缓存类该有的行为(存、取、清除单个、清除全部...)

实现类来实现 ICache

2、Demo

2.1 基于 基于 Google-Guava的Cache 实现的本地缓存

技术点:

com.google.common.cache.Cache

package spzc.platform.utils.cache.localcache;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

import spzc.platform.utils.cache.ICache;

/**
 * 基于Guava实现的本地缓存
 * 
 * @author live Dec 28, 2018 7:06:58 PM
 * @param 
 * @param 
 */
public class GuavaChache implements ICache {

	// 最大值
	private Long maximumSize;
	// 写入到缓存后,多久过期
	private Duration expireAfterWrite;
	// 访问后,多久过期
	private Duration expireAfterAccess;

	private Cache cache;

	public GuavaChache() {
		this(null, null, null);
	}

	public GuavaChache(Long maximumSize, Duration expireAfterWrite, Duration expireAfterAccess) {
		this.maximumSize = maximumSize;
		this.expireAfterWrite = expireAfterWrite;
		this.expireAfterAccess = expireAfterAccess;
		this.cache = new GuavaCacheUtil().buildCache(maximumSize, expireAfterWrite, expireAfterAccess);
	}

	public GuavaChache(Long maximumSize, Duration expireAfterWrite) {
		this(maximumSize, expireAfterWrite, null);
	}

	public GuavaChache(Duration expireAfterWrite) {
		this(null, expireAfterWrite, null);
	}

	public Long getMaximumSize() {
		return maximumSize;
	}

	public void setMaximumSize(long maximumSize) {
		this.maximumSize = maximumSize;
	}

	public Duration getExpireAfterWrite() {
		return expireAfterWrite;
	}

	public void setExpireAfterWrite(Duration expireAfterWrite) {
		this.expireAfterWrite = expireAfterWrite;
	}

	public Duration getExpireAfterAccess() {
		return expireAfterAccess;
	}

	public void setExpireAfterAccess(Duration expireAfterAccess) {
		this.expireAfterAccess = expireAfterAccess;
	}

	public Cache getCache() {
		return cache;
	}

	public void setCache(Cache cache) {
		this.cache = cache;
	}

	@Override
	public V get(K key) {
		return cache.getIfPresent(key);
	}

	public V getOrLoad(K key, Callable callable) {
		try {
			return cache.get(key, callable);
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void put(K key, V v) {
		this.cache.put(key, v);
	}

	@Override
	public void remove(K key) {
		this.cache.invalidate(key);
	}

	@Override
	public void clear() {
		this.cache.invalidateAll();
	}

	/**
	 * 工具类
	 * 构建 com.google.common.cache.Cache
	 * @author live
	 * Dec 29, 2018 10:45:47 AM
	 * @param 
	 * @param 
	 */
	private class GuavaCacheUtil {
		Cache buildCache(Long maximumSize, Duration expireAfterWrite, Duration expireAfterAccess) {
			CacheBuilder builder = CacheBuilder.newBuilder();
			if (maximumSize != null) {
				builder.maximumSize(maximumSize);
			}
			if (expireAfterWrite != null) {
				builder.expireAfterWrite(expireAfterWrite.getTime(), expireAfterAccess.getTimeUnit());
			}
			if (expireAfterAccess != null) {
				builder.expireAfterWrite(expireAfterAccess.getTime(), expireAfterAccess.getTimeUnit());
			}
			return builder.build();
		}
	}

	/**
	 * “时长类”:
	 * 封装了时间、时间单位
	 * @author live
	 * Dec 29, 2018 10:44:55 AM
	 */
	public static class Duration {
		private long time;
		private TimeUnit timeUnit;

		public Duration(long time, TimeUnit timeUnit) {
			super();
			this.time = time;
			this.timeUnit = timeUnit;
		}

		public long getTime() {
			return time;
		}

		public void setTime(long time) {
			this.time = time;
		}

		public TimeUnit getTimeUnit() {
			return timeUnit;
		}

		public void setTimeUnit(TimeUnit timeUnit) {
			this.timeUnit = timeUnit;
		}
	}
}

2.2 基于 Map 实现的本地缓存

技术点:

java.util.Map

java.util.concurrent.ConcurrentHashMap

package spzc.platform.utils.cache.localcache;

import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;

import spzc.platform.utils.cache.ICache;
/**
 * 基于map实现的本地缓存
 * @author live
 * Dec 28, 2018 7:06:58 PM
 * @param 
 * @param 
 */
public class MapCache implements ICache{

	private Map map = new ConcurrentHashMap();
	
	@Override
	public V get(K key) {
		return map.get(key);
	}

	@Override
	public V getOrLoad(K key, Callable callable) {
		try {
			return callable.call();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void put(K key, V v) {
		map.put(key, v);
	}

	@Override
	public void remove(K key) {
		map.remove(key);
	}

	@Override
	public void clear() {
		map.clear();
	}

}

 

你可能感兴趣的:(本地缓存,Java)