设为首页 收藏本站
查看: 1525|回复: 0

[经验分享] IBatis.Net使用MemCached替换内置缓存策略

[复制链接]

尚未签到

发表于 2015-8-31 14:12:47 | 显示全部楼层 |阅读模式
  最近有个项目, 使用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=&quot;key&quot;></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=&quot;key&quot;></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=&quot;properties&quot;></param>   
        public void Configure(System.Collections.IDictionary properties)   
        {   
            string size = (string)properties[&quot;CacheSize&quot;];   
            if (size != null)   
            {   
                _cacheSize = Convert.ToInt32(size);   
            }        
        }
  #endregion   
    }
  如上, 比较简单.
  
  2. 第二步在DomSqlMapBuilder类中注册我们的新缓存类型.
  // xionglx添加的使用MemCached
  cacheAlias = new TypeAlias(typeof(MemCachedController));
  cacheAlias.Name = &quot;MEMCACHED&quot;;
  _configScope.SqlMapper.TypeHandlerFactory.AddTypeAlias(cacheAlias.Name, cacheAlias);
  
  3. 在SqlMap.xsd配置文件中注册新的类型,
  <xs:simpleType>
  <xs:restriction base=&quot;xs:NMTOKEN&quot;>
  <xs:enumeration value=&quot;LRU&quot;/>
  <xs:enumeration value=&quot;MEMORY&quot;/>
  <xs:enumeration value=&quot;FIFO&quot;/>
  <xs:enumeration value=&quot;MEMCACHED&quot;/>
  </xs:restriction>
  </xs:simpleType>
  
  4. Web.Config中添加配置.
  <!--memcached-->   
        <sectionGroup name=&quot;enyim.com&quot;>   
            <section name=&quot;memcached&quot; type=&quot;Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching&quot; />   
        </sectionGroup>
  <enyim.com>   
        <memcached>   
            <servers>   
                <add address=&quot;127.0.0.1&quot; port=&quot;11211&quot; />   
                <!--<add address=&quot;127.0.0.1&quot; port=&quot;20004&quot; />-->   
            </servers>   
            <socketPool minPoolSize=&quot;10&quot; maxPoolSize=&quot;100&quot; connectionTimeout=&quot;00:10:00&quot; deadTimeout=&quot;00:02:00&quot; />   
        </memcached>   
    </enyim.com>
  
  经过测试, 可以正常的访问MemeCached服务端,  并且缓存正常.
  需要注意的是, 实体类需要标记为可序列化.  在Map文件的缓存策略中,
  <cacheModels>   
        <cacheModel id=&quot;ModuleCache&quot;  implementation=&quot;MEMCACHED&quot;  readOnly=&quot;false&quot;>   
            <flushInterval hours=&quot;24&quot;/>   
            <flushOnExecute  statement=&quot;Module.Insert&quot;/>   
            <flushOnExecute  statement=&quot;Module.Update&quot;/>   
            <flushOnExecute  statement=&quot;Module.Delete&quot;/>   
            <property name=&quot;CacheSize&quot; value=&quot;100&quot;/>   
        </cacheModel>   
    </cacheModels>
  不能使用serialize=&quot;true&quot;, 反序列化的时候会出错, 我还没仔细查原因. 这里readOnly=&quot;false&quot;是说, 这个对象是
  会发生更改的, 只有那些系统初始化后, 不会发生变化的数据,才设置成readOnly=&quot;true&quot;.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-106866-1-1.html 上篇帖子: 全windows平台下进行memcached开发 下篇帖子: memcached session
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表