我使用以下代码将每个省项目添加到 Appfabric 缓存中。
并使用 GetObjectsInRegion 方法从缓存中获取省份列表。
但是对于 120 条记录,它比 SQL Server 慢得多。
使用 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>