惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
The GitHub Blog
The GitHub Blog
Recent Announcements
Recent Announcements
MyScale Blog
MyScale Blog
P
Proofpoint News Feed
S
Security @ Cisco Blogs
N
News and Events Feed by Topic
H
Hacker News: Front Page
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
Microsoft Security Blog
Microsoft Security Blog
N
Netflix TechBlog - Medium
U
Unit 42
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
Google Online Security Blog
Google Online Security Blog
Spread Privacy
Spread Privacy
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
L
LINUX DO - 热门话题
T
Tenable Blog
博客园 - 叶小钗
D
DataBreaches.Net
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园_首页
人人都是产品经理
人人都是产品经理
aimingoo的专栏
aimingoo的专栏
C
Check Point Blog
博客园 - 三生石上(FineUI控件)
量子位
P
Proofpoint News Feed
H
Help Net Security
Blog — PlanetScale
Blog — PlanetScale
宝玉的分享
宝玉的分享
Recorded Future
Recorded Future
The Register - Security
The Register - Security
F
Fortinet All Blogs
Engineering at Meta
Engineering at Meta
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
S
Schneier on Security
V
Vulnerabilities – Threatpost
雷峰网
雷峰网
Microsoft Azure Blog
Microsoft Azure Blog
G
GRAHAM CLULEY
G
Google Developers Blog
月光博客
月光博客
V
V2EX
T
Troy Hunt's Blog
A
Arctic Wolf

博客园 - 大熊(BigBear)

ADO.net实现批量SQL操作. - 大熊(BigBear) - 博客园 Application_AcquireRequestState事件,导致Ajax客户端不能加载 CSLA.Net在Web项目中使用SQL2005的分页 Moss中Form验证中"找不到匹配项"问题的处理 对于知识库系统建设的思考 知识管理中知识地图详解〔转〕 知识管理中的一些概念 Aop面向方面编程之PostSharp框架 [摘]IIS上部署WCF的问题 Linq使用之分组 Silverlight表格绑定中的一点细节处理 页面加载性能的优化 由一棵树绑定引发的遐想 .Net下调用Java安全Web服务 Webpart深入(二) Webpart深入(一) ASP.NET MVC 中文文档3 ASP.NET MVC 中文文档2 ASP.net MVC 中文文档1
IBatis.Net使用MemCached替换内置缓存策略
大熊(BigBear) · 2009-11-24 · via 博客园 - 大熊(BigBear)

最近有个项目, 使用Ibatis.net需要部署在负载均衡的环境下, 显然Ibatis.net的内置缓存方式, 是不能适用的.

多个Web服务器之间的缓存不能进行同步是问题的关键. 于是决定扩展他的缓存策略, 使用MemCached.

Ibatis.net现有的缓存方式有: MEMORY, LRU, FIFO, 我们扩展一个叫MemCached的方式.

1. 实现一个ICacheController类, 先要下载一个MemCaced的客户端,

我使用的是enyim.com memcached 1.2.0.2的客户端.

    /// <summary>
    /// 使用MemCached做分布式缓存
    /// </summary>
    public class MemCachedController : ICacheController
    {
        MemcachedClient _mc = null;
        private int _cacheSize = 0;
        private IList _keyList = null;

        /// <summary>
        ///
        /// </summary>
        public MemCachedController()
        {
            _mc = new MemcachedClient();
            _cacheSize = 100;
            _keyList = ArrayList.Synchronized(new ArrayList());
        }

        #region ICacheController Members
        /// <summary>
        ///
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public object this[object key]
        {
            get
            {
                _keyList.Remove(key);
                _keyList.Add(key);
                return _mc.Get(key.ToString());
            }
            set
            {
                _mc.Store(StoreMode.Set, key.ToString(), value);
                _keyList.Add(key);
                if (_keyList.Count > _cacheSize)
                {
                    object oldestKey = _keyList[0];
                    _keyList.Remove(0);
                    _mc.Remove(oldestKey.ToString());
                }
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public object Remove(object key)
        {
            //object o = _mc.Get(key.ToString());
            _keyList.Remove(key);
            _mc.Remove(key.ToString());
            return null;
        }

        /// <summary>
        ///
        /// </summary>
        public void Flush()
        {
            //_mc.FlushAll();
            foreach (object arr in _keyList)
            {
                _mc.Remove(arr.ToString());
            }
            _keyList.Clear();
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="properties"></param>
        public void Configure(System.Collections.IDictionary properties)
        {
            string size = (string)properties["CacheSize"];
            if (size != null)
            {
                _cacheSize = Convert.ToInt32(size);
            }       
        }

        #endregion
    }

如上, 比较简单.

2. 第二步在DomSqlMapBuilder类中注册我们的新缓存类型.

            // xionglx添加的使用MemCached

            cacheAlias = new TypeAlias(typeof(MemCachedController));

            cacheAlias.Name = "MEMCACHED";

            _configScope.SqlMapper.TypeHandlerFactory.AddTypeAlias(cacheAlias.Name, cacheAlias);

3. 在SqlMap.xsd配置文件中注册新的类型,

<xs:simpleType>

       <xs:restriction base="xs:NMTOKEN">

                <xs:enumeration value="LRU"/>

                <xs:enumeration value="MEMORY"/>

                <xs:enumeration value="FIFO"/>

                <xs:enumeration value="MEMCACHED"/>

        </xs:restriction>

</xs:simpleType>

4. Web.Config中添加配置.

        <!--memcached-->
        <sectionGroup name="enyim.com">
            <section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching" />
        </sectionGroup>

<enyim.com>
        <memcached>
            <servers>
                <add address="127.0.0.1" port="11211" />
                <!--<add address="127.0.0.1" port="20004" />-->
            </servers>
            <socketPool minPoolSize="10" maxPoolSize="100" connectionTimeout="00:10:00" deadTimeout="00:02:00" />
        </memcached>
    </enyim.com>

经过测试, 可以正常的访问MemeCached服务端,  并且缓存正常.

需要注意的是, 实体类需要标记为可序列化.  在Map文件的缓存策略中,

<cacheModels>
        <cacheModel id="ModuleCache"  implementation="MEMCACHED"  readOnly="false">
            <flushInterval hours="24"/>
            <flushOnExecute  statement="Module.Insert"/>
            <flushOnExecute  statement="Module.Update"/>
            <flushOnExecute  statement="Module.Delete"/>
            <property name="CacheSize" value="100"/>
        </cacheModel>
    </cacheModels>

不能使用serialize="true", 反序列化的时候会出错, 我还没仔细查原因. 这里readOnly="false"是说, 这个对象是

会发生更改的, 只有那些系统初始化后, 不会发生变化的数据,才设置成readOnly="true".