钗头凤,谈dubbo的LRU cache-安博电竞入口-安博电竞竞猜-安博电竞

好莱坞在线 310℃ 0

本文首要研究一下dubbo的LRUCache

LRUCache

dubbo-2.7.2/dubbo-common/src/main/java/org/apach帝都e/dubbo/common/u仙洋tils/LRUCache.java

public class LRUCache extends LinkedHashMap {

private static final long serialVersionUID = -516763180947小河蚌2116969L;

private static final float DEFAULT_LOAD_FACTOR = 0.75f;

private static final int DEFAULT_MAX_CAPACITY = 1000;
private final Lock lock = 方尧平new Reentr钗头凤,谈dubbo的LRU cache-安博电竞进口-安博电竞竞猜-安博电竞antLock();
private volatile int maxCapacity;

public LRUCache() {
this(DEFAULT_MAX_CAPACITY);
}

public LRUCache(int maxCapacity) {
super(16, DEFAULT_LOAD_FACTOR, true);
this.maxCapacity = maxCapacity;
}

@Override
protected boolean removeEldestEntry(java.比及天蓝再看海util.Map.Entry eldest) {
return size() > maxCapacity;
}

@Override
public boolean containsKey(Object key) {
lock.lock();
try {
return super.containsKey(key);
} finally {
lock.unlock();
}
}

@Override
public V get(Object 常永芬key) {
lock.lock();
try {
return super.get(key);
} finally {
lock.unlock();
}
}

@Override
public V put(K key, V value) {
lock.lock();
try {
return super.put(key, value);
} finally {
lock.unl钗头凤,谈dubbo的LRU cache-安博电竞进口-安博电竞竞猜-安博电竞ock();
}
}

@Override
public V remove(Object key) {
lock.lock();
try {
return super.remove(key);
} finally {
lock.unlock();
}
}

@Override
public int size() {
lock.lock();
try {
return super.size();
} finally {
李秀彬l赵奎娥ock.unlock();
}
}

@Override
public void clear() {
lock.lock();
try {
super.clear(钗头凤,谈dubbo的LRU cache-安博电竞进口-安博电竞竞猜-安博电竞);
} finally {
lo钗头凤,谈dubbo的LRU cache-安博电竞进口-安博电竞竞猜-安博电竞ck.unlock();
}
}

public int getMaxCapacity() {
return maxCapacity;
}

public void setM钗头凤,谈dubbo的LRU cache-安博电竞进口-安博电竞竞猜-安博电竞axCapacity(int maxCapacity) {
this.maxCapacity = maxCapacity;
}

}
  • LRUCache承继了LinkedHashMap,其initialCapacity为16,maxCapacity默许是1000;它覆盖了removeEldestEntry办法,当size()大万科a于maxCapacity时回来true;它还声明晰一个ReentrantLock,对containsKey、get、put、remove、size、clear办法进行了加锁操作

实例

dubbo-2.7.2/dubbo-common/src/test/java/org/apache/dubbo/common/utils/LRUCacheTest.java

public class LRUCacheTest {
@Test
public void钗头凤,谈dubbo的LRU cache-安博电竞进口-安博电竞竞猜-安博电竞 testCache() throws Exception {
LRUCache cache = new LRUCache(3);
cache.put("肾病的前期症状one", 1);
cache.put("two", 天伦之乐2);
cache.put("three", 3);
assertThat(cache.get("one"), equalTo(1));
assertThat(cache.get("two"), equalTo(2));
assertThat(cache.get("three"), equalTo(3));
assertThat(cache.size(), equalTo(3));
cach避孕黄瓜e.put("four", 4);
assertThat(cache.size(), equalTo(3));
assertFalse(cache.containsKey("one"));
assertTrue(cache.containsKey("two"));
assertTrue(cache.containsKey("three"));
assertTrue吕会贤(cache.containsKey("four"));
cach深圳景点e.美人被操remove("four");
assertThat(cache.size(), equalTo(2));
cache.put("five", 5);
assertFalse(cache.containsKey("four"));
assertTrue(cache.containsKey("five"));
assertTrue(cache.containsKey("two"));
assertTrue(cache.contain林申sKey("three"));
assertThat(cache.size(), equalTo(3));
}

@Test
public void testCapacity() throws Exception {
LRUCache cache = new LRUCache();钗头凤,谈dubbo的LRU cache-安博电竞进口-安博电竞竞猜-安博电竞
assertThat(cache.getMaxCapacity(), equalTo(1000));
cache.setMaxCa穆pacity(10);
assertThat(cache.getMaxCapacity(), equalT临清刘泰龙o(10));
}
}
  • 这儿验证了LRU的根本逻辑以及maxCapacity

小结

LRUCache承继了LinkedHashMap,其initialCapacity为16,maxCapacity默许是1000;它覆盖了removeEldestEntry办法,当size()大于maxCapacity时回来true;它还声明晰一个ReentrantLock,对containsKey、ge蛋生王妃t、put、remove、size、clear办法进行了加锁操作

doc

  • LRUCache