springboot+mybatis+ehcache實現緩存數據


一、springboot緩存簡介

在 Spring Boot中,通過@EnableCaching注解自動化配置合適的緩存管理器(CacheManager),Spring Boot根據下面的順序去偵測緩存提供者:
* Generic
* JCache (JSR-107)
* EhCache 2.x
* Hazelcast
* Infinispan
* Redis
* Guava
* Simple

關於 Spring Boot 的緩存機制:
高速緩存抽象不提供實際存儲,並且依賴於由org.springframework.cache.Cache和org.springframework.cache.CacheManager接口實現的抽象。 Spring Boot根據實現自動配置合適的CacheManager,只要緩存支持通過@EnableCaching注釋啟用即可。

二、實例

1、application.properyies

server.port=8850

#cache 多個用逗號分開
spring.cache.cache-names=userCache
spring.cache.jcache.config=classpath:ehcache.xml


# datasource
spring.datasource.name=ehcahcetest
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3312/ehcahcetest
spring.datasource.username=root
spring.datasource.password=123456

# mybatis
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
mybatis.config-location=classpath:mybatis/mybatis-config.xml
#mybatis.type-aliases-package=

2、springboot啟動類

1 @SpringBootApplication
2 @ComponentScan(basePackages="com.ehcache")//掃描組件
3 @EnableCaching
4 public class EhcacheTestApplication {
5 
6     public static void main(String[] args) {
7         SpringApplication.run(EhcacheTestApplication.class, args);
8     }
9 }

3、加了緩存注解的service

 1 @Service
 2 public class UserService {
 3     
 4     @Autowired
 5     private UserDao userDao;
 6 
 7     @CacheEvict(key="'user_'+#uid", value="userCache")
 8     public void del(String uid) {
 9         // TODO Auto-generated method stub
10         userDao.del(uid);
11     }
12 
13     @CachePut(key="'user_'+#user.uid", value="userCache")
14     public void update(User user) {
15         userDao.update(user);
16     }
17     
18     @Cacheable(key="'user_'+#uid",value="userCache")
19     public User getUserById(String uid){
20         System.err.println("緩存里沒有"+uid+",所以這邊沒有走緩存,從數據庫拿數據");
21         return userDao.findById(uid);
22         
23     }
24 
25     @CacheEvict(key="'user'",value="userCache")
26     public String save(User user) {
27         // TODO Auto-generated method stub
28         return userDao.save(user);
29     }
30 
31     
32 }

 4、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     <diskStore path="java.io.tmpdir" />
 5 
 6     <!-- 配置提供者 1、peerDiscovery,提供者方式,有兩種方式:自動發現(automatic)、手動配置(manual) 2、rmiUrls,手動方式時提供者的地址,多個的話用|隔開 -->
 7     <cacheManagerPeerProviderFactory
 8         class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
 9         properties="peerDiscovery=manual,rmiUrls=//127.0.0.1:40002/userCache" />
10     <!-- <cacheManagerPeerProviderFactory
11         class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
12         properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, multicastGroupPort=4446,timeToLive=255"/>
13  -->
14     <!-- 配置監聽器 1、hostName 主機地址 2、port 端口 3、socketTimeoutMillis socket子模塊的超時時間,默認是2000ms -->
15     <cacheManagerPeerListenerFactory
16         class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
17         properties="hostName=127.0.0.1, port=40001, socketTimeoutMillis=2000" />
18     <!-- <cacheManagerPeerListenerFactory
19          class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"/> -->
20     
21 
22     <defaultCache eternal="false" maxElementsInMemory="1000"
23         overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
24         timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" />
25 
26     <cache 
27         name="userCache" 
28         maxElementsInMemory="1000"
29         eternal="false"
30         timeToIdleSeconds="300"
31         timeToLiveSeconds="300"
32         overflowToDisk="false"
33         memoryStoreEvictionPolicy="LRU">
34         
35         
36         <!-- 配置緩存事件監聽器 replicateAsynchronously 操作是否異步,默認值為true. replicatePuts 添加操作是否同步到集群內的其他緩存,默認為true. 
37             replicateUpdates 更新操作是否同步到集群內的其他緩存,默認為true. replicateUpdatesViaCopy 更新之后的對象是否復制到集群中的其他緩存(true); 
38             replicateRemovals 刪除操作是否同步到集群內的其他緩存,默認為true. -->
39         <cacheEventListenerFactory 
40             class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
41             properties="
42                     replicateAsynchronously=true,
43                     replicatePuts=true,
44                     replicateUpdates=true,
45                     replicateUpdatesViaCopy=true,
46                     replicateRemovals=true " />
47                     
48                     
49          <!-- 初始化緩存,以及自動設置 -->
50         <bootstrapCacheLoaderFactory
51             class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"
52             properties="bootstrapAsynchronously=true" />
53 
54     </cache>
55 
56 </ehcache>

5、OperationController.java 測試類

  1 package com.ehcache.controller;
  2 
  3 import java.util.ArrayList;
  4 import java.util.HashMap;
  5 import java.util.List;
  6 import java.util.Map;
  7 
  8 import javax.servlet.http.HttpServletRequest;
  9 
 10 import org.springframework.beans.factory.annotation.Autowired;
 11 import org.springframework.cache.annotation.CachePut;
 12 import org.springframework.cache.annotation.Cacheable;
 13 import org.springframework.web.bind.annotation.RequestMapping;
 14 import org.springframework.web.bind.annotation.RequestMethod;
 15 import org.springframework.web.bind.annotation.RequestParam;
 16 import org.springframework.web.bind.annotation.ResponseBody;
 17 import org.springframework.web.bind.annotation.RestController;
 18 
 19 import com.ehcache.entity.User;
 20 import com.ehcache.factory.CacheManagerFactory;
 21 import com.ehcache.factory.UserFactory;
 22 import com.ehcache.service.UserService;
 23 import com.google.gson.Gson;
 24 
 25 import net.sf.ehcache.Element;
 26 
 27 
 28 @RestController
 29 @RequestMapping("/o")
 30 public class OperationController {
 31     
 32     @Autowired
 33     private UserService userService;
 34     
 35     Gson gson = new Gson();
 36     
 37     CacheManagerFactory cmf = CacheManagerFactory.getInstance();
 38     
 39     @RequestMapping(value = "/test", method = RequestMethod.GET)
 40     public String test(HttpServletRequest request){
 41         
 42         // 保存一個新用戶
 43         String uid = userService.save(UserFactory.createUser());
 44         User user = userService.getUserById(uid);
 45         user.setUsername("xiaoli");
 46         userService.update(user);
 47         
 48         // 查詢該用戶
 49         System.out.println(gson.toJson(user, User.class));
 50         /*System.out.println();
 51         // 再查詢該用戶
 52         User user = userService.getUserById(uid);
 53         System.out.println(gson.toJson(user, User.class));
 54         System.out.println();
 55         // 更新該用戶
 56         userService.update(user);
 57         // 更新好了再查詢該用戶
 58         System.out.println(gson.toJson(userService.getUserById(uid), User.class));
 59         System.out.println();
 60         // 刪除該用戶
 61         userService.del(uid);
 62         System.out.println();
 63         // 刪除好了再查詢該用戶
 64         System.out.println(gson.toJson(userService.getUserById(uid), User.class));*/
 65         
 66         
 67         // 再保存一個新用戶
 68 //        String uid1 = userService.save(UserFactory.createUser());
 69         
 70         
 71         return uid;
 72     }
 73     
 74     
 75     @RequestMapping(value = "/test1", method = RequestMethod.GET)
 76     public String test1(HttpServletRequest request,String key){
 77         
 78         String res = "";
 79 
 80         Element element = cmf.getElement("userCache", "map");
 81         if(element == null){
 82             Map<String, String> map = new HashMap<String, String>();
 83             map.put(key, key);
 84             cmf.setElement("userCache", new Element("map", map));
 85         }else{
 86             Map<String, String> map = (Map<String, String>) element.getValue();
 87             res = map.get(key);
 88             if(res == null){
 89                 map.put(key, key);
 90                 // 多次測試發現,存在同名Element是,重復put的是無法復制的,因此當遇到兩個節點同步不上的時候,先remove后put。 
 91                 cmf.getCache("userCache").remove("map");
 92                 cmf.setElement("userCache", new Element("map", map));
 93                 res = "0;null";
 94             }
 95         }
 96         return res;
 97     }
 98     
 99     
100 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM