mybatis-缓存(Mybatis cache)

1 缓存

1.1 什么是缓存

  • 存在内存中的临时数据
  • 将用户经常查询的数据放在缓存(内存)中,用户去查询数据就不用从磁盘上(关系型数据库数据文件)查询,从缓存中查询,从而提高查询效率,解决了高并发系统的性能问题

1.2 为什么使用缓存

  • 减少和数据库的交互次数,减少系统开销,提高系统效率

1.3 什么样的数据能使用缓存

  • 经常查询并且不经常改变的数据

2 mybatis缓存

2.1 mybatis包含一个非常强大的查询缓存特性,它可以非常方便地定制和配置缓存。缓存可以极大的提升查询效率

2.2 mybatis系统中默认定义了两级缓存:一级缓存和二级缓存

  • 默认情况下,只有一级缓存开启.(SqlSession级别的缓存,也称为本地缓存)
  • 二级缓存需要手动开启和配置,他是基于namespace级别的缓存
  • 为了提高扩展性,mybatis定义了缓存接口Cache。我们可以通过实现Cache接口来自定义二级缓存

3 一级缓存

3.1 介绍

  • 一级缓存也叫本地缓存
  • 一级缓存是SqlSession级别的缓存,是一直开启的,我们关闭不了它
  • 与数据库同一次会话期间查询到的数据会放在一级缓存中,以后如果需要获取相同的数据,直接从缓存中拿,没必须再去查询数据库

3.2 一级缓存失效的四种情况

  • sqlSession不同
  • 查询条件不同
  • 多次查询之间对数据库进行了更新操作
  • 手动清除一级缓存

4 二级缓存

4.1 介绍

  • 二级缓存也叫全局缓存,一级缓存作用域太低了,所以诞生了二级缓存
  • 基于namespace级别的缓存,一个名称空间,对应一个二级缓存;

4.2 工作机制

  • 一个会话查询一条数据,这个数据就会被放在当前会话的一级缓存中
  • 如果当前会话关闭了,这个会话对应的一级缓存就没了;但是我们想要的是,会话关闭了,一级缓存中的数据被保存到二级缓存中
  • 新的会话查询信息,就可以从二级缓存中获取内容
  • 不同的mapper查出的数据会放在自己对应的缓存(map)中

5 ehcache

————————

1 cache

1.1 what is caching

  • Temporary data in memory
  • The data frequently queried by users is placed in the cache (memory), and users do not need to query from the disk (relational database data file) but from the cache to query the data, so as to improve the query efficiency and solve the performance problem of high concurrency system

1.2 why cache

  • Reduce the number of interactions with the database, reduce system overhead and improve system efficiency

1.3 what data can be cached

  • Frequently queried and infrequently changed data

2 mybatis缓存

2.1 mybatis includes a very powerful query caching feature, which can easily customize and configure the cache. Caching can greatly improve query efficiency

2.2 two levels of cache are defined by default in mybatis system: Level 1 cache and level 2 cache

  • By default, only L1 cache is enabled (sqlsession level cache, also known as local cache)
  • The L2 cache needs to be manually enabled and configured. It is based on the namespace level cache
  • In order to improve scalability, mybatis defines the cache interface cache. We can customize the L2 cache by implementing the cache interface

3 L1 cache

3.1 introduction

  • L1 cache is also called local cache
  • The L1 cache is a sqlsession level cache. It is always on. We can’t close it
  • The data queried during the same session with the database will be placed in the first level cache. In the future, if you need to obtain the same data, you can get it directly from the cache. There is no need to query the database

3.2 four situations of L1 cache invalidation

  • sqlSession不同
  • Different query conditions
  • The database was updated between multiple queries
  • Manually clear L1 cache

4 L2 cache

4.1 introduction

  • L2 cache is also called global cache. The scope of L1 cache is too low, so L2 cache was born
  • Based on the namespace level cache, a namespace corresponds to a L2 cache;

4.2 working mechanism

  • When a session queries a piece of data, the data will be placed in the first level cache of the current session
  • If the current session is closed, the L1 cache corresponding to the session is gone; But what we want is that the session is closed and the data in the L1 cache is saved to the L2 cache
  • With the new session query information, you can get the content from the L2 cache
  • The data found by different mappers will be placed in their corresponding cache (map)

5 ehcache