1. 程式人生 > >springmvc快取和mybatis快取

springmvc快取和mybatis快取

首先要有一個搭建好的ssm框架,筆者使用的是基於maven搭建的ssm框架。

加入springmvc快取:

1. 匯入相關依賴包:

複製程式碼
 1 <dependency>
 2    <groupId>org.springframework</groupId>
 3    <artifactId>spring-context-support</artifactId>
 4    <version>4.3.7.RELEASE</version>
 5 </dependency>
 6 <dependency>
 7     <groupId>net.sf.ehcache</groupId>
 8     <artifactId>ehcache</artifactId>
 9     <version>1.6.2</version>
10 </dependency>
複製程式碼

2. 加入ehcache的配置檔案ehcache.xml:(注意檔案的位置!)

複製程式碼
 1 <?xml version="1.0" encoding="UTF-8"?>  
 2 <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 3     xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"  
 4     updateCheck="false">  
 5       
 6     <diskStore path="java.io.tmpdir" />  
 7       
 8     <defaultCache eternal="false"   
 9         maxElementsInMemory="1000"  
10         overflowToDisk="false"   
11         diskPersistent="false"   
12         timeToIdleSeconds="0"  
13         timeToLiveSeconds="600"   
14         memoryStoreEvictionPolicy="LFU" />  
15   
16     <cache name="myCache"   
17         eternal="false"   
18         maxElementsInMemory="500"  
19         overflowToDisk="false"   
20         diskPersistent="false"   
21         timeToIdleSeconds="0"  
22         timeToLiveSeconds="300"   
23         memoryStoreEvictionPolicy="LFU" />  
24   
25 </ehcache>
複製程式碼

3. 在springmvc的配置檔案中開啟快取功能:(注意引入名稱空間,不然會報錯!)

4. 開啟快取註解

複製程式碼
1 <!-- 啟用快取註解功能 -->
2 <cache:annotation-driven cache-manager="cacheManager" />
3 <!-- spring提供的基於的ehcache實現的快取管理器 -->
4 <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
5     <property name="configLocation" value="classpath:ehcache.xml" />
6 </bean>
7 <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
8     <property name="cacheManager" ref="cacheManagerFactory" />
9 </bean>
複製程式碼

5. 將快取註解寫在了service層:(控制層程式碼省略,就是呼叫一下service層)

複製程式碼
1 @Cacheable(value = "myCache", key = "'UserService.findById'")
2 @Override
3 public List<User> findById(int id) {
4     System.out.println("*************************************************我是快取方法*************************************************");
5     List<User> list = userMapper.findById(id);
6     return list;
7 }
複製程式碼

value對應的是ehcache.xml檔案裡的name,相當於一個快取空間。key最好在全域性是唯一的,這裡使用的類名+方法名,因為後面可能會根據這個值對特定的快取進行清理。

6. 測試:(jsp程式碼省略,就是呼叫一下controller層,然後controller層呼叫上面加過快取註解的service層方法)

第一次呼叫,進入該方法進行了相關程式:

第二次呼叫,沒有進入該方法,直接從快取中輸出了結果:

在更新的時候,需要將該快取清除掉:

1 @CacheEvict(value = "myCache", key = "'UserService.findById'")
2 @Override
3 public void removeCache() {
4     System.out.println("*************************************************移除了快取*************************************************");
5 }

再次查詢的時候就會重新進入該方法進行查詢。

加入MyBatis快取:

mybatis的一級快取是預設開啟的,二級快取有一個最簡單的開啟方法,在每個Mapper.xml檔案里加入一個<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true" />即可(注意要放到mapper標籤裡)

測試:(此處測試時先遮蔽掉了springmvc的快取)

1 @Override
2 public List<User> findById(int id) {
3     System.out.println("*************************************************我是快取方法*************************************************");
4     List<User> list = userMapper.findById(id);
5     return list;
6 }

第一次訪問,對資料庫進行了查詢:

第二次訪問,沒有對資料庫進行查詢,直接使用了快取:

原始碼下載:http://files.cnblogs.com/files/ImNemo/ssm-cache.rar

 

首先要有一個搭建好的ssm框架,筆者使用的是基於maven搭建的ssm框架。

加入springmvc快取:

1. 匯入相關依賴包:

複製程式碼
 1 <dependency>
 2    <groupId>org.springframework</groupId>
 3    <artifactId>spring-context-support</artifactId>
 4    <version>4.3.7.RELEASE</version>
 5 </dependency>
 6 <dependency>
 7     <groupId>net.sf.ehcache</groupId>
 8     <artifactId>ehcache</artifactId>
 9     <version>1.6.2</version>
10 </dependency>
複製程式碼

2. 加入ehcache的配置檔案ehcache.xml:(注意檔案的位置!)

複製程式碼
 1 <?xml version="1.0" encoding="UTF-8"?>  
 2 <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 3     xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"  
 4     updateCheck="false">  
 5       
 6     <diskStore path="java.io.tmpdir" />  
 7       
 8     <defaultCache eternal="false"   
 9         maxElementsInMemory="1000"  
10         overflowToDisk="false"   
11         diskPersistent="false"   
12         timeToIdleSeconds="0"  
13         timeToLiveSeconds="600"   
14         memoryStoreEvictionPolicy="LFU" />  
15   
16     <cache name="myCache"   
17         eternal="false"   
18         maxElementsInMemory="500"  
19         overflowToDisk="false"   
20         diskPersistent="false"   
21         timeToIdleSeconds="0"  
22         timeToLiveSeconds="300"   
23         memoryStoreEvictionPolicy="LFU" />  
24   
25 </ehcache>
複製程式碼

3. 在springmvc的配置檔案中開啟快取功能:(注意引入名稱空間,不然會報錯!)

4. 開啟快取註解

複製程式碼
1 <!-- 啟用快取註解功能 -->
2 <cache:annotation-driven cache-manager="cacheManager" />
3 <!-- spring提供的基於的ehcache實現的快取管理器 -->
4 <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
5     <property name="configLocation" value="classpath:ehcache.xml" />
6 </bean>
7 <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
8     <property name="cacheManager" ref="cacheManagerFactory" />
9 </bean>
複製程式碼

5. 將快取註解寫在了service層:(控制層程式碼省略,就是呼叫一下service層)

複製程式碼
1 @Cacheable(value = "myCache", key = "'UserService.findById'")
2 @Override
3 public List<User> findById(int id) {
4     System.out.println("*************************************************我是快取方法*************************************************");
5     List<User> list = userMapper.findById(id);
6     return list;
7 }
複製程式碼

value對應的是ehcache.xml檔案裡的name,相當於一個快取空間。key最好在全域性是唯一的,這裡使用的類名+方法名,因為後面可能會根據這個值對特定的快取進行清理。

6. 測試:(jsp程式碼省略,就是呼叫一下controller層,然後controller層呼叫上面加過快取註解的service層方法)

第一次呼叫,進入該方法進行了相關程式:

第二次呼叫,沒有進入該方法,直接從快取中輸出了結果:

在更新的時候,需要將該快取清除掉:

1 @CacheEvict(value = "myCache", key = "'UserService.findById'")
2 @Override
3 public void removeCache() {
4     System.out.println("*************************************************移除了快取*************************************************");
5 }

再次查詢的時候就會重新進入該方法進行查詢。

加入MyBatis快取:

mybatis的一級快取是預設開啟的,二級快取有一個最簡單的開啟方法,在每個Mapper.xml檔案里加入一個<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true" />即可(注意要放到mapper標籤裡)

測試:(此處測試時先遮蔽掉了springmvc的快取)

1 @Override
2 public List<User> findById(int id) {
3     System.out.println("*************************************************我是快取方法*************************************************");
4     List<User> list = userMapper.findById(id);
5     return list;
6 }

第一次訪問,對資料庫進行了查詢:

第二次訪問,沒有對資料庫進行查詢,直接使用了快取:

原始碼下載:http://files.cnblogs.com/files/ImNemo/ssm-cache.rar