自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Springboot+Mybatis集成自定義緩存Ehcache用法

存儲 存儲軟件
EhCache 是一個純Java的進程內(nèi)緩存管理框架,屬于開源的Java分布式緩存框架,主要用于通用緩存,Java EE和輕量級容器。

[[425595]]

今天小編給大家整理了springboot+mybatis集成自定義緩存ehcache用法筆記,希望對大家能有所辦幫助!

一、ehcache介紹

EhCache 是一個純Java的進程內(nèi)緩存管理框架,屬于開源的Java分布式緩存框架,主要用于通用緩存,Java EE和輕量級容器。

1、特點

1. 簡單、快速

2. 提供多種緩存策略

3. 緩存數(shù)據(jù)可分兩級:內(nèi)存和磁盤

4. 緩存數(shù)據(jù)會在服務(wù)器重啟的過程中重新寫入磁盤

.5 可以通過RMI、可插入API等方式進行分布式緩存

6. 具有緩存和緩存管理器的偵聽接口

7. 支持多緩存管理器實例,以及一個實例的多個緩存區(qū)域

8. 提供了Hibernate的緩存實現(xiàn)

2、應(yīng)用場景

單應(yīng)用或?qū)彺嬖L問性能要求很高的應(yīng)用

適合簡單共享

適合緩存內(nèi)容不大的場景,比如MyBatis自定義緩存、系統(tǒng)配置信息、頁面緩存。

二、springboot+mybatis集成ehcache步驟

Spring Boot 的緩存機制

高速緩存抽象不提供實際存儲,并且依賴于由org.springframework.cache.Cache和org.springframework.cache.CacheManager接口實現(xiàn)的抽象。 Spring Boot根據(jù)實現(xiàn)自動配置合適的CacheManager,只要緩存支持通過@EnableCaching注解啟用即可。

1、添加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,提供者方式,有兩種方式:自動發(fā)現(xiàn)(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.     <!-- 配置監(jiān)聽器 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.         <!-- 配置緩存事件監(jiān)聽器 replicateAsynchronously 操作是否異步,默認值為true. replicatePuts 添加操作是否同步到集群內(nèi)的其他緩存,默認為true. 
  37.             replicateUpdates 更新操作是否同步到集群內(nèi)的其他緩存,默認為true. replicateUpdatesViaCopy 更新之后的對象是否復(fù)制到集群中的其他緩存(true); 
  38.             replicateRemovals 刪除操作是否同步到集群內(nèi)的其他緩存,默認為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.          <!-- 初始化緩存,以及自動設(shè)置 --> 
  50.         <bootstrapCacheLoaderFactory 
  51.             class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" 
  52.             properties="bootstrapAsynchronously=true" /> 
  53.  
  54.     </cache> 
  55.  
  56. </ehcache> 

2、配置 application.properyies

  1. #cache 配置cache  
  2.  
  3. spring.cache.cache-names=userCache  
  4. spring.cache.jcache.config=classpath:ehcache.xml 

3、springboot啟動類增加注解@EnableCaching

  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.     } 

4、UserInfoService.java 文件增加緩存注解

  1. @Service 
  2. public class UserInfoService { 
  3.  
  4.     @Autowired 
  5.     private UserDao userDao; 
  6.  
  7.     @CacheEvict(key="'user_'+#uid", value="userCache"
  8.     public void del(String uid) {        
  9.         userDao.del(uid); 
  10.     } 
  11.  
  12.     @CachePut(key="'user_'+#user.id", value="userCache"
  13.     public void update(User user) { 
  14.         userDao.update(user); 
  15.     } 
  16.  
  17.     @Cacheable(key="'user_'+#id",value="userCache"
  18.     public User getUserById(String id){      
  19.         return userDao.findById(id);    } 
  20.  
  21.     @CacheEvict(key="'user'",value="userCache"
  22.     public String save(User user) {         
  23.         return userDao.save(user); 
  24.     } 

5、增加測試控制器TestController.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("/CacheTest"
  30. public class CacheTestController { 
  31.     @Autowired 
  32.     private UserService userService; 
  33.     Gson gson = new Gson(); 
  34.     CacheManagerFactory cmf = CacheManagerFactory.getInstance(); 
  35.     @RequestMapping(value = "/test", method = RequestMethod.GET) 
  36.     public String test(HttpServletRequest request){ 
  37.         // 新增新用戶 
  38.         String id = userService.save(UserFactory.createUser()); 
  39.         User user = userService.getUserById(id); 
  40.         user.setUsername("小明"); 
  41.         userService.update(user); 
  42.         // 查詢該用戶 
  43.         System.out.println(gson.toJson(userUser.class));       
  44.         System.out.println(); 
  45.         // 再查詢該用戶 
  46.         User user = userService.getUserById(uid); 
  47.         System.out.println(gson.toJson(userUser.class)); 
  48.         System.out.println(); 
  49.         // 更新該用戶 
  50.         userService.update(user); 
  51.         // 更新成功后再查詢該用戶        System.out.println(gson.toJson(userService.getUserById(id), User.class)); 
  52.         System.out.println(); 
  53.         // 刪除該用戶 
  54.         userService.del(id); 
  55.         System.out.println(); 
  56.         // 刪除后再查詢該用戶        System.out.println(gson.toJson(userService.getUserById(id), User.class)); 
  57.         return id; 
  58.     } 

 個人博客網(wǎng)站:https://programmerblog.xyz

本文轉(zhuǎn)載自微信公眾號「IT技術(shù)分享社區(qū)」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系IT技術(shù)分享社區(qū)公眾號。

 

責任編輯:武曉燕 來源: IT技術(shù)分享社區(qū)
相關(guān)推薦

2023-02-14 07:47:20

SpringBootEhcache

2021-06-05 07:34:00

SpringBootMybatis用法

2023-01-11 15:11:36

SpringEhcache

2024-10-09 10:46:41

springboot緩存redis

2022-03-15 08:22:31

Ehcachespring緩存

2015-07-29 10:31:16

Java緩存算法

2014-12-31 09:56:29

Ehcache

2023-10-11 07:57:23

springboot微服務(wù)

2010-08-13 11:34:54

Flex自定義事件

2023-01-04 09:33:31

SpringBootMybatis

2023-07-03 08:29:11

BannerSpringBoot

2014-12-31 09:45:48

EhCache

2015-02-12 15:33:43

微信SDK

2023-10-12 08:00:48

2021-03-16 10:39:29

SpringBoot參數(shù)解析器

2022-07-11 10:37:41

MapPart集合

2023-10-09 07:37:01

2015-02-12 15:38:26

微信SDK

2021-05-26 06:22:34

SpringBootJPA后端開發(fā)

2021-07-11 07:05:28

RedisSpringBoot用法
點贊
收藏

51CTO技術(shù)棧公眾號