欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

自定义缓存实现

程序员文章站 2022-05-19 15:40:08
...

直接上代码:

一、定义缓存通用接口

package com.example.cache.spring;

/**
 * 定义缓存
 * @author OKali
 *
 */
public interface Cache<K, V> {

	// 返回缓存实例名称
	String getName();
	
	Object getCacheValue(K key);
	
	void setCacheObject(K key, V value);
	
	void clearCache(String name);
}

二、定义缓存管理器:

package com.example.cache.spring;

/**
 * 缓存管理
 * @author OKali
 *
 */
public interface CacheManager<K, V> {
	
	/**
	 * 获取缓存
	 * @param name
	 * @return
	 */
	Cache<K, V> getCache(String name);
	
	/**
	 * 清除缓存
	 * @param name
	 */
	void flushCache(String name);
	
}

三、实现缓存

package com.example.cache.spring;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 缓存升级版,支持根据缓存名称使用不同缓存实例
 * TODO 后期加入失效时间等内容
 * @author OKali
 *
 */
public class ConfigContext implements Cache<String, Object> {
	
	/**
	 * 缓存名称
	 */
	private String name;
	
	public final ConcurrentHashMap<String, Object> store = new ConcurrentHashMap<>();
	
	public ConfigContext(String name) {
		this.name = name;
	}
	
	public void setCacheObject(String key, Object value) {
		synchronized (this.store) {
			this.store.put(key, value);
		}
	}
	
	public Object getCacheValue(String key) {
		return this.store.get(key);
	}
	
	public void clearCache(String name) {
		this.store.clear();
	}

	@Override
	public final String getName() {
		return this.name;
	}
	
}

四、实现缓存管理

package com.example.cache.spring;

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


/**
 * ConfigCache 缓存管理
 * (饿汉单例模式--线程安全)
 * @author OKali
 *
 */
public class ConfigCache implements CacheManager<String, Object> {
	
	private final ConcurrentMap<String, Cache<String, Object>> cacheMap = new ConcurrentHashMap<String, Cache<String, Object>>(16);
	private static final ConfigCache CONFIG_CACHE = new ConfigCache();
	
	private ConfigCache() {}
	
	public static final ConfigCache getConfigCacheManger() {
		return CONFIG_CACHE;
	}

	@Override
	public Cache<String, Object> getCache(String name) {
		synchronized (this.cacheMap) { // 非必需
			Cache<String, Object> cache = this.cacheMap.get(name);
			if (cache == null) {
				// 创建缓存
				cache = new ConfigContext(name);
				this.cacheMap.put(name, cache);
			}
		return cache;
		}
	}
	
	@Override
	public void flushCache(String name) {
		Cache<String, Object> cache = this.cacheMap.get(name);
		cache.clearCache(name);
	}

}

测试:

package com.example.cache.spring;

public class TestConfigCache {

	public static void main(String[] args) {
		String msg = "xxxxxxxxxxxxxxx";
		String msg2 = "yyyyyyyyyyyyyy";
		String name ="alison";
		String name2 = "pamgo";
		ConfigContext configContext = (ConfigContext) ConfigCache.getConfigCacheManger().getCache(name);
		ConfigContext configContext3 = (ConfigContext) ConfigCache.getConfigCacheManger().getCache(name);
		if (configContext == configContext3) {
			System.out.println("同一个实例");
		} else {
			System.out.println("不是同一个实例");
		}
		ConfigContext configCache2 = (ConfigContext) ConfigCache.getConfigCacheManger().getCache(name2);
		for (int i = 0; i < 100; i++) {
			String cacheMsg = (String) configContext.getCacheValue("key");
			if (cacheMsg == null) {
				configContext.setCacheObject("key", msg);
				System.out.println("非缓存中读取《《《《《《《《《《《《《《《《");
			} else {
				System.out.println(name+"缓存中读取");
			}
		}
		for (int j = 0; j < 100; j++) {
			String cacheMsg2 = (String) configCache2.getCacheValue("key2");
			if (cacheMsg2 == null) {
				configCache2.setCacheObject("key2", msg2);
				System.out.println("非缓存中读取《《《《《《《《《《《《《《《《");
			} else {
				System.out.println(name2+"缓存中读取");
			}
			
		}
		ConfigCache.getConfigCacheManger().flushCache(name2);
		System.out.println("缓存一名称:"+configContext.getName()+"信息:" + configContext.store.toString());
		System.out.println("缓存二名称:" + configCache2.getName()+"信息:" + configCache2.store.toString());
	}
}


具体请参考Spring 中的Cache以及Manager实现