3.8 Cache Models

创建一个cacheModel

 <cacheModels>
    <cacheModel id="Cache1" implementation="LRU">
      <flushInterval hours="24"/>
      <flushOnExecute statement="GetById"/>
      <flushOnExecute statement="updateProduct"/>
      <flushOnExecute statement="deleteProduct"/>
      <property name="CacheSize" value="1" />
    </cacheModel>
  </cacheModels>
View Code

给statement指定cacheModel

<statement id="getProductList" cacheModel="product-cache">
  select * from PRODUCT where PRD_CAT_ID = #value#
</statement>
View Code

3.8.1. Read-Only vs. Read/Write

ReadOnly(默认)缓存全局共享提供更好的性能。不能修改从缓存读出的对象的值,修改会导致缓存值修改,其他人数据库读取操作仍然读取该缓存的话,会读到修改后的缓存的值(此时和数据值不同)

如果要修改对象的值使用Read/Write缓存模式,设置readOnly="false"

3.8.2. Serializable Read/Write Caches

单独设置readOnly=“false” 使用Read/Write模式缓存,每个会话缓存数据对程序性能没有帮助。结合 serialize="true" 使用  serializable read/write cache。这种模式的缓存每次会话返回的缓存对象不是同一个,所以每个会话可以修改该对象。

Realize the difference in semantics here, usually you would expect the same instance to be returned from a cache, but in this case you'll get a different one.(体会一下这里语义上的差异,通常我们期望返回的缓存对象是同一个,但是这里会返回不同的对象,值相同,引用不同)。

使用此缓存模式需要缓存对象可序列化。所以在使用lazy loading feature时会遇到问题,因为lazy proxies不能序列化。最好的解决办法就是,试一试,再决定使用哪种缓存方式,还有使用懒加载(lazy load)还是连接查询(table join)。

默认值 为 readOnly=“true”  serialize="false"

3.8.3. Cache Implementation

缓存模块使用可插拔架构设计支持不同类型的缓存。(loading...   还没看,源码怎么设计的鸭??

缓存必须实现ICacheController 接口或者是“MEMORY”、"LRU"、"FIFO"中的一种

3.8.4. "MEMORY"

GC决定是否回收缓存的对象  对于没有对象重用模式的应用程序或内存不足的应用程序来说是一个不错的选择。(The MEMORY cache is a good choice for applications that don't have an identifiable pattern of object reuse, or applications where memory is scarce.

例子

<cacheModel id="product-cache" implementation="MEMORY" >
  <flushInterval hours="24"/>
  <flushOnExecute  statement="insertProduct"/>
  <flushOnExecute  statement="updateProduct"/>
  <flushOnExecute  statement="deleteProduct"/>
  <property name="Type" value="WEAK"/>
</cacheModel>

只有一个属性可以设置,属性的值必须是:STRONG, SOFT, or WEAK

    • WEAK(default)大部分时候是最好的选择,可以提升经常使用的结果集的性能,但是如果结果集没有使用,一定会释放内存用来创建新的对象。
    • SOFT(目前只有JAVA适用)This reference type will reduce the likelihood of running out of memory in case the results are not currently in use and the memory is needed for other objects. However, this is not the most aggressive reference type in that regard and memory still might be allocated and made unavailable for more important objects.
    • STRONG 结果集会一直在内存中,直到被flushede.g. by time interval or flush on execute) 。对于非常小,静态,经常使用的对象来说是最好的选择。优点就是对于这个特定的查询来说性能非常好。缺点就是内存一直被占用。

3.8.5. "LRU"

最近最少使用原则,最近最少使用的对象大概率在内存不足时被回收。这对于一些具有使用模式的应用程序来说是一个好的选择,一些对象会在一段时间内被一个或多个用户经常访问。例如分页列表之间的来回导航(经常翻页)或热点搜索

例子

<cacheModel id="product-cache"  implementation="LRU" >
  <flushInterval hours="24"/>
  <flushOnExecute  statement="insertProduct"/>
  <flushOnExecute  statement="updateProduct"/>
  <flushOnExecute  statement="deleteProduct"/>
   <property name="CacheSize" value="100"/>
</cacheModel>

 

Only a single property is recognized by the LRU cache implementation. This property, named CacheSize must be set to an integer value representing the maximum number of objects to hold in the cache at once. An important thing to remember here is that an object can be anything from a single String instance to an ArrayList of object. So take care not to store too much in your cache and risk running out of memory!

CacheSize.value到底设置的是什么鸭?不是结果集行数,去看源码叭?  loading...

3.8.6. "FIFO"

First In First Out 

例子:

<cacheModel id="product-cache" implementation="FIFO" >
  <flushInterval hours="24"/>
  <flushOnExecute  statement="insertProduct"/>
  <flushOnExecute  statement="updateProduct"/>
  <flushOnExecute  statement="deleteProduct"/>
  <property name="CacheSize" value="100"/>
</cacheModel>
 
posted @ 2020-03-24 00:08  vvf  阅读(204)  评论(0编辑  收藏  举报