Appfabric在区域中获取列表对象非常慢

时间:2013-10-21 11:09:31

标签: sql-server performance caching appfabric

我使用以下代码将每个省项添加到Appfabric缓存。

并使用GetObjectsInRegion方法从缓存中获取省列表。

但是对于120条记录来说,它比SQL服务器慢得多。

使用Appfabric它需要超过3000毫秒&使用SQL服务器大约30毫秒。

是什么原因?

请帮帮我!!!!

try
        {
            using (CacheEntities context = new CacheEntities())
            {
                if (force)
                {
                    Cache.RemoveRegion(ObjectName);
                }
                if (Cache.CreateRegion(ObjectName))
                {
                    List<Province> province = (from t in context.Province select t).ToList<Province>();
                    if (province != null && province.Count() > 0)
                    {
                        foreach (Province provinceItem in province)
                        {
                            CommonFunctions.Add(ObjectName, provinceItem.ProvinceID, provinceItem);
                            totalItemsCached++;
                        }
                    }
                }

            }
        }
        catch (Exception ex)
        {
            CommonFunctions.ErrorInfo(ObjectName, ex);
        }

编辑:缓存来自评论的检索代码:

using System.Collections.Generic;
using System.Threading.Tasks;

public class Sample
{
    public List<object> GetListObjectsInRegion(string region)
    {
        var cache = GetCache();
        IEnumerable<KeyValuePair<string, object>> rawResult = 
              cache.GetObjectsInRegion(region);

        List<object> result = new List<object>();
        Parallel.ForEach(rawResult, rawResultItem => 
              { result.Add(rawResultItem.Value); });
        return result;
    }
}

更新:这是GetCache函数中的代码

private static DataCacheFactory _factory;
    private static DataCache _cache;
    private static DataCache GetCache()
    {
        if (_cache != null) return _cache;

        DataCacheFactoryConfiguration configuration = new DataCacheFactoryConfiguration();
        configuration.IsCompressionEnabled = true;
        _factory = new DataCacheFactory(configuration);
        _cache = _factory.GetCache("default");
        return _cache;
    }

更新:这是appfabric配置

  </configSections>
  <dataCacheClient>
    <localCache isEnabled="true" sync="TimeoutBased" objectCount="1000" ttlValue="600" />
    <hosts>
      <host name="localhost" cachePort="22233" />
    </hosts>
  </dataCacheClient>

0 个答案:

没有答案