locked
LazyCache Not Working on Remote IIS Server RRS feed

  • Question

  • User-939035612 posted

    I successfully added LazyCache to an ASP.Net 4.7.2 WebForm site, but when I upload it to my remote IIS server it does not appear to be working. I have caching enabled on the server, but for whatever reason I do not seem to be getting cached results when I load a page. 

    I am using LazyCache in my DAL to cache results from table adapters in .xsd files. On my local machine I know that the data is cached because when I add a new post to the website and visit the home page I do not see the new post. I don't see the new post because the data source for the home page has a cache that should not be refreshed for 30 minutes, so I must wait to see the new post on the page. 

    When I deploy the site to the remote server I never have to wait to see a new post on the home page or any place else. Something is keeping the application from caching the results of the queries in memory. 

    Tuesday, December 22, 2020 3:18 AM

All replies

  • User475983607 posted

    We cannot see your code but it sounds like a bug where you must wait for the cache to expire after adding a new item.  Either add the item to the cache manually or expire the cache.

    Tuesday, December 22, 2020 1:06 PM
  • User-939035612 posted

    I don't add the new post to the cache right away, that is why I know that the cache is not working because I should not see the new post on the home page for up to 30 minutes because the part of the cache with that cache key is set to expire every 30 minutes. 

    The ObjectDataSource on the home page gets its data from the following code

    public Posts.GetPostsPagedDataTable GetPostsPaged(int startRowIndex, int maximumRows, string search, int category, int subcategory, int state)
        {
            IAppCache cache = new CachingService(CachingService.DefaultCacheProvider) { DefaultCachePolicy = new CacheDefaults { DefaultCacheDurationSeconds = GlobalStatic.SITEPOSTSPAGEDCACHEDURATION() } };
            string cacheKey = String.Empty;
            if (search != null)
            {
                cacheKey = GlobalStatic.SITEGETPOSTSPAGEDCACHEKEY() + "-" + startRowIndex.ToString() + "-" + maximumRows.ToString() + "-" + HttpContext.Current.Server.HtmlEncode(search.ToString()) + "-" + category.ToString() + "-" + subcategory.ToString() + "-" + state.ToString();
            }
            else
            {
                cacheKey = GlobalStatic.SITEGETPOSTSPAGEDCACHEKEY() + "-" + startRowIndex.ToString() + "-" + maximumRows.ToString() + "-" + category.ToString() + "-" + subcategory.ToString() + "-" + state.ToString();
            }
            Func<Posts.GetPostsPagedDataTable> actionThatWeWantToCache = () => Adapter.GetPostsPaged(startRowIndex, maximumRows, search, category, subcategory, state);
            var CachedAdapter = cache.GetOrAdd(cacheKey, actionThatWeWantToCache);
            return CachedAdapter;
        }
        public int GetPostsCount(string search, int category, int subcategory, int state)
        {
            IAppCache cache = new CachingService(CachingService.DefaultCacheProvider) { DefaultCachePolicy = new CacheDefaults { DefaultCacheDurationSeconds = GlobalStatic.SITEPOSTSPAGEDCACHEDURATION() } };
            string cacheKey = String.Empty;
            if (search != null)
            {
                cacheKey = GlobalStatic.SITEGETPOSTSPAGEDCOUNTCACHEKEY() + "-" + HttpContext.Current.Server.HtmlEncode(search.ToString()) + "-" + category.ToString() + "-" + subcategory.ToString() + "-" + state.ToString();
            }
            else 
            {
                cacheKey = GlobalStatic.SITEGETPOSTSPAGEDCOUNTCACHEKEY() + "-" + category.ToString() + "-" + subcategory.ToString() + "-" + state.ToString();
            }
            Func<int> actionThatWeWantToCache = () => (int)Adapter.GetPostsCount(search, category, subcategory, state);
            var CachedAdapter = cache.GetOrAdd(cacheKey, actionThatWeWantToCache);
            return CachedAdapter;
        }

    When a post is created I remove some cache items based on their key value. That should result in those caches being cleared and rebuilt. When people fill out the form to create a new post this code runs:

    IAppCache cache = new CachingService();
            cache.Remove(GlobalStatic.SITEGETPOSTSBYCATEGORYCACHEKEY());
            cache.Remove(GlobalStatic.SITEGETPOSTSBYCATEGORYCOUNTCACHEKEY());
            cache.Remove(GlobalStatic.SITEGETPOSTSBYLOCATIONCACHEKEY());
            cache.Remove(GlobalStatic.SITEGETPOSTSBYLOCATIONCOUNTCACHEKEY());
            cache.Remove(GlobalStatic.SITEGETPOSTSBYTAGCACHEKEY());
            cache.Remove(GlobalStatic.SITEGETPOSTSBYTAGCOUNTCACHEKEY());
            int postid = (int)e.Command.Parameters["@postid"].Value;
            TextBox posttitle = (TextBox)FormView1.FindControl("posttitleTextBox");
            string posturl = BlastZoneURL.BZURL(postid.ToString(), posttitle.Text.ToString());
            Response.Redirect(posturl);

    That clears most caches so that they must refresh whenever a user visits those pages. However, the home page 

    Wednesday, December 23, 2020 1:08 AM
  • User475983607 posted

    I though the original issue was a newly added item does not show until the cache expires and refreshes from the database. 

    Your latest post seems to indicate the 30 timeout is expected.  Can you explain the problem you are tryin to solve?

    Wednesday, December 23, 2020 2:13 PM
  • User-939035612 posted

    The problem seems to be that the expected delay between adding a new record and seeing it show up on the site does not exist on the remote host. This leads me to believe that the site is not being cached the way that it should be.

    Thursday, December 31, 2020 10:19 AM
  • User-939850651 posted

    Hi CopBlaster,

    Could you show how you enabled the lazycache at your application? 

    In IIS for client cache you could use http response header feature Client Cache <clientCache> | Microsoft Docs

    Best regards,

    Xudong Peng

    Tuesday, January 5, 2021 9:53 AM
  • User-939035612 posted

    I installed the LazyCache Nuget package (https://github.com/alastairtree/LazyCache). Then I added it to classes where I wanted to cache data rather than require round trips to the database. The classes add LazyCache with a using directive at the top of the page:

    using LazyCache;

    Then I have something like the following for accessing a table adapter and then saving the table adapter object in the cache:

    public Posts.GetPostsPagedDataTable GetPostsPaged(int startRowIndex, int maximumRows, string search, int category, int subcategory, int state)
        {
            IAppCache cache = new CachingService(CachingService.DefaultCacheProvider) { DefaultCachePolicy = new CacheDefaults { DefaultCacheDurationSeconds = GlobalStatic.SITEPOSTSPAGEDCACHEDURATION() } };
            string cacheKey = String.Empty;
            if (search != null)
            {
                cacheKey = GlobalStatic.SITEGETPOSTSPAGEDCACHEKEY() + "-" + startRowIndex.ToString() + "-" + maximumRows.ToString() + "-" + HttpContext.Current.Server.HtmlEncode(search.ToString()) + "-" + category.ToString() + "-" + subcategory.ToString() + "-" + state.ToString();
            }
            else
            {
                cacheKey = GlobalStatic.SITEGETPOSTSPAGEDCACHEKEY() + "-" + startRowIndex.ToString() + "-" + maximumRows.ToString() + "-" + category.ToString() + "-" + subcategory.ToString() + "-" + state.ToString();
            }
            Func<Posts.GetPostsPagedDataTable> actionThatWeWantToCache = () => Adapter.GetPostsPaged(startRowIndex, maximumRows, search, category, subcategory, state);
            var CachedAdapter = cache.GetOrAdd(cacheKey, actionThatWeWantToCache);
            return CachedAdapter;
        }

    The problem again is that I do not see the delay on the server that I have on my desktop. That tells me that the cache must not be working right on the remote host because when I post a new post on the remote host it shows up on the home page right away even though LazyCache is only supposed to refresh that cache every half hour.

    The link in your previous response has nothing to do with LazyCache. LazyCache uses in memory caching not client caching.

    Sunday, January 10, 2021 9:54 AM