private static string GetCacheKey(string langtag) { //return string.Format("po:{0}", langtag).ToLowerInvariant(); // The above will cause a new string to be allocated. // So subsituted with the following code. // Obtain the cache key without allocating a new string (except for first time). // As this method is a high-frequency method, the overhead in the (lock-free) dictionary // lookup is thought to outweigh the potentially large number of temporary string allocations // and the consequently hastened garbage collections. return(LanguageTag.GetCachedInstance(langtag).GlobalKey); }
public virtual ConcurrentDictionary <string, LanguageTag> GetAppLanguages() { ConcurrentDictionary <string, LanguageTag> AppLanguages = (ConcurrentDictionary <string, LanguageTag>)System.Web.HttpRuntime.Cache["i18n.AppLanguages"]; if (AppLanguages != null) { return(AppLanguages); } lock (Sync) { AppLanguages = (ConcurrentDictionary <string, LanguageTag>)System.Web.HttpRuntime.Cache["i18n.AppLanguages"]; if (AppLanguages != null) { return(AppLanguages); } AppLanguages = new ConcurrentDictionary <string, LanguageTag>(); // Insert into cache. // NB: we do this before actually populating the collection. This is so that any changes to the // folders before we finish populating the collection will cause the cache item to be invalidated // and hence reloaded on next request, and so will not be missed. System.Web.HttpRuntime.Cache.Insert("i18n.AppLanguages", AppLanguages, _translationRepository.GetCacheDependencyForAllLanguages()); // Populate the collection. List <string> languages = _translationRepository.GetAvailableLanguages().Select(x => x.LanguageShortTag).ToList(); // Ensure default language is included in AppLanguages where appropriate. if (LocalizedApplication.Current.MessageKeyIsValueInDefaultLanguage && !languages.Any(x => LocalizedApplication.Current.DefaultLanguageTag.Equals(x))) { languages.Add(LocalizedApplication.Current.DefaultLanguageTag.ToString()); } foreach (var langtag in languages) { if (IsLanguageValid(langtag)) { AppLanguages[langtag] = LanguageTag.GetCachedInstance(langtag); } } // Done. return(AppLanguages); } }