Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Caching on Web Farm

Status
Not open for further replies.

developer155

Programmer
Joined
Jan 21, 2004
Messages
512
Location
US
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");
}
 
my only suggestions is the race condition that exists between inserting an object into cache and clearing the cache before it's inserted into the db.
Datatables are considered heavy objects. instead you may want to create a lightweight DTO.

I would store an IList<EmailDTO> in cache and remove objects 1 at a time.
Code:
public class EmailDTO
{
   public string PartnerID
   {
       get { return this.parenterId; }
   }
   public string Email
   {
       get { return this.email; }
   }
   public string QueryString
   {
       get { return this.queryString; }
   }
   private string partnerId, email, queryString;
   public EmailDTO(string partnerId, string email, string queryString)
   {
      this.parenterId = partnerId;
      this.email = email;
      this.queryString = queryString;
   }
}

//somwhere in code
EmailDTO dto = new EmailDTO(Scramble.Decrypt(Server.UrlDecode(Request.QueryString["PID"].ToString())), Server.UrlDecode(Request.QueryString["EM"].ToString()), Request.QueryString.ToString());
try
{
   //insert into db
}
catch
{
   //check if this is db error
   if (ex.Message.IndexOf("connection") > 0)
   {
        IList<EmailDTO> dtos = (IList<EmailDTO>)Cache("dtos");
        if(dtos == null)
        {
           dtos = new List<EmailDTO>();
        }
        dtos.Add(EmailDTO);
   }
}

//somewhere else is code
IList<EmailDTO> dtos = (IList<EmailDTO>)Cache("dtos");
if(dtos != null)
{
   foreach(EmailDTO dto in dtos)
   {
       //insert into DB
       dtos.Remove(dto);
   }
}
there may be some problems with this, but it would ensure you don't accidentally drop entire entries when clearing cache. It should also be more responsive(milliseconds?), since your dealing with simple DTOs.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top