Cache
https://www.jianshu.com/p/5a0669d6305e
iCEM 的 cache 是用 ehcache
首先先建立一個 SpringRootConfig 的 class
@Configuration
@Import({ ...CacheConfig.class... })
@ComponentScan({ ... })
public class SprintRootConfig {
}
@Configuration 代表這個 class 是一個 @bean
@Import 是將其他的 bean 引入此檔案 (可以想成把其他 xml 引入主要 xml)
@ComponentScan 是告訴 Spring 該掃描哪裡的 package
然後在 WebInitializer 的 gteRootConfigClasses 方法,把 SpringRootConfig 加進去,這樣初始時就知道要用了哪些 config
Cache 也是在 SpringRootConfig 設定的
接下來看一下 CacheConfig
這個 class 也是一個 @Configuration
裡面宣告兩個 bean
@Bean(name="springCacheManager")
public CacheManager cacheManager() {
return new EhCacheCacheManager(ehCacheCacheManager().getObject());
}
@Bean
public EhCacheManagerFactoryBean ehCacheCacheManager() {
}
如此一來其他的 class 在透過 @Autowired 就可以取得這兩個 bean
例如在其他檔案宣告
@Autowired
public CacheManager cacheManager;
那麼 Autowired 會去找註冊過的 Bean
我們會宣告一份 xml 檔案用來指定一些 cache 的設定
<cache name="ABCCache"
maxEntriesLocalHeap="100"
eternal="false"
timeToLiveSeconds="3600"
transactionalMode="off">
<persistence strategy="none"/>
</cache>
然後在要取用這個 cache 的地方如下取用:
this.cacheManager.getCache("ABCCache").put(Key, value);