Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jeffrey Schoemaker 408 posts 2138 karma points MVP 8x c-trib
    Jul 19, 2013 @ 11:17
    Jeffrey Schoemaker
    0

    Caching a macro

    Hi all,

    I'm getting a headache of the caching in my Umbraco project. Hopefully someone get help me :)

    I have a multi-lingual website and I want to output cache the dropdownlist with countries in it. I've wrapped the usercontrol into a macro, but I don't want to cache it by page, but I would like to do it by country. I don't think the macro caching supports this out of the box?

    Then I tried outputcaching the usercontrol with code like this

    <%@ OutputCache Duration="60" VaryByParam="None" VaryByCustom="System.Globalization.CultureInfo.CurrentCulture" %>

    I think I tried every combination that I could imagine but it still isn't working.

    Does anyone has a hint?

    Thanks in advance,

    Jeffrey

  • Peter Duncanson 430 posts 1360 karma points c-trib
    Jul 19, 2013 @ 14:26
    Peter Duncanson
    0

    Hi Jeffery,

    I don't think you can get the caching to work out the box for what you are wanting (not that I know of anyway). I'd be tempted to change your code (you mention a usercontrol so I'll assume you have custom code) to cache the part you think is taking so long. Lets say its the getting and sorting of the languages to rendering in your dropdown thats the issue. You could do this work once (again lets assume you generate a List<Country> for instance, then cache that list in memory. On return visits just check the cache to get the list back. The rendering itself is normally very quick, the slow part is getting the data I find. 

    private object cacheLock = new object();
    
    public List<Country> GetLanguages {
        var cacheKey = "AllLanguages";
        var languages = (List<Country>) HttpRuntime.Cache[cacheKey];
    
        // Was there anything in the cache?
        if ( languages == null ) {
            // Lock the cache to prevent others from rerunning this work (not that essential if its not mission critical)
            lock( cacheLock ) {
                // Double check another thread/request has not already started reloading this
                if ( languages = null ) {
                    // Noting cached yet so load it up
    
                    // Do the work to get the languages
                    languages = SomeAmazingCodeThatTakesSomeEffortToDo();
    
                    // Save it to the Cache for 5 minutes, check out CacheDependancy for some awesome options (http://msdn.microsoft.com/en-us/library/system.web.caching.cachedependency.aspx)
                    HttpRuntime.Cache.Add( cacheKey, languages, null, DateTime.Now.AddMinutes(5), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
                }
            }
        }
    
        return languages;
    } 
Please Sign in or register to post replies

Write your reply to:

Draft