我正在尝试使用Azure redis缓存来减少对我的数据库的命中率,但我被一些非常奇怪的东西卡住了。我的WebMethod看起来就是这样。
[WebMethod]
public static string HistoricalData(string searchterm, string topicId)
{
string constr = ConfigurationManager.ConnectionStrings["Azure"].ConnectionString;
var lazyConnection = new Lazy<ConnectionMultiplexer>(() => {
string cacheConnection = ConfigurationManager.ConnectionStrings["CacheConnection"].ToString();
return ConnectionMultiplexer.Connect(cacheConnection);
});
JavaScriptSerializer json = new JavaScriptSerializer();
List<HistoricalGraph> hg = new List<HistoricalGraph>();
json.MaxJsonLength = Int32.MaxValue;
IDatabase cache = lazyConnection.Value.GetDatabase();
var cachedCategory = cache.StringGet(topicId).ToString();
if (cachedCategory == null){
using (SqlConnection conn = new SqlConnection(constr))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("my_sp", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Type", "GetData");
cmd.Parameters.AddWithValue("@Topic", searchterm);
using (SqlDataReader r = cmd.ExecuteReader())
{
if (r.HasRows)
{
while (r.Read())
{
hg.Add(new HistoricalGraph
{
asin = r.GetValue(0).ToString() ?? "",
title = r.GetValue(1).ToString() ?? "",
tagBrand = r.GetValue(2).ToString() ?? "",
TagFieldValue1 = r.GetValue(3).ToString() ?? "",
TagFieldValue2 = r.GetValue(4).ToString() ?? "",
TagFieldValue3 = r.GetValue(5).ToString() ?? "",
TagFieldValue4 = r.GetValue(6).ToString() ?? "",
TagFieldValue5 = r.GetValue(7).ToString() ?? "",
pack = r.GetValue(8).ToString() ?? "",
date = r.GetValue(9).ToString() ?? "",
avgPrice = r.GetValue(10).ToString() ?? "",
units = r.GetValue(11).ToString() ?? "",
revenue = r.GetValue(12).ToString() ?? "",
image = r.GetValue(13).ToString() ?? ""
});
}
}
}
}
}
System.Diagnostics.Debug.WriteLine("From SQL");
var serializedData = json.Serialize(hg);
cache.StringSet(topicId, serializedData).ToString(); //Errors here
return serializedData;
}
else
{
System.Diagnostics.Debug.WriteLine("From Redis Cache");
return cachedCategory.ToString();
}
}json字符串的平均大小约为8MB。任何关于这个问题的想法或建议都将不胜感激。
发布于 2020-04-28 04:20:08
抛出的错误如下所示:
System.TimeoutException: Timeout performing MGET 2728cc84-58ae-406b-8ec8-3f962419f641, inst: 1,mgr: Inactive, queue: 73, qu=6, qs=67, qc=0, wr=1/1, in=0/0
IOCP: (Busy=6, Free=999, Min=2,Max=1000), WORKER (Busy=7,Free=8184,Min=2,Max=8191)为了让它正常工作,我做了一些改动。
首先,我删除并重新创建了缓存,将TLS保留为默认值。然后,为了减小响应的大小,我对字符串进行了压缩和解压缩。将大小降低到更易于管理的1MB。希望如此,但此后我再也没有收到多路复用器错误。
发布于 2022-02-08 18:06:19
检查是否以JSON格式存储数据。接下来,您需要反序列化topicId值下的json数据
https://stackoverflow.com/questions/61374738
复制相似问题