上一篇IBatis.net ORM初体验简单介绍了IBatis.net的基本应用.本篇介绍下IBatis.net的缓存应用.

还是接着上篇的代码来写..

首先在sqlmap.config中启用缓存

<setting cacheModelsEnabled="true"/>

然后新建一个xml:CacheAccount.xml

并在sqlmap.config中的sqlMaps节点配置

<sqlMaps>
   <sqlMap resource="Maps/Account.xml"/>
   <sqlMap resource="Maps/CacheAccount.xml"/>
</sqlMaps>

新建立的CacheAccount.xml如下

<?xml version="1.0" encoding="utf-8" ?>
<sqlMap namespace="CacheAccount" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 
  <!--
  implementation="MEMORY"是设置缓存的实现方式,可以指定LRU、FIFO等,有点类似于内存的页替换策略。MEMORY是最常使用的一种方式。
  flushOnExecute设置的是当执行了这些语句时更新缓存  readOnly="false" serialize="true"
  -->
  <cacheModels>
    <cacheModel id="account-cache"  implementation="MEMORY" >
      <flushInterval hours="24"/>
      <flushOnExecute  statement="Account.sql_InsertOne"/>
      <!--<flushOnExecute  statement="sql_Delete"/>-->
      <flushOnExecute  statement="CacheAccount.sql_update"/>
      <property name="Type" value="Weak"/>
    </cacheModel>
  </cacheModels>
  <resultMaps>
    <resultMap id="Account-result"  class="Account">
      <result property="Id"    column="id"/>
      <result property="Item"    column="Item"/>
      <result property="Year"    column="Year"/>
      <result property="Month"    column="Month"/>
      <result property="Day"    column="Day"/>
      <result property="CreateOn"    column="CreateOn"/>
      <result property="Level"    column="Level"/>
    </resultMap>
  </resultMaps>
 
  <statements>
    <select id="sql_CacheSelect" resultMap="Account-result" cacheModel="account-cache" >
      select * from Accounts
    </select>
 
    <delete id="sql_Delete" parameterClass="int">
      delete from Accounts
      <dynamic prepend="where">
        <isParameterPresent property="id" prepend="">
          [id] = #id#
        </isParameterPresent>
      </dynamic>
    </delete>
 
    <update id="sql_update" parameterClass="Account">
      update Accounts set Item = #Item#
      where id = #Id#
    </update>
  </statements>
</sqlMap>

 

说明:

cacheModels节点是配置缓存的节点,cacheModel的property元素来设置,目前包括以下的 4 个实现:

MEMORY” (com.ibatis.db.sqlmap.cache.memory.MemoryCacheController) 。MEMORY cache 实现使用 reference 类型来管理 cache 的行为。垃圾收集器可以根据 reference类型判断是否要回收 cache 中的数据。MEMORY实现适用于没有统一的对象重用模式的应用,或内存不足的应用。

LRU” (com.ibatis.db.sqlmap.cache.lru.LruCacheController) 。LRU Cache 实现用“近期最少使用”原则来确定如何从 Cache 中清除对象。当 Cache溢出时,最近最少使用的对象将被从 Cache 中清除。使用这种方法,如果一个特定的对象总是被使用,它将保留在 Cache 中,而且被清除的可能性最小。对于在较长的期间内,某些用户经常使用某些特定对象的情况(例如,在 PaginatedList 和常用的查询关键字结果集中翻页) ,LRU Cache 是一个不错的选择。

FIFO” (com.ibatis.db.sqlmap.cache.fifo.FifoCacheController) 。FIFO Cache 实现用“先进先出”原则来确定如何从 Cache 中清除对象。当 Cache 溢出时,最先进入 Cache 的对象将从 Cache 中清除。对于短时间内持续引用特定的查询而后很可能不再使用的情况,FIFO Cache 是很好的选择。

OSCACHE” (com.ibatis.db.sqlmap.cache.oscache.OSCacheController)  。OSCACHE Cache 实现是OSCache2.0缓存引擎的一个 Plugin。它具有高度的可配置性,分布式,高度的灵活性。

flushOnExecute:设置的是当执行了这些语句时更新缓存。

flushInterval : Cache刷新间隔. 可以配置hours,minutes,seconds,milliseconds.

property : 这是针对cacheModel的额外的一些属性配置.不同type的cacheModel将会有自己专有的一些property配置.
                 FIFO: <property name="size" value="100" />
                LRU: <property name="cache-size" value="100" />
                MEMORY: <property name="reference-type" value="WEAK" />

readOnly : 是否只读. 默认为true, 只读.

serialize : 是否从Cache中读取同一个对象,还是对象的副本. 只有在readOnly=false才有效.  因为Cache是只读的,那么为不同session返回的对象肯定是一个. 只有在Cache是可读写的时候,才需要为每个session返回对象的副本.

注意我这里更改了命名空间:namespace="CacheAccount"

select 节点 加入 cacheModel="account-cache"

 

注:如果不在同一个命名空间下  需要用命名空间.名称

添加三个statement,查询,删除 更新..增加在另一个xml文件里,为了做缓存测试,我把删除的statement注释掉了.

 

添加一个Service类CacheAccountService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBatisDemo.Model;
using IBatisDemo.Dao;
 
namespace IBatisDemo.Service
{
    //测试IBatis缓存
    public class CacheAccountService
    {
        public IList<Accounts> GetAccountList()
        {
            return Mapper.GetMaper.QueryForList<Accounts>("CacheAccount.sql_CacheSelect", null);
        }
 
        public void DeleteAccountById(int id)
        {
            Mapper.GetMaper.Delete("CacheAccount.sql_Delete", id);
        }
 
        public void UpdateAccount(Accounts account) 
        {
            Mapper.GetMaper.Update("CacheAccount.sql_update", account);
        }
    }
}

 

新建Default1.aspx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IBatisDemo.Service;
using IBatisDemo.Model;
 
namespace IBatisDemo
{
    public partial class Default1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        //删除
        protected void Button1_Click(object sender, EventArgs e)
        {
            CacheAccountService service = new CacheAccountService();
            service.DeleteAccountById(int.Parse(this.TextBox1.Text));
        }
        //查询全部
        protected void Button2_Click(object sender, EventArgs e)
        {
            CacheAccountService service = new CacheAccountService();
            this.GridView1.DataSource = service.GetAccountList();
            this.GridView1.DataBind();
        }
        //添加
        protected void Button3_Click(object sender, EventArgs e)
        {
            Accounts account = new Accounts();
            account.Id = -1;
            account.CreateOn = DateTime.Now;
            account.Day = 12;
            account.Item = "小刚1";
            account.Level = "无";
            account.Money = 56;
            account.Month = 6;
            account.Year = 2011;
 
            AccountService service = new AccountService();
            service.TestInsertOne(account);
        }
        //更新
        protected void Button4_Click(object sender, EventArgs e)
        {
            Accounts account = new Accounts();
            account.Id = 4;
            account.Item = this.TextBox2.Text;
            CacheAccountService service = new CacheAccountService();
            service.UpdateAccount(account);
        }
    }
}

 

运行效果界面:

TM截图未命名

 

测试说明:

1,首先点击 查询测试缓存,获取到全部数据,此时数据被缓存起来.

2,因为我们注释掉了删除方法,所以删除一条数据,缓存不会更新,点击删除,再点击 查询测试缓存,发现数据不会发生变化.

  注:缓存也是有命中率滴,所以有时候不缓存上不要惊慌.

3,之后点击添加或者更新,缓存会更新,再点击查询全部按钮  获得的就是更新缓存之后的数据.

 

通过昨天的测试,IBatis.net在Framwork4.0下也是可以运行成功滴.

作者: 小刚qq 发表于 2011-06-30 17:53 原文链接

推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架
新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"