views:

27

answers:

1

I am wondering about potential issues with – alternatives to - caching using an application variable. To preface, I have a custom application object that stores all application data in ONE application variable – the object organizes data in an XML cloud and stores the cloud in one variable for performance.

I am aware of DataSet caching, but all of my DAL objects select (read only) into data readers for performance, so if the solution involves DataSets I have a ton of refactoring to do – not ideal. Record count is low to medium, involving website data and small to medium project management apps; we're not talking a half-million records.

Here is the structure of the function I intend to use in my DAL classes (Select All):

if (AppSetting.GetSetting(_CacheSettingName) == "")
    {
        SqlHelper objSqlHelper = new SqlHelper();
        XmlReader objReader = objSqlHelper.GetXmlReaderByCmd("Select * From FAQ FOR XML PATH('item'), root('" + _CacheSettingName + "')");

//load Cache       

        StringBuilder _xml = new StringBuilder();
        objReader.Read();
        while (objReader.ReadState != ReadState.EndOfFile)
        {
            _xml.Append(objReader.ReadOuterXml());
        }
        objSqlHelper.Dispose();
        AppSetting.SaveSetting(_CacheSettingName, _xml.ToString());
    }
    //we have cache loaded
    // now load the object list from the XML cloud from the application cache       
    List<FAQBLL> objFAQList = new List<FAQBLL>();
    FAQBLL objFAQ;
    XmlDocument oXmlDocument = new XmlDocument();
    oXmlDocument.LoadXml(AppSetting.GetSetting(_CacheSettingName));
    foreach (XmlNode oNode in oXmlDocument.FirstChild.ChildNodes)
    {
        objFAQ = new FAQBLL();
        objFAQ.ID = Convert.ToInt32(oNode.SelectSingleNode("ID").InnerXml);
        objFAQ.Question = oNode.SelectSingleNode("Question").InnerXml;
        objFAQ.Answer = oNode.SelectSingleNode("Answer").InnerXml;
        objFAQList.Add(objFAQ);
        objFAQ = null;

    }

    return objFAQList.Count > 0 ? objFAQList: null;

So my cache returns all to the calling proc, then I LINK to filter the object (by active, by location). On insert, update, and delete I have one line of code to clear the cache. Again, I have the advantage of using read only data readers for performance, and my application object organizes XML into one application variable. MY thinking was that this is low overhead, not having to discard data readers for data sets when I'm just reading data.

Thoughts? Opinions? Are you traumatized by this?

+1  A: 

yes, it is a little traumatizing. The problem with using application variables as a cache is that you forfeit some of the best features of caches. For example, the asp.net cache provides some awesome cache invalidation features. This ensures that a) the cache does not increasingly take up more and more resources, and b) that the cache remains fresh (for example, if sourced from a database).

You should really think about using the right tool for the job

Joel Martinez
Thanks for the helpful response. Given these issues, it might be wise to consider dataset caching.
shxo