springboot+hibernate+ehcache的二級緩存清除方法,手動管理緩存(特別是集羣環境下對同一個數據庫進行操作)

1:注入EntityManager

@PersistenceContext
private EntityManager entityManger;

2:獲取hibernate緩存對象

CacheImpl cache=(CacheImpl)entityManger.getEntityManagerFactory().getCache();

3:接下來就可以隨心所欲過期不需要的緩存,如實體緩存,集合緩存,查詢緩存等,具體方法自己去查看

org.hibernate.internal.CacheImpl類,如清除全部類型緩存是cache.evictAllRegions()

主要有以下幾類:

(1)evictCollectionRegions():集合緩存相關
(2)evictDefaultQueryRegion();查詢緩存
(3)evictEntityRegions();實體緩存
(4)evictQueryRegions();查詢緩存
(5)evictNaturalIdRegions();不知道什麼緩存

ps:有想要了解具體hibernate緩存詳情的請查看友鏈:

https://www.baeldung.com/hibernate-second-level-cache,                                  https://blog.csdn.net/czp11210/article/details/51996217

 

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