針對「讀多寫少、變動頻率低」的熱點資料,iBATIS 內建快取機制可大幅降低資料庫負載。CacheModel 在 SQL Map 設定檔中預先定義,再由 MappedStatement 參照使用。
<cacheModel id="employee-cache"
implementation="LRU"
readOnly="true"
serialize="false">
<flushInterval hours="24"/>
<flushOnExecute statement="insertEmployee"/>
<flushOnExecute statement="updateEmployee"/>
<flushOnExecute statement="deleteEmployee"/>
<property name="CacheSize" value="100"/>
</cacheModel>
| 屬性 | 說明 | 可選值/範例 |
|---|---|---|
implementation | 快取淘汰演算法 | LRU (最近最少使用)、FIFO、MEMORY |
readOnly | 唯讀快取效能更高 | true/false |
serialize | 跨序列化支援 (叢集環境) | true/false |
flushInterval | 定時清空間隔 | hours/minutes/seconds/milliseconds |
flushOnExecute | 執行特定 statement 時清空 | statement="insertEmployee" |
CacheSize | 最大快取條目數 | 超過時依 implementation 淘汰 |
<statement id="EmployeeList" cacheModel="employee-cache">
SELECT * FROM Employee
</statement>
EmployeeList 時,結果存入 employee-cacheinsertEmployee、updateEmployee、deleteEmployee 任一時,快取整體失效