yuanqiao 发表于 2015-8-31 14:12:47

IBatis.Net使用MemCached替换内置缓存策略

  最近有个项目, 使用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   
      {   
            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;   
                  _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;/>   
            <flushOnExecutestatement=&quot;Module.Insert&quot;/>   
            <flushOnExecutestatement=&quot;Module.Update&quot;/>   
            <flushOnExecutestatement=&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]
查看完整版本: IBatis.Net使用MemCached替换内置缓存策略