I'm attempting to use Azure redis cache to reduce hits to my database, but I'm stuck on something very weird. My WebMethod looks as such.
[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();
}
}
StackExchange redis throws a multiplexer error that isn't very descriptive, but the kicker is if i reload the page it successfully pulls the json data from my redis cache. The json string is on average about 8MB. Any thoughts or suggestions on this issue would be greatly appreciated.