C# 在Redis Azure中存储更大(2MB)的对象
我正在尝试使用Azure中的Redis在我的应用程序中进行缓存。我的每把钥匙都可能超过2-4MB。当我在本地机器上针对Redis运行我的应用程序时,一切都很好,但是当在Azure上运行时性能很差,检索密钥通常需要8-10秒,实际上从原始源重新获取这些数据要比从缓存中更快 所以我想第一个问题是,我的钥匙太大了吗?我是不是用Redis找错了方向 如果没有,你知道为什么这么慢吗?该应用程序是一个Azure网站,该网站和redis实例位于同一区域。我正在使用stackexchange redis客户端,并在global.asax文件中将多路复用器创建为单例,为避免重新创建,代码如下: Global.asax:C# 在Redis Azure中存储更大(2MB)的对象,c#,azure,asp.net-web-api,redis,C#,Azure,Asp.net Web Api,Redis,我正在尝试使用Azure中的Redis在我的应用程序中进行缓存。我的每把钥匙都可能超过2-4MB。当我在本地机器上针对Redis运行我的应用程序时,一切都很好,但是当在Azure上运行时性能很差,检索密钥通常需要8-10秒,实际上从原始源重新获取这些数据要比从缓存中更快 所以我想第一个问题是,我的钥匙太大了吗?我是不是用Redis找错了方向 如果没有,你知道为什么这么慢吗?该应用程序是一个Azure网站,该网站和redis实例位于同一区域。我正在使用stackexchange redis客户端,
redisConstring = ConfigurationManager.ConnectionStrings["RedisCache"].ConnectionString;
if (redisConstring != null)
{
if (RedisConnection == null || !RedisConnection.IsConnected)
{
RedisConnection = ConnectionMultiplexer.Connect(redisConstring);
}
RedisCacheDb = RedisConnection.GetDatabase();
Application["RedisCache"] = RedisCacheDb;
}
IDatabase redisCache = System.Web.HttpContext.Current.Application["RedisCache"] as IDatabase;
string cachedJson = redisCache.StringGet(id);
if (cachedJson == null)
{
cachedJson=OutfitFactory.GetMembersJson(id);
redisCache.StringSet(id, cachedJson, TimeSpan.FromMinutes(15));
}
return OutfitFactory.GetMembersFromJson(cachedJson);
Web API控制器:
redisConstring = ConfigurationManager.ConnectionStrings["RedisCache"].ConnectionString;
if (redisConstring != null)
{
if (RedisConnection == null || !RedisConnection.IsConnected)
{
RedisConnection = ConnectionMultiplexer.Connect(redisConstring);
}
RedisCacheDb = RedisConnection.GetDatabase();
Application["RedisCache"] = RedisCacheDb;
}
IDatabase redisCache = System.Web.HttpContext.Current.Application["RedisCache"] as IDatabase;
string cachedJson = redisCache.StringGet(id);
if (cachedJson == null)
{
cachedJson=OutfitFactory.GetMembersJson(id);
redisCache.StringSet(id, cachedJson, TimeSpan.FromMinutes(15));
}
return OutfitFactory.GetMembersFromJson(cachedJson);
从评论来看,问题似乎是带宽。。。所以:使用更少的带宽。想法:
作为参考,在SE,我们使用gzip压缩protobuf网络进行包装是的,这并不是特别小,但也不可怕;在使用redis之前,你是否使用本地服务器内存作为缓存?@MarcGravell不,目前我只使用redis(主要原因是我想为流行查询预加载缓存),但如果需要,我可以这样做,在尝试使用redis之前,我使用的是本地内存。你衡量过时间花在哪里了吗?是带宽吗?或者是反序列化时间?我很确定这是带宽,只是用Redis桌面工具访问一个值也很慢。我刚刚把它转换到内存缓存中使用,速度快了很多,但不是很理想。使用GZIP和Protbuf将大小降到8K,并使它比现在的内存缓存要好得多。非常感谢你的帮助!