zl程序教程

您现在的位置是:首页 >  后端

当前栏目

MyBatis源码第12章之缓存模块

2023-09-14 09:14:05 时间

一、前言

    数据库的资源是珍贵的,我们要充分利用以及减少对数据库的直接访问,提高系统的性能。缓存就是用来解决一些系统性能瓶颈的,充分发挥缓存的功能,使得系统高可用。前面介绍了装饰器模式与JDK动态代理,其实并不是与之没有联系的,都是为了为后面埋下伏笔。MyBatis的缓存模块就是使用了装饰器模式去实现相关功能的,不过它类图就简单些,没有抽象装饰器,都是具体装饰器。那么MyBatis到底是怎么实现缓存功能的呢,这给我们的日常开发带来哪些启发?

二、缓存模块

1、接口层Cache

public interface Cache {

  /**
   * @return The identifier of this cache
   */
  String getId();

  /**
   * @param key
   *          Can be any object but usually it is a {@link CacheKey}
   * @param value
   *          The result of a select.
   */
  void putObject(Object key, Object value);

  /**
   * @param key
   *          The key
   * @return The object stored in the cache.
   */
  Object getObject(Object key);

  /**
   * As of 3.3.0 this method is only called during a rollback
   * for any previous value that was missing in the cache.
   * This lets any blocking cach