├── .classpath
├── .gitattributes
├── .gitignore
├── .project
├── .settings
├── org.eclipse.core.resources.prefs
├── org.eclipse.jdt.core.prefs
└── org.eclipse.m2e.core.prefs
├── README.md
├── example.log
├── pom.xml
└── src
├── main
└── java
│ └── org
│ └── luanlouis
│ └── mybatis
│ └── plugin
│ └── cache
│ ├── CacheKeysPool.java
│ ├── EnhancedCachingExecutor.java
│ ├── EnhancedCachingManager.java
│ ├── dependency.dtd
│ └── impl
│ └── EnhancedCachingManagerImpl.java
└── test
└── java
├── com
└── louis
│ └── mybatis
│ ├── domain
│ ├── DepartmentsMapper.xml
│ └── EmployeesMapper.xml
│ ├── model
│ ├── Department.java
│ └── Employee.java
│ └── test
│ └── SelectDemo3.java
├── dependencys.xml
├── log4j.properties
└── mybatisConfig.xml
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
4 | # Custom for Visual Studio
5 | *.cs diff=csharp
6 | *.sln merge=union
7 | *.csproj merge=union
8 | *.vbproj merge=union
9 | *.fsproj merge=union
10 | *.dbproj merge=union
11 |
12 | # Standard to msysgit
13 | *.doc diff=astextplain
14 | *.DOC diff=astextplain
15 | *.docx diff=astextplain
16 | *.DOCX diff=astextplain
17 | *.dot diff=astextplain
18 | *.DOT diff=astextplain
19 | *.pdf diff=astextplain
20 | *.PDF diff=astextplain
21 | *.rtf diff=astextplain
22 | *.RTF diff=astextplain
23 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Windows image file caches
2 | Thumbs.db
3 | ehthumbs.db
4 |
5 | # Folder config file
6 | Desktop.ini
7 |
8 | # Recycle Bin used on file shares
9 | $RECYCLE.BIN/
10 |
11 | # Windows Installer files
12 | *.cab
13 | *.msi
14 | *.msm
15 | *.msp
16 |
17 | # =========================
18 | # Operating System Files
19 | # =========================
20 |
21 | # OSX
22 | # =========================
23 |
24 | .DS_Store
25 | .AppleDouble
26 | .LSOverride
27 |
28 | # Icon must end with two \r
29 | Icon
30 |
31 | # Thumbnails
32 | ._*
33 |
34 | # Files that might appear on external disk
35 | .Spotlight-V100
36 | .Trashes
37 |
38 | # Directories potentially created on remote AFP share
39 | .AppleDB
40 | .AppleDesktop
41 | Network Trash Folder
42 | Temporary Items
43 | .apdisk
44 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | mybatis-enhanced-cache
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 | org.eclipse.m2e.core.maven2Builder
15 |
16 |
17 |
18 |
19 |
20 | org.eclipse.jdt.core.javanature
21 | org.eclipse.m2e.core.maven2Nature
22 |
23 |
24 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.core.resources.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | encoding//src/main/java=UTF-8
3 | encoding//src/test/java=UTF-8
4 | encoding/=UTF-8
5 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.5
6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
12 | org.eclipse.jdt.core.compiler.source=1.5
13 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.m2e.core.prefs:
--------------------------------------------------------------------------------
1 | activeProfiles=
2 | eclipse.preferences.version=1
3 | resolveWorkspaceProjects=true
4 | version=1
5 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | mybatis-enhanced-cache
2 | ======================
3 |
4 | 简 介
5 | ----
6 | MyBatis Enhanced Cache, Control your Caches precisely!
7 | 该插件主要是为了弥补MyBatis二级缓存控制上的不足,提高二级缓存Cache和数据库数据的同步性和一致性,处理各个Cache之间的关联关系。
8 | 该插件可以精确地管理MyBatis的二级缓存,实现对MyBatis二级缓存细粒度的控制。
9 | 当执行过对数据库表的更新操作(update、delete、insert)时,可以指定清除由特定的StatementId表示的查询语句产生的缓存。
10 |
11 | 应 用 场 景:
12 | -----
13 | 当前的MyBaits对于缓存的比较粗糙,一般为一个Mapper配置一个Cache缓存,或者多个Mapper共用一个缓存。
14 | 而对缓存的维护都是独立的,缓存之间不会相互影响,指定的Mapper中的语句只会影响到该Mapper对应的Cache缓存。
15 | 有时候希望当执行某些更新操作时,能够刷新或者清空特定的查询语句产生的缓存,以避免数据不一致的情况。
16 | ###举例
17 | ***
18 | 现有AMapper.xml 中定义了对数据库表`ATable`的CRUD操作,BMapper定义了对数据库表`BTable`的CRUD操作;
19 | 假设MyBatis的二级缓存开启,并且AMapper中使用了二级缓存,AMapper对应的二级缓存为`ACache`;
20 | 除此之外,AMapper中还定义了一个跟BTable有关的查询语句,类似如下所述:
21 | ```xml
22 |
25 | ```
26 |
27 | 1. 执行了`selectATableWithJoin`操作,该查询的缓存会被放置到对应的二级缓存`ACache`中,
28 | 当再次执行相同的查询,则直接从二级缓存`ACache`中;
29 | 2. 如果某个时候,BMapper中执行了对BTable的update操作(update 、delete、insert),BTable 的数据已经更新,
30 | 3. 再次执行`selectATableWithJoin`操作,该查询的结果直接从二级缓存`ACache`中取,这时候就造成了`**数据不一致**`的情况
31 |
32 | **针对上述的问题,需要解决**:
33 | **当BMapper执行对BTable的update操作时,指定刷新 `ACache`中的 `selectATableWithJoin`语句产生的缓存**
34 |
35 | 该插件正是解决上述的这一问题!
36 |
37 |
38 | 使用方法:
39 | ----
40 | 使用此插件非常简单:
41 | 1. 在mybatisConfig.xml 文件中定义plugin节点如下:
42 | ```xml
43 |
44 |
45 |
46 |
47 |
48 |
49 | ```
50 | 2. dependencys.xml配置文件,配置StatemntId依赖关系
51 | ```xml
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 | ```
61 | * \节点配置更新语句和查询语句的依赖关系,如果id表示的更新语句执行了,会清空由配置的id表示的查询语句生成的缓存。
62 | * \节点表示当父节点 id表示的更新语句执行后,应该清除此语句所产生的缓存
63 |
--------------------------------------------------------------------------------
/example.log:
--------------------------------------------------------------------------------
1 | INFO main com.louis.mybatis.test.SelectDemo3 - ------------Cache Status------------
2 | INFO main com.louis.mybatis.test.SelectDemo3 - EmployeesMapper:1
3 | INFO main com.louis.mybatis.test.SelectDemo3 - DepartmentsMapper:0
4 | INFO main com.louis.mybatis.test.SelectDemo3 - com.louis.mybatis.dao.DepartmentsMapper:0
5 | INFO main com.louis.mybatis.test.SelectDemo3 - com.louis.mybatis.dao.EmployeesMapper:1
6 | INFO main com.louis.mybatis.test.SelectDemo3 - ------------------------------------
7 | INFO main com.louis.mybatis.test.SelectDemo3 - ------------Cache Status------------
8 | INFO main com.louis.mybatis.test.SelectDemo3 - EmployeesMapper:2
9 | INFO main com.louis.mybatis.test.SelectDemo3 - DepartmentsMapper:0
10 | INFO main com.louis.mybatis.test.SelectDemo3 - com.louis.mybatis.dao.DepartmentsMapper:0
11 | INFO main com.louis.mybatis.test.SelectDemo3 - com.louis.mybatis.dao.EmployeesMapper:2
12 | INFO main com.louis.mybatis.test.SelectDemo3 - ------------------------------------
13 | INFO main com.louis.mybatis.test.SelectDemo3 - ------------Cache Status------------
14 | INFO main com.louis.mybatis.test.SelectDemo3 - EmployeesMapper:3
15 | INFO main com.louis.mybatis.test.SelectDemo3 - DepartmentsMapper:0
16 | INFO main com.louis.mybatis.test.SelectDemo3 - com.louis.mybatis.dao.DepartmentsMapper:0
17 | INFO main com.louis.mybatis.test.SelectDemo3 - com.louis.mybatis.dao.EmployeesMapper:3
18 | INFO main com.louis.mybatis.test.SelectDemo3 - ------------------------------------
19 | INFO main com.louis.mybatis.test.SelectDemo3 - ------------Cache Status------------
20 | INFO main com.louis.mybatis.test.SelectDemo3 - EmployeesMapper:4
21 | INFO main com.louis.mybatis.test.SelectDemo3 - DepartmentsMapper:0
22 | INFO main com.louis.mybatis.test.SelectDemo3 - com.louis.mybatis.dao.DepartmentsMapper:0
23 | INFO main com.louis.mybatis.test.SelectDemo3 - com.louis.mybatis.dao.EmployeesMapper:4
24 | INFO main com.louis.mybatis.test.SelectDemo3 - ------------------------------------
25 | INFO main com.louis.mybatis.test.SelectDemo3 - ------------Cache Status------------
26 | INFO main com.louis.mybatis.test.SelectDemo3 - EmployeesMapper:0
27 | INFO main com.louis.mybatis.test.SelectDemo3 - DepartmentsMapper:1
28 | INFO main com.louis.mybatis.test.SelectDemo3 - com.louis.mybatis.dao.DepartmentsMapper:1
29 | INFO main com.louis.mybatis.test.SelectDemo3 - com.louis.mybatis.dao.EmployeesMapper:0
30 | INFO main com.louis.mybatis.test.SelectDemo3 - ------------------------------------
31 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 | org.luanlouis.mybatis.plugin
6 | mybatis-enhanced-cache
7 | 0.0.1-SNAPSHOT
8 | jar
9 |
10 | cache-plugin
11 | http://maven.apache.org
12 |
13 |
14 | UTF-8
15 |
16 |
17 |
18 |
19 | org.mybatis
20 | mybatis
21 | 3.2.7
22 |
23 |
24 |
25 | log4j
26 | log4j
27 | 1.2.17
28 | true
29 |
30 |
31 | org.apache.logging.log4j
32 | log4j-core
33 | 2.0-rc1
34 | true
35 |
36 |
37 | commons-logging
38 | commons-logging
39 | 1.1.1
40 | true
41 | test
42 |
43 |
44 | com.oracle
45 | ojdbc14
46 | 10.2.0.4.0
47 | test
48 |
49 |
50 |
51 |
--------------------------------------------------------------------------------
/src/main/java/org/luanlouis/mybatis/plugin/cache/CacheKeysPool.java:
--------------------------------------------------------------------------------
1 | package org.luanlouis.mybatis.plugin.cache;
2 |
3 | import java.util.HashSet;
4 | import java.util.Map;
5 | import java.util.Map.Entry;
6 | import java.util.Set;
7 | import java.util.concurrent.ConcurrentHashMap;
8 |
9 |
10 |
11 | /**
12 | * CacheKey缓冲池
13 | * 记录MyBatis的每次查询的查询所产生的CacheKey
14 | * 对于由某个特定的statementId 所执行的查询,其查询所产生的CacheKey存放到一个Set集合中,
15 | * 以>的形式存储到CacheKey缓冲池中
16 | * @author louluan
17 | * @date 2014-12-5
18 | */
19 | public class CacheKeysPool
20 | {
21 | private Map> pool = new ConcurrentHashMap>();
22 |
23 | public Set