spring集成ehcache緩存

1.Ehcahce是什麼?

Ehcache是一種廣泛使用的開源Java分佈式緩存。主要面向通用緩存,Java EE和輕量級容器。它具有內存和磁盤存儲,緩存加載器,緩存擴展,緩存異常處理程序,一個gzip緩存servlet過濾器,支持REST和SOAP api等特點。

Ehcache適用場景

1、比較少的更新數據表的情況
2、對併發要求不是很嚴格的情況
多臺應用服務器中的緩存是不能進行實時同步的。
3、對一致性要求不高的情況下
因爲Ehcache本地緩存的特性,目前無法很好的解決不同服務器間緩存同步的問題,所以我們在一致性要求非常高的場合下,儘量使用Redis、Memcached等集中式緩存。

2.Spring怎麼集成ehcache

1、pom.xml

//除了SpringMVC 、sql server相關就是ehcache-core
    <dependency>
      <groupId>net.sf.ehcache</groupId>
      <artifactId>ehcache-core</artifactId>
      <version>2.6.11</version>
    </dependency>

2.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"
    updateCheck="false">

<!-- 緩存到磁盤路徑 -->
//java.io.tmpdir 默認的臨時文件夾
    <diskStore path="java.io.tmpdir" />
//默認配置
    <defaultCache 
        maxElementsInMemory="10000" maxElementsOnDisk="0" 
        eternal="true"         overflowToDisk="true"
        diskPersistent="false" timeToIdleSeconds="0" 
        timeToLiveSeconds="0"  diskSpoolBufferSizeMB="50" 
        diskExpiryThreadIntervalSeconds="120"   memoryStoreEvictionPolicy="LFU" />
//自定義配置
    <cache name="myCache"         
        maxElementsInMemory="1000"   maxElementsOnDisk="0" 
        eternal="false"          overflowToDisk="false"
        diskPersistent="false"   timeToIdleSeconds="120" 
        timeToLiveSeconds="300"  diskSpoolBufferSizeMB="50" 
        diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LFU" />

</ehcache>

關於緩存策略的理解:

memoryStoreEvictionPolicy:緩存滿了之後的淘汰算法。

  1. 1 FIFO,先進先出
  2. 2 LFU,最少被使用,緩存的元素有一個hit屬性,hit值最小的將會被清出緩存。
  3. 3 LRU,(Ehcache默認策略),緩存的元素有一個時間戳,當緩存容量滿了,而又需要騰出地方來緩存新的元素的時候,那麼現有緩存元素中時間戳離當前時間最遠的元素將被清出緩存

3.Spring配置,進行bean類的注入

   <!--啓用緩存註解-->
    <cache:annotation-driven cache-manager="cacheManager"/>
    <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:ehcache.xml"/>
    </bean>
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="cacheManagerFactory"/>
    </bean>

4.業務上應用

4.1 Service層裏調用Dao層時使用緩存註解

在listAllUser方法上使用Cacheable註解,第一次運行時緩存裏還沒數據控制檯會打印query from database...再次調用則直接讀取緩存數據

@Cacheable(value = "myCache")
    public List<User> listAllUser() {
        System.out.println("query from database...");
        return userDao.listAllUser();
    }

4.2 Controller裏調用

@Controller
@RequestMapping("/User")
public class UserController {
 
    @Autowired
    UserService userService;
 
    @RequestMapping("/index")
    public ModelAndView index(){
        System.out.println("size:"+userService.listAllUser().size());
 
        ModelAndView mav =new ModelAndView("index");
 
        return mav;
    }
}

3.Spring +ehcache +redis實現二級緩存

客官請移步
https://blog.csdn.net/liaoyulin0609/article/details/51787020

4.思考?

什麼性質的數據適合放入緩存?

緩存池的大小怎麼配置才能獲得更好的性能?

ehcache一致性問題

ehcache是一種本地緩存,存放位置是JVM,所以它的一致性不好,併發寫操作頻繁的數據,不採用使用ehcache緩存;

讀操作,先讀緩存,緩存沒有再去數據庫讀,並且把結果放到緩存中。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章