Springboot+Mybatis集成自定義緩存Ehcache用法
今天小編給大家整理了springboot+mybatis集成自定義緩存ehcache用法筆記,希望對(duì)大家能有所辦幫助!
一、ehcache介紹
EhCache 是一個(gè)純Java的進(jìn)程內(nèi)緩存管理框架,屬于開源的Java分布式緩存框架,主要用于通用緩存,Java EE和輕量級(jí)容器。
1、特點(diǎn)
1. 簡(jiǎn)單、快速
2. 提供多種緩存策略
3. 緩存數(shù)據(jù)可分兩級(jí):內(nèi)存和磁盤
4. 緩存數(shù)據(jù)會(huì)在服務(wù)器重啟的過(guò)程中重新寫入磁盤
.5 可以通過(guò)RMI、可插入API等方式進(jìn)行分布式緩存
6. 具有緩存和緩存管理器的偵聽接口
7. 支持多緩存管理器實(shí)例,以及一個(gè)實(shí)例的多個(gè)緩存區(qū)域
8. 提供了Hibernate的緩存實(shí)現(xiàn)
2、應(yīng)用場(chǎng)景
單應(yīng)用或?qū)彺嬖L問(wèn)性能要求很高的應(yīng)用
適合簡(jiǎn)單共享
適合緩存內(nèi)容不大的場(chǎng)景,比如MyBatis自定義緩存、系統(tǒng)配置信息、頁(yè)面緩存。
二、springboot+mybatis集成ehcache步驟
Spring Boot 的緩存機(jī)制
高速緩存抽象不提供實(shí)際存儲(chǔ),并且依賴于由org.springframework.cache.Cache和org.springframework.cache.CacheManager接口實(shí)現(xiàn)的抽象。 Spring Boot根據(jù)實(shí)現(xiàn)自動(dòng)配置合適的CacheManager,只要緩存支持通過(guò)@EnableCaching注解啟用即可。
1、添加ehcache.xml配置文件
- <?xml version="1.0" encoding="UTF-8"?>
 - <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 - xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
 - <diskStore path="java.io.tmpdir" />
 - <!-- 配置提供者 1、peerDiscovery,提供者方式,有兩種方式:自動(dòng)發(fā)現(xiàn)(automatic)、手動(dòng)配置(manual) 2、rmiUrls,手動(dòng)方式時(shí)提供者的地址,多個(gè)的話用|隔開 -->
 - <cacheManagerPeerProviderFactory
 - class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
 - properties="peerDiscovery=manual,rmiUrls=//127.0.0.1:40002/userCache" />
 - <!-- <cacheManagerPeerProviderFactory
 - class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
 - properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, multicastGroupPort=4446,timeToLive=255"/>
 - -->
 - <!-- 配置監(jiān)聽器 1、hostName 主機(jī)地址 2、port 端口 3、socketTimeoutMillis socket子模塊的超時(shí)時(shí)間,默認(rèn)是2000ms -->
 - <cacheManagerPeerListenerFactory
 - class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
 - properties="hostName=127.0.0.1, port=40001, socketTimeoutMillis=2000" />
 - <!-- <cacheManagerPeerListenerFactory
 - class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"/> -->
 - <defaultCache eternal="false" maxElementsInMemory="1000"
 - overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
 - timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" />
 - <cache
 - name="userCache"
 - maxElementsInMemory="1000"
 - eternal="false"
 - timeToIdleSeconds="300"
 - timeToLiveSeconds="300"
 - overflowToDisk="false"
 - memoryStoreEvictionPolicy="LRU">
 - <!-- 配置緩存事件監(jiān)聽器 replicateAsynchronously 操作是否異步,默認(rèn)值為true. replicatePuts 添加操作是否同步到集群內(nèi)的其他緩存,默認(rèn)為true.
 - replicateUpdates 更新操作是否同步到集群內(nèi)的其他緩存,默認(rèn)為true. replicateUpdatesViaCopy 更新之后的對(duì)象是否復(fù)制到集群中的其他緩存(true);
 - replicateRemovals 刪除操作是否同步到集群內(nèi)的其他緩存,默認(rèn)為true. -->
 - <cacheEventListenerFactory
 - class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
 - properties="
 - replicateAsynchronously=true,
 - replicatePuts=true,
 - replicateUpdates=true,
 - replicateUpdatesViaCopy=true,
 - replicateRemovals=true " />
 - <!-- 初始化緩存,以及自動(dòng)設(shè)置 -->
 - <bootstrapCacheLoaderFactory
 - class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"
 - properties="bootstrapAsynchronously=true" />
 - </cache>
 - </ehcache>
 
2、配置 application.properyies
- #cache 配置cache
 - spring.cache.cache-names=userCache
 - spring.cache.jcache.config=classpath:ehcache.xml
 
3、springboot啟動(dòng)類增加注解@EnableCaching
- @SpringBootApplication
 - @ComponentScan(basePackages="com.ehcache")//掃描組件
 - @EnableCaching
 - public class EhcacheTestApplication {
 - public static void main(String[] args) {
 - SpringApplication.run(EhcacheTestApplication.class, args);
 - }
 - }
 
4、UserInfoService.java 文件增加緩存注解
- @Service
 - public class UserInfoService {
 - @Autowired
 - private UserDao userDao;
 - @CacheEvict(key="'user_'+#uid", value="userCache")
 - public void del(String uid) {
 - userDao.del(uid);
 - }
 - @CachePut(key="'user_'+#user.id", value="userCache")
 - public void update(User user) {
 - userDao.update(user);
 - }
 - @Cacheable(key="'user_'+#id",value="userCache")
 - public User getUserById(String id){
 - return userDao.findById(id); }
 - @CacheEvict(key="'user'",value="userCache")
 - public String save(User user) {
 - return userDao.save(user);
 - }
 - }
 
5、增加測(cè)試控制器TestController.java
- package com.ehcache.controller;
 - import java.util.ArrayList;
 - import java.util.HashMap;
 - import java.util.List;
 - import java.util.Map;
 - import javax.servlet.http.HttpServletRequest;
 - import org.springframework.beans.factory.annotation.Autowired;
 - import org.springframework.cache.annotation.CachePut;
 - import org.springframework.cache.annotation.Cacheable;
 - import org.springframework.web.bind.annotation.RequestMapping;
 - import org.springframework.web.bind.annotation.RequestMethod;
 - import org.springframework.web.bind.annotation.RequestParam;
 - import org.springframework.web.bind.annotation.ResponseBody;
 - import org.springframework.web.bind.annotation.RestController;
 - import com.ehcache.entity.User;
 - import com.ehcache.factory.CacheManagerFactory;
 - import com.ehcache.factory.UserFactory;
 - import com.ehcache.service.UserService;
 - import com.google.gson.Gson;
 - import net.sf.ehcache.Element;
 - @RestController
 - @RequestMapping("/CacheTest")
 - public class CacheTestController {
 - @Autowired
 - private UserService userService;
 - Gson gson = new Gson();
 - CacheManagerFactory cmf = CacheManagerFactory.getInstance();
 - @RequestMapping(value = "/test", method = RequestMethod.GET)
 - public String test(HttpServletRequest request){
 - // 新增新用戶
 - String id = userService.save(UserFactory.createUser());
 - User user = userService.getUserById(id);
 - user.setUsername("小明");
 - userService.update(user);
 - // 查詢?cè)撚脩?nbsp;
 - System.out.println(gson.toJson(user, User.class));
 - System.out.println();
 - // 再查詢?cè)撚脩?nbsp;
 - User user = userService.getUserById(uid);
 - System.out.println(gson.toJson(user, User.class));
 - System.out.println();
 - // 更新該用戶
 - userService.update(user);
 - // 更新成功后再查詢?cè)撚脩?nbsp; System.out.println(gson.toJson(userService.getUserById(id), User.class));
 - System.out.println();
 - // 刪除該用戶
 - userService.del(id);
 - System.out.println();
 - // 刪除后再查詢?cè)撚脩?nbsp; System.out.println(gson.toJson(userService.getUserById(id), User.class));
 - return id;
 - }
 - }
 
個(gè)人博客網(wǎng)站:https://programmerblog.xyz
本文轉(zhuǎn)載自微信公眾號(hào)「IT技術(shù)分享社區(qū)」,可以通過(guò)以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系IT技術(shù)分享社區(qū)公眾號(hào)。
















 
 
 










 
 
 
 