自定义缓存实现

3 篇文章 0 订阅

直接上代码:

一、定义缓存通用接口

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实现

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值