developer155
Programmer
Hi,
I have the following situation: I have an app running on a a web-farm (load-balanced). What the app does is simply accepts HTTP Post and saves query string passed into the db. I need to add a caching mechanism so if db is down the app will cache the query string and try to insert it later. So I wrote the following code that creates datatable and adds it to cache object. My question is do I need to worry about the web-farm environment? I do not believe Cache needs to be syncronized between machines as each machine will have its own cache, which is fine. Please let me know if the following code makes sense
try{
//insert into db
}
catch
{
//check if this is db error
if (ex.Message.IndexOf("connection") > 0)
{
DataTable cacheTable;
if (Cache["cacheTable"] != null)
cacheTable = (DataTable)Cache["cacheTable"];
else//create new table
{
cacheTable = new DataTable();
cacheTable.Columns.Add(new DataColumn("PartnerID"));
cacheTable.Columns.Add(new DataColumn("Email"));
cacheTable.Columns.Add(new DataColumn("QueryString"));
}
//create row and ad
DataRow rowToCache = cacheTable.NewRow();
rowToCache["PartnerID"] = Scramble.Decrypt(Server.UrlDecode(Request.QueryString["PID"].ToString()));
rowToCache["Email"] = Server.UrlDecode(Request.QueryString["EM"].ToString());
rowToCache["QueryString"] = Request.QueryString.ToString();
cacheTable.Rows.Add(rowToCache);
Cache.Insert("cacheTable", cacheTable, null, DateTime.Now.AddDays(2), new TimeSpan(2));
}
}
-----------------------------------------------
Then I have this code to insert Cache stuff in db
//check to see if we have anything cached and if so put it in db
DataTable cacheTable;
if (Cache["cacheTable"] != null)
{
cacheTable = (DataTable)Cache["cacheTable"];
//insert into db
InsertIntoDb(cacheTable);
//clear cache
Cache.Remove("cacheTable");
}
I have the following situation: I have an app running on a a web-farm (load-balanced). What the app does is simply accepts HTTP Post and saves query string passed into the db. I need to add a caching mechanism so if db is down the app will cache the query string and try to insert it later. So I wrote the following code that creates datatable and adds it to cache object. My question is do I need to worry about the web-farm environment? I do not believe Cache needs to be syncronized between machines as each machine will have its own cache, which is fine. Please let me know if the following code makes sense
try{
//insert into db
}
catch
{
//check if this is db error
if (ex.Message.IndexOf("connection") > 0)
{
DataTable cacheTable;
if (Cache["cacheTable"] != null)
cacheTable = (DataTable)Cache["cacheTable"];
else//create new table
{
cacheTable = new DataTable();
cacheTable.Columns.Add(new DataColumn("PartnerID"));
cacheTable.Columns.Add(new DataColumn("Email"));
cacheTable.Columns.Add(new DataColumn("QueryString"));
}
//create row and ad
DataRow rowToCache = cacheTable.NewRow();
rowToCache["PartnerID"] = Scramble.Decrypt(Server.UrlDecode(Request.QueryString["PID"].ToString()));
rowToCache["Email"] = Server.UrlDecode(Request.QueryString["EM"].ToString());
rowToCache["QueryString"] = Request.QueryString.ToString();
cacheTable.Rows.Add(rowToCache);
Cache.Insert("cacheTable", cacheTable, null, DateTime.Now.AddDays(2), new TimeSpan(2));
}
}
-----------------------------------------------
Then I have this code to insert Cache stuff in db
//check to see if we have anything cached and if so put it in db
DataTable cacheTable;
if (Cache["cacheTable"] != null)
{
cacheTable = (DataTable)Cache["cacheTable"];
//insert into db
InsertIntoDb(cacheTable);
//clear cache
Cache.Remove("cacheTable");
}