gpt4 book ai didi

c# - WCF REST 服务的 Azure 缓存间歇性响应时间

转载 作者:太空狗 更新时间:2023-10-29 22:00:56 26 4
gpt4 key购买 nike

我正在 Azure 中构建 EF6 Web 应用程序,并使用 Azure 缓存。我正在测试对 WCF 服务的调用,但响应时间非常不稳定 - 在 300 毫秒到 15 秒之间!

我根据 this example 配置了我的代码,它在本地运行良好

我已经进行了远程调试,我可以看到正在找到缓存键并且正在从缓存中调用数据,因此我很难理解为什么响应时间存在巨大差异。大多数时候是 5 秒以上,这显然太长了。

我测试过的示例如下:

WCF 服务 GET 请求至: http://feniksdev-staging.azurewebsites.net/EkckoNewsService.svc/getFriends

       // Cache client configured by settings in application configuration file.
public DataCacheFactory cacheFactory = new DataCacheFactory();
public DataCache _cache;
public DataCache cache
{
get
{
if (_cache == null)
_cache = cacheFactory.GetDefaultCache();

return _cache;
}
set { }
}
...
...

[OperationContract]
[System.ServiceModel.Web.WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "/getFriends")]
public string getFriends()
{
string cachekey = "getFriends/{" + user.Id + "}";
object result = cache.Get(cachekey);
if (result == null)
{
using (EkckoContext entities = new EkckoContext())
{
var frnds = entities.UserConnections.Where(uc => uc.UserId == user.Id).Select(uc => new { Name = uc.Friend.Username }).ToList();
JsonSerializerSettings jsonSettings = new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects };
string json = JsonConvert.SerializeObject(frnds, jsonSettings);
cache.Add(cachekey, json);
return json;
}
}
else
{
return (string)result;
}
}

UserConnection 是我的数据库中的一个简单表,当前没有数据,因此调用返回一个空的 JSON 数组。 user 是一个 Session 对象,当前 user.Id 默认为 1

远程调试时,会在缓存中找到该对象并返回缓存的对象。一切都很好,只是响应时间仍然相差 20 倍(300 毫秒 - 6 秒)。

在远程调试其他 Web 服务方法之一时,在尝试使用相应的 key (object result = cache.Get(cachekey);) 访问缓存对象时出现以下错误:

{"ErrorCode:SubStatus:There is a temporary failure. Please retry later. (One or more specified cache servers are unavailable, which could be caused by busy network or servers. For on-premises cache clusters, also verify the following conditions. Ensure that security permission has been granted for this client account, and check that the AppFabric Caching Service is allowed through the firewall on all cache hosts. Also the MaxBufferSize on the server must be greater than or equal to the serialized object size sent from the client.). Additional Information : The client was trying to communicate with the server: net.tcp://ekckodev.cache.windows.net:22238."}

然后,我在配置中设置 maxBufferSize,如下所示:

    <configSections>
<section name="dataCacheClients" type="Microsoft.ApplicationServer.Caching.DataCacheClientsSection, Microsoft.ApplicationServer.Caching.Core" allowLocation="true" allowDefinition="Everywhere" />
<section name="cacheDiagnostics" type="Microsoft.ApplicationServer.Caching.AzureCommon.DiagnosticsConfigurationSection, Microsoft.ApplicationServer.Caching.AzureCommon" allowLocation="true" allowDefinition="Everywhere" />
</configSections>
...
...
<system.web>
...
...
<caching>
<outputCache defaultProvider="AFCacheOutputCacheProvider">
<providers>
<add name="AFCacheOutputCacheProvider" type="Microsoft.Web.DistributedCache.DistributedCacheOutputCacheProvider, Microsoft.Web.DistributedCache" cacheName="default" dataCacheClientName="default" applicationName="AFCacheOutputCache" />
</providers>
</outputCache>
</caching>
</system.web>
....
....
...
<dataCacheClients>
<dataCacheClient name="default">
<autoDiscover isEnabled="true" identifier="ekckodev.cache.windows.net" />
<localCache isEnabled="true" sync="TimeoutBased" objectCount="100000" ttlValue="300" />
<securityProperties mode="Message" sslEnabled="false">
<messageSecurity authorizationInfo="xxxxxxxxxxxxxxxxxxxxxxx" />
</securityProperties>
<transportProperties connectionBufferSize="131072" maxBufferPoolSize="268435456"
maxBufferSize="8388608" maxOutputDelay="2" channelInitializationTimeout="60000"
receiveTimeout="600000"/>
</dataCacheClient>
</dataCacheClients>

但我的响应时间仍然不稳定 - 特别是在重复调用同一服务时。

添加 maxbuffersize 配置后,缓存调用仍然时好时坏。有些获取对象;其他时候我会遇到相同的异常,但端口不同

"... The client was trying to communicate with the server: net.tcp://ekckodev.cache.windows.net:22233."}"

这可能是防火墙问题吗?如果是这样,我如何打开适当的端口?

在实例化 DataCache 对象时,我还遇到了以下异常:

_cache = cacheFactory.GetDefaultCache();

ErrorCode:SubStatus:There is a temporary failure. Please retry later. (One or more specified cache servers are unavailable, which could be caused by busy network or servers. For on-premises cache clusters, also verify the following conditions. Ensure that security permission has been granted for this client account, and check that the AppFabric Caching Service is allowed through the firewall on all cache hosts. Also the MaxBufferSize on the server must be greater than or equal to the serialized object size sent from the client.)

对于为什么我会得到这样的结果有什么想法吗?使用缓存肯定不会比不使用缓存更快,因此缓存中似乎存在某种延迟,这似乎不正确......

预先感谢您的帮助!

更新:经过更多搜索后,似乎我不是唯一遇到此问题的人: poor performance with azure cache

我很难相信这就是我应该期待的表现

更新2我已经注释掉了我的服务中所有与缓存相关的代码,并再次运行了相同的测试。如果没有缓存,响应时间会明显缩短!在没有缓存的情况下,“getFriends”调用平均大约为 250 毫秒,但在有缓存的情况下,峰值时间超过 5 秒。我的另一种方法获取大约 4kb 的数据,使用缓存时峰值时间为 20 秒以上,现在不使用缓存时平均时间约为 2 秒。

再次:我很难相信这就是我应该期待的表现

更新3我现在已经放弃了 Azure 缓存,转而使用 MemoryCache。很好的例子 here我的服务调用现在在浏览器中持续花费大约 300 毫秒。

我已经向 Microsoft Azure 支持开了一张有关 Azure 缓存的票证,所以当他们联系我并询问他们为什么他们的缓存如此垃圾时,我会更新这篇文章。正当我对微软的信心不断攀升时:/

最佳答案

看来您已经得出了正确的结论,即不要使用 Azure 托管缓存。大约 6 个月前,Microsoft 开始建议所有新开发都针对 Azure 中基于 Redis 的缓存产品进行。

We recommend all new developments use Azure Redis Cache.

奇怪的是,他们没有显示在“旧”Azure 管理站点 (manage.windowsazure.com) 中创建 Redis 缓存的选项,但他们在“预览”中确实有此选项 Azure management portal .

关于c# - WCF REST 服务的 Azure 缓存间歇性响应时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24053970/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com