Springboot + Mybatis + Ehcache

Recently doing a project, to address the problem of poor concurrency, use the secondary cache Mybatis

However, in the case of a joint multi-table queries, Mybatis secondary cache is problematic reading of dirty data

Two days is to think of ways to solve this problem of dirty read data

Considering the simplicity, performance, compatibility, scalability, I chose springboot comes Ehcache framework to integrate solve this problem

I was first exposed to Ehcache this framework, it is also considered from scratch

 

According Mybatis secondary cache features, my idea is

  Whenever additions and deletions to a particular table, clears the table for all to refresh its associated cache

(Size herein be divided into two directions:

In the method of additions and deletions, do the relevant association to clear the cache operation - this is more of the details that make the modification may be more

On the whole class where the CRUD method, do the relevant associations Clear Cache operation - I need to do to modify this relatively small point, but that inside each method (including some insignificant) will trigger the Clear Cache operations

Have their pros and cons)

 

Along the way is not smooth,

At first I try myself to see if I can find a better way, but it is new to all of a sudden themselves also unlikely

But there is a plug-in to see the problem solved Mybatis secondary cache - LuanLouis on GitHub mybatis-enhanced-cache 

But this project was written five years ago, but a recent update is only 2 years ago, and now the development of technology so fast, it is time to see the feeling may not work

Download behind himself down, mvn install found unsuccessful, the lack jar package ojdbc14, online say ojdbc14.jar for a fee, so the central repository is not conducting this package

Read some information that, Oracle installation directory there, but the company this did not install Oracle; there is information that can be used ojdbc6 or other ojdbc in place, I downloaded ojdbc6 and ojdbc8 have tried it, are able to solve that ojdbc jar packages question,

jar mybatis-enhanced-cache plugin also successfully imported maven repository and projects, but after the import, use the time but found all kinds of wrong report, I felt cold,

Has always been to simply and quickly, just want to use someone else's plug-in, so I can not myself went to modify the plugin to use, and can not necessarily be changed! So use this plug-in program can only give up

 

Later, I found a lot of information online, but also by the example of many steps do

Like all kinds of notes, what a variety of configurations, do not know the notes and mybatis conflict, or I write invalid

(Like a lot of information online say, mybatis secondary cache is not recommended, as to why I would like to use, you can see in front of me an article,

So I encountered such problems should be few, will not find much information, can only rely on their own)

Like a lot of possibilities, tried again and again, want to try to do simply, straightforward XML configuration annotated, can this just fine.

But the reality is cruel, I do not know how many times the attempt, successful or not, do anything, but finally only to give up the program

 

There is no way you can only select a final plan - to write their own code to handle the business logic layer do

This is the rookie of the ignorant, Burongyia

Fortunately insist on trying, finally found a solution

Configuration ehcache.xml

. 1  <? XML Version = "1.0" encoding = "UTF-. 8" ?> 
2  <-! <Ehcache> -> 
. 3  < Ehcache xmlns: the xsi = "http://www.w3.org/2001/XMLSchema -instance " 
4           xsi: noNamespaceSchemaLocation =" ehcache.xsd " > 
5  
6      <-! 
7          disk storage: the objects in the cache temporarily not in use, transfer to the hard disk, similar to the Windows virtual memory system
 8          path: Specifies the hard disk path storage object
 . 9          path may be configured with a directory:
 10              user.home (user home)
 . 11              user.dir (user's current working directory)
 12 is              the java.io.tmpdir (default temporary directory)
 13             ehcache.disk.store.dir (ehcache configuration directory)
 14              absolute path (eg: D: \\ Ehcache)
 15          See Path Method: String = tmpDir the System.getProperty ( "the java.io.tmpdir");
 16       -> 
. 17      < diskStore path = "the java.io.tmpdir"  /> 
18 is  
. 19      <-! configuration provider 1, peerDiscovery, provided by way of two ways: automatic discovery (automatic), manual configuration (manual) 2, rmiUrls address provider manual mode, with a plurality of words separated by | -> 
20 is      ! <- <cacheManagerPeerProviderFactory
 21 is          class = "net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
 22 is          Properties = "peerDiscovery = manual, rmiUrls = //127.0.0.1:40002/userCache "/>-->
23     <cacheManagerPeerProviderFactory
24         class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
25         properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, multicastGroupPort=4446,timeToLive=255"/>
26     <!-- <cacheManagerPeerProviderFactory
27         class="org.ehcache.distribution.RMICacheManagerPeerProviderFactory"
28         properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, multicastGroupPort=4446,timeToLive=255"/> -->
29 
30     <!- configure the listener 1, hostName host address 2, port port 3, socketTimeoutMillis socket timeout sub-module, the default is 2000ms-> 
31 is      <-! <CacheManagerPeerListenerFactory
 32          class = "net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
 33 is          Properties = "of hostName = 127.0.0.1, Port = 40001, socketTimeoutMillis = 2000" /> -> 
34 is      < cacheManagerPeerListenerFactory
 35           class = "net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory" /> 
36  
37 [  
38 is      <-! 
39          defaultCache: default cache configuration, if not otherwise stated, all of the objects treated according to this configuration item
 40          maxElementsInMemory: set the upper limit of the cache, the number of recording object store up to
 41 is          Eternal: whether the representative objects never expires (specified below is true for the two configurations for an indefinite 0)
 42         timeToIdleSeconds: maximum idle time / s
 43 is          timeToLiveSeconds: maximum survival time / s
 44 is          overflowToDisk: whether to allow objects to be written to the disk
 45          Description: The following configuration from the cache to establish 600 seconds (10 minutes) effective.
46          within an effective 600 seconds (10 minutes), if the continuous 120 seconds (2 minutes) does not access the cache, the cache is invalidated.
47          even if there is access, it will only survive 600 seconds.
48       -> 
49      < defaultCache maxElementsInMemory = "10000" Eternal = "to false" 
50                    timeToIdleSeconds = "600" timeToLiveSeconds = "600" overflowToDisk = "to true"  /> 
51 is  
52 is      < Cache name="*.*.*.*.dao.WarnMapper" maxElementsInMemory="10000" eternal="false"
53                   timeToIdleSeconds="120" timeToLiveSeconds="300" overflowToDisk="true" />
54 
55     <cache name="*.*.*.*.dao.ProjectMapper" maxElementsInMemory="10000" eternal="false" 56  timeToIdleSeconds="120" timeToLiveSeconds="300" overflowToDisk="true" /> 57 <!-- <cache:annotation-driven /> --> 58 </ehcache>

Configuration application.properties 

1 # Ehcache缓存
2 spring.cache.type=ehcache
3 spring.cache.ehcache.config=classpath:/ehcache.xml

Yewuluojiceng Code

1 CacheManager CacheManager = CacheManager.newInstance ( "src / main / resources / ehcache.xml" );  
2 Cache cache cacheManager.getCache = ( "* * * * dao.WarnMapper...." ); 3 cache.removeAll ();

 

 

Common learning and common progress, if any supplement, please point out, thank you!

Guess you like

Origin www.cnblogs.com/dengguangxue/p/11276791.html