Difference between HttpRuntime.Cache and HttpContext.Current.Cache?

后端 未结 3 598
忘了有多久
忘了有多久 2020-12-01 00:56

What is the difference between HttpRuntime.Cache and HttpContext.Current.Cache?

3条回答
  •  心在旅途
    2020-12-01 01:36

    Using HttpRuntime.Cache is simple to use than HttpContext.Current.Cache.As already said that objects can be stored in the cache and are indexed by a string.Also in unit test and console HttpRuntime this available.

    Here is an example to use HttpRuntime.Cache.

    public static XmlDocument GetStuff(string sKey) 
    {
    XmlDocument xmlCodes;
    xmlCodes = (XmlDocument) HttpRuntime.Cache.Get( sKey );
    if (xmlCodes == null)
    {
          xmlCodes = SqlHelper.ExecuteXml(new dn("Nodes", "Node"), "Get_Stuff_From_Database", sKey);
          HttpRuntime.Cache.Add(sKey, xmlCodes, null,
          DateTime.UtcNow.AddMinutes(1.0),
          System.Web.Caching.Cache.NoSlidingExpiration,
          System.Web.Caching.CacheItemPriority.Normal, null);
    }
    return xmlCodes;
    }
    

    What this example does actually:


    The method GetStuff takes a string parameter which is used to retrieve a set of items from the database. The method first checks to see if an XmlDocument indexed by the parameter key is in the cache. If it is, it simply returns this object, if not it queries database. After it has retrieved the document from the database it then put it into cache. If this method is called again within the stipulated time, it will get the object rather than hitting the database.

提交回复
热议问题