All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xiaoleilu.hutool.cache.impl.AbstractCache Maven / Gradle / Ivy

package com.xiaoleilu.hutool.cache.impl;

import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;

import com.xiaoleilu.hutool.cache.Cache;
import com.xiaoleilu.hutool.collection.CopiedIterator;

/**
 * 超时和限制大小的缓存的默认实现
* 继承此抽象缓存需要:
*
    *
  • 创建一个新的Map
  • *
  • 实现 prune 策略
  • *
* * @author Looly,jodd * * @param 键类型 * @param 值类型 */ public abstract class AbstractCache implements Cache{ protected Map> cacheMap; private final ReentrantReadWriteLock cacheLock = new ReentrantReadWriteLock(); private final ReadLock readLock = cacheLock.readLock(); private final WriteLock writeLock = cacheLock.writeLock(); /** 返回缓存容量,0表示无大小限制 */ protected int capacity; /** 缓存失效时长, 0 表示没有设置,单位毫秒 */ protected long timeout; /** 每个对象是否有单独的失效时长,用于决定清理过期对象是否有必要。 */ protected boolean existCustomTimeout; /** 命中数 */ protected int hitCount; /** 丢失数 */ protected int missCount; // ---------------------------------------------------------------- put start @Override public void put(K key, V object) { put(key, object, timeout); } @Override public void put(K key, V object, long timeout) { writeLock.lock(); try { CacheObj co = new CacheObj(key, object, timeout); if (timeout != 0) { existCustomTimeout = true; } if (isFull()) { pruneCache(); } cacheMap.put(key, co); } finally { writeLock.unlock(); } } // ---------------------------------------------------------------- put end // ---------------------------------------------------------------- get start @Override public boolean containsKey(K key) { readLock.lock(); try { //不存在或已移除 final CacheObj co = cacheMap.get(key); if (co == null) { return false; } //过期 if (co.isExpired() == true) { // remove(key); // 此方法无法获得锁 removeWithoutLock(key); missCount++; return false; } //命中 return true; } finally { readLock.unlock(); } } /** * @return 命中数 */ public int getHitCount() { return hitCount; } /** * @return 丢失数 */ public int getMissCount() { return missCount; } @Override public V get(K key) { readLock.lock(); try { //不存在或已移除 final CacheObj co = cacheMap.get(key); if (co == null) { missCount++; return null; } //过期 if (co.isExpired() == true) { // remove(key); // 此方法无法获得锁 removeWithoutLock(key); missCount++; return null; } //命中 hitCount++; return co.get(); } finally { readLock.unlock(); } } // ---------------------------------------------------------------- get end @Override public Iterator iterator() { CopiedIterator> copiedIterator; readLock.lock(); try { copiedIterator = CopiedIterator.copyOf(this.cacheMap.values().iterator()); } finally { readLock.unlock(); } return new CacheValuesIterator(copiedIterator); } // ---------------------------------------------------------------- prune start /** * 清理实现 * @return 清理数 */ protected abstract int pruneCache(); @Override public final int prune() { writeLock.lock(); try { return pruneCache(); } finally { writeLock.unlock(); } } // ---------------------------------------------------------------- prune end // ---------------------------------------------------------------- common start @Override public int capacity() { return capacity; } /** * @return 默认缓存失效时长。
* 每个对象可以单独设置失效时长 */ @Override public long timeout() { return timeout; } /** * 只有设置公共缓存失效时长或每个对象单独的失效时长时清理可用 * @return 过期对象清理是否可用,内部使用 */ protected boolean isPruneExpiredActive() { return (timeout != 0) || existCustomTimeout; } @Override public boolean isFull() { return (capacity > 0) && (cacheMap.size() >= capacity); } @Override public void remove(K key) { writeLock.lock(); CacheObj co; try { co = cacheMap.remove(key); } finally { writeLock.unlock(); } if(null != co){ onRemove(co.key, co.obj); } } @Override public void clear() { writeLock.lock(); try { cacheMap.clear(); } finally { writeLock.unlock(); } } @Override public int size() { return cacheMap.size(); } @Override public boolean isEmpty() { return cacheMap.isEmpty(); } @Override public String toString() { return this.cacheMap.toString(); } // ---------------------------------------------------------------- common end /** * 对象移除回调。默认无动作 * @param key 键 * @param cachedObject 被缓存的对象 */ protected void onRemove(K key, V cachedObject) { } /** * 移除元素,无锁 * @param key 键 */ private void removeWithoutLock(K key) { CacheObj co = cacheMap.remove(key); if(null != co){ onRemove(co.key, co.obj); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy