三、装饰器在缓存模块的使用
==============
MyBatis 缓存模块是一个经典的使用装饰器实现的模块,类图如下:

-
Cache:Cache 接口是缓存模块的核 心接口,定义了缓存的基本操作;
-
PerpetualCache:在缓存模块中扮演 ConcreteComponent 角色,使用 HashMap 来实现 cache 的相关操作;
-
BlockingCache:阻塞版本的缓存装 饰器,保证只有一个线程到数据库去查 找指定的 key 对应的数据;BlockingCache 是阻塞版本的缓存装饰器,这个装饰器通过 ConcurrentHashMap 对锁的粒度 进行了控制,提高加锁后系统代码运行的效率(注:缓存雪崩的问题可以使用细粒度锁的方 式提升锁性能)
源码分析:
/**
-
Simple blocking decorator
-
Simple and inefficient version of EhCache’s BlockingCache decorator.
-
It sets a lock over a cache key when the element is not found in cache.
-
This way, other threads will wait until this element is filled instead of hitting the database.
-
阻塞版本的缓存装饰器,保证只

最低0.47元/天 解锁文章
861

被折叠的 条评论
为什么被折叠?



