mybatis中cache的类型
PerpetualCache
永久缓存 一旦存入就一直保持,只要两个id相同就认为是两个相同的cache
SynchronizedCache
同步缓存
SerializedCache
序列化缓存
LoggingCache
日志缓存
WeakCache
弱引用缓存,可以看到代码和SoftCache如出一辙,就是SoftReference变成了WeakReference
使用到数据对象Deque Deques can also be used as LIFO (Last-In-First-Out) stacks 是一个队列集合,支持队列两端插入和删除元素
SoftCache
软引用缓存,核心是SoftReference
涉及到的引用
Reference->WeakReference->SoftReference引用相关 对象回收相关
ScheduledCache
定时调度缓存,目的是每一小时清空一下缓存
private boolean clearWhenStale() {
//如果到时间了,清空一下缓存
if (System.currentTimeMillis() - lastClear > clearInterval) {
clear();
return true;
}
return false;
}
FifoCacheFIFO缓存
这个类就是维护一个FIFO链表,其他都委托给所包装的cache去做。典型的装饰模式
Deque<Object> keyList//增加记录时判断如果记录已超过1024条,会移除链表的第一个元素,从而达到FIFO缓存效果
LruCache最近最少使用缓存
存了两个map,用LinkedHashMap实现最近最少策略
class LinkedHashMap<K,V>
extends HashMap<K,V>
implements Map<K,V>
void afterNodeAccess(Node<K,V> e) { // move node to last
LinkedHashMap.Entry<K,V> last;
if (accessOrder && (last = tail) != e) {
LinkedHashMap.Entry<K,V> p =
(LinkedHashMap.Entry<K,V>)e, b = p.before, a = p.after;
p.after = null;
if (b == null)
head = a;
else
b.after = a;
if (a != null)
a.before = b;
else
last = b;
if (last == null)
head = p;
else {
p.before = last;
last.after = p;
}
tail = p;
++modCount;
}
}
最近访问和最近插入都放到链表的后面,链表最前面的就是最长时间没有访问的节点
CacheKey缓存key
MyBatis 对于其 Key 的生成采取规则为:[mappedStementId + offset + limit + SQL + queryParams + environment]生成一个哈希码
https://my.oschina.net/lixin91/blog/620068