Copied to clipboard

Flag this post as spam?

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


  • David Peck 587 posts 1554 karma points c-trib
    Mar 06, 2018 @ 12:00
    David Peck
    0

    FYI: Pre-caching the single language dictionary

    This is not a question, it's info in case anyone ever has the same issue as me.

    I use the dictionary to save what should be static text (e.g. 'Title', 'First name', 'Surname'), even though we only have one language. It helps bridge the gap between putting everything in the CMS, which would cause confusion, and having complaints that certain text can't be changed. When we which to use 'Last name' instead of 'Surname' it is no problem.

    Unfortunately the dictionary items aren't cached. They're lazy loaded using individual database requests. This means that when your site starts up it is going to start making loads of database request. To resolve this I've create a dictionary what preloads all the dictionary items on start-up. Perhaps it will be a useful addition to someone else who uses the dictionary in the same way.

    https://gist.github.com/anonymous/1f55bfcd92b74092a6687d38fb186542

    NB: This will not work as it is for multiple languages, though you'd only need to modify this to have an array of dictionaries (one per language) to do so.

  • David Peck 587 posts 1554 karma points c-trib
    Mar 06, 2018 @ 12:22
    David Peck
    102

    Issue with that gist code. You'll end up with a new dictionary for every UmbracoHelper. This use this instead for the factory:

    public class PreCachedDictionaryFactory : ICultureDictionaryFactory
    {
        private ICultureDictionary _dictionary;
    
        public ICultureDictionary CreateDictionary()
        {
            if (_dictionary == null)
            {
                var dic = new PreCachedDictionary();
                dic.Preload();
                _dictionary = dic;
            }
    
            return _dictionary;
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft