Results 1 to 4 of 4

Thread: Caching

  1. #1
    Join Date
    Sep 2006
    Posts
    210
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Caching

    Hi

    Well I have Googled for hours to no avail so I am hoping there is an Asp.Net 2.0 guru on here that knows about caching.

    Okay so on my forms in the code behind VB flavour I put some site settings into cache (I check first to see if the cache has been ejected) if it has I then run the following code:

    'Create New Object SiteSettings
    Dim myCacheSiteSettings As New SiteSettingsCs

    'Load SiteSettings Data
    myCacheSiteSettings.SiteName = GetSharedSettings.SiteName

    ' Insert the Cache into memory
    Cache.Insert("SiteSettings", myCacheSiteSettings, Nothing, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(15))

    Works great

    Now the problem is that I am doing this on every page I need the site settings, what I would like to do is get the above routine into 1 place in a class for example where I can call it to reload the cache.

    But so far I have been unable to workout how to do this

    So can anyone point me in the right direction please

    Thanks

  2. #2
    Join Date
    Jun 2005
    Posts
    1,081
    Thanks
    4
    Thanked 15 Times in 15 Posts

    Default

    Here's how I would do it in C#

    Code:
    public class SiteSettingsManager
    {
        //we don't want to instantiate this class.
        private SiteSettingsManager()
        {}
    
    
        public static SiteSettingsCs GetSiteSettings()
        {
            SiteSettingsCs siteSettings = Cache["SiteSettings"];
            if (siteSettings == null)
            { 
                 // the SiteSettings were not in the cache so create them
                 siteSettings = new SiteSettings();
    
                 // put result in the cache
                 Cache.Insert("SiteSettings", siteSettings, null, Cache.NoAbsoluteExpiration, TimSpan.FromMinutes(15));
            }
    
            return siteSettings;
    }

  3. #3
    Join Date
    Sep 2006
    Posts
    210
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default

    Hi

    But this line of code won't complie

    Insert the Cache into memory
    Cache.Insert("SiteSettings", myCacheSiteSettings, Nothing, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(15))

    Gives this error
    reference to a non shared member requires an object reference

  4. #4
    Join Date
    Jun 2005
    Posts
    1,081
    Thanks
    4
    Thanked 15 Times in 15 Posts

    Default

    Try wrapping the Cache class:

    Code:
    public class MyCache
    {
      private static readonly System.Web.Caching.Cache m_cache;
     
      ///<summary>
      /// Static initializer should ensure we only have to look up the current cache
      /// instance once.
      ///</summary>
      static MyCache()
      {
          HttpContext context = HttpContext.Current;
          if (context != null)
          {
               m_cache = context.Cache;
           }
           else
           {
               m_cache = HttpRuntime.Cache;
            }
       }
     
       ///<summary>
       /// Removes all items from the Cache
       ///</summary>
       public static void Clear()
       {
             IDictionaryEnumerator CacheEnum = m_cache.GetEnumerator();
             while (CacheEnum.MoveNext())
                 m_cache.Remove(CacheEnum.Key.ToString());
        }
     
        ///<summary>
        /// Removes the specified key from the cache
        ///</summary>
        ///<param name="key"></param>
        public static void Remove(string key)
        {
              m_cache.Remove(key);
         }
     
         ///<summary>
         /// Insert the current "obj" into the cache. 
         ///</summary>
         ///<param name="key"></param>
        ///<param name="obj"></param>
        public static void Insert(string key, object obj)
        {
              Insert(key, obj, null, 1);
         }
     
         public static void Insert(string key, object obj, int seconds, CacheItemPriority priority)
         {
               Insert(key, obj, null, seconds, priority);
          }
     
          public static void Insert(string key, object obj, CacheDependency dep, int seconds, CacheItemPriority priority)
         {
                if (obj != null)
               {
                      m_cache.Insert(key, obj, dep, DateTime.Now.AddSeconds(m_factor * seconds), TimeSpan.Zero, priority, null);
               }
          }
     
          public static object Get(string key)
          {
                return m_cache[key];
          }
     
    }
    
    I should also point out that doing this allows you to use the cache from Windows Forms applications.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. HOWTO: Handle Website Caching
    By Warren Ashcroft in forum Technical Support
    Replies: 0
    Last Post: 22nd December 2005, 11:51 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •