一般cache的做法

其实比较简单,直接来个例子,e.g.

TryCache.java

public class InforoneCache<K, V> implements Cache<K, V> {

	class CacheObject<K2, V2> {
		final K2 key;
		final V2 value;
		long lastModified; // 最后添加时间
		long expire; // 对象存活时间

		CacheObject(K2 key, V2 value, long expire) {
			this.key = key;
			this.value = value;
			this.lastModified = System.currentTimeMillis();
			this.expire = expire;
		}

		boolean isExpired() {
			if (expire == 0) {
				return false;
			}
			return lastModified + expire < System.currentTimeMillis();
		}

		V2 getObject() {
			return value;
		}
	}

	protected Map<K, CacheObject<K, V>> cacheMap;

	private final ReentrantReadWriteLock cacheLock = new ReentrantReadWriteLock();
	private final Lock readLock = cacheLock.readLock();
	private final Lock writeLock = cacheLock.writeLock();

	protected int cacheSize; // 缓存大小 , 0 -> 无限制

	protected boolean existCustomExpire; // 是否设置默认过期时间

	public int getCacheSize() {
		return cacheSize;
	}

	protected long defaultExpire; // 默认过期时间, 0 -> 永不过期

	public TryCache(int cacheSize, long defaultExpire) {
		this.cacheSize = cacheSize;
		this.defaultExpire = defaultExpire;
		cacheMap = new HashMap<K, CacheObject<K, V>>(cacheSize + 1);
	}

	public long getDefaultExpire() {
		return defaultExpire;
	}

	public void put(K key, V value) {
		put(key, value, defaultExpire);
	}

	public void put(K key, V value, long expire) {
		writeLock.lock();

		try {
			CacheObject<K, V> co = new CacheObject<K, V>(key, value, expire);
			if (expire != 0) {
				existCustomExpire = true;
			}
			cacheMap.put(key, co);
		} finally {
			writeLock.unlock();
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public V get(K key) {
		readLock.lock();

		try {
			CacheObject<K, V> co = cacheMap.get(key);
			if (co == null) {
				return null;
			}
			if (co.isExpired() == true) {
				cacheMap.remove(key);
				return null;
			}

			return co.getObject();
		} finally {
			readLock.unlock();
		}
	}

	public boolean isFull() {
		if (cacheSize == 0) {// o -> 无限制
			return false;
		}
		return cacheMap.size() >= cacheSize;
	}

	public void remove(K key) {
		writeLock.lock();
		try {
			cacheMap.remove(key);
		} finally {
			writeLock.unlock();
		}
	}

	public void clear() {
		writeLock.lock();
		try {
			cacheMap.clear();
		} finally {
			writeLock.unlock();
		}
	}

	public int size() {
		return cacheMap.size();
	}

	public boolean isEmpty() {
		return size() == 0;
	}

}

GetData.java

	static final InforoneCache<String, DatumMining> cache = new InforoneCache<String, DatumMining>(0, 30 * 60 * 1000L);
public getData() {
		Xxx xxx = cache.get(key);
		if (DatumMining == null) {
...
		}
		cache.put(key, xxx);
}


跨节点情况请参考:一致性hash算法: cache、负载均衡应用

http://blog.youkuaiyun.com/textboy/article/details/46004117

03-14
### 缓存的概念与实现 #### 缓存在操作系统中的作用 当进程始终运行在同一核心上时,由于缓存重用的可能性增加,其执行效率可能会更高[^1]。这是因为如果进程从一个CPU迁移到另一个CPU,则旧的指令和地址缓存会失效,而新CPU上的缓存则需要时间来填充数据。 #### 多线程环境下的缓存策略 在多线程环境中,可能的最佳解决方案是让每个线程维护自己的独立缓存[^2]。这种做法虽然引入了一些重复计算的低效性,但它可以减少因多个线程共享同一缓存而导致的竞争开销。 #### 新型内存技术对缓存的影响 随着CXL(Compute Express Link)协议的发展,尤其是到CXL 2.0版本时,访问的内存不一定局限于传统的DDR5类型。理论上可以通过CXL设备使用其他类型的存储器,比如Intel Optane、GDDR6X或其他新兴技术[^3]。这表明未来缓存的设计不仅依赖于传统RAM的速度提升,还可能涉及更多新型存储介质的应用场景。 ```python class CacheImplementationExample: def __init__(self, size): self.cache_size = size self.data_cache = {} def add_to_cache(self, key, value): if len(self.data_cache) >= self.cache_size: first_key = next(iter(self.data_cache)) del self.data_cache[first_key] self.data_cache[key] = value def retrieve_from_cache(self, key): return self.data_cache.get(key) example_cache = CacheImplementationExample(3) example_cache.add_to_cache('A', 'Value A') example_cache.add_to_cache('B', 'Value B') print(example_cache.retrieve_from_cache('A')) # Output: Value A ``` 上述代码展示了一个简单的缓存实现例子,其中定义了`CacheImplementationExample`类用于管理固定大小的数据缓存,并提供了添加和检索功能。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值