Example #1
0
        public async Task <string> GetResource(string key, string culture, string category, IResourceProvider provider, ILocalStorageService localStorageService)
        {
            var comparer = StringComparer.OrdinalIgnoreCase;

            if (string.IsNullOrWhiteSpace(category) && _options.FallbackCategory != null)
            {
                category = _options.FallbackCategory;
            }

            if (_options.MemoryCacheDisabled)
            {
                var categoryResult = await provider.GetCategoryResources(category, culture);

                categoryResult = new Dictionary <string, string>(categoryResult, comparer);
                if (categoryResult.TryGetValue(key, out var res))
                {
                    return(res);
                }
                else
                {
                    await provider.AddMissingKey(category, culture, key);

                    return(key);
                }
            }

            var categoryKey = $"{category}-{culture}";


            await _semaphore.WaitAsync();

            var cachedCultureCategory = _cache.SingleOrDefault(x => x.Category == category && x.Culture == culture);

            if (cachedCultureCategory == null)
            {
                if (!_options.LocalStorageOptions.CacheDisabled)
                {
                    var cachedCategory = await localStorageService.GetItemAsync <CultureCategoryResources>(categoryKey);

                    if (cachedCategory != null)
                    {
                        if (cachedCategory.UpdatedTime.Add(_options.LocalStorageOptions.CacheInvalidation) > DateTime.UtcNow)
                        {
                            cachedCultureCategory = cachedCategory;
                        }
                    }
                }

                if (cachedCultureCategory == null)
                {
                    cachedCultureCategory = _cache.SingleOrDefault(x => x.Category == category && x.Culture == culture);
                    if (cachedCultureCategory == null)
                    {
                        var resources = await provider.GetCategoryResources(category, culture);

                        resources             = new Dictionary <string, string>(resources, comparer);
                        cachedCultureCategory = new CultureCategoryResources
                        {
                            Category    = category,
                            Culture     = culture,
                            Resources   = resources,
                            UpdatedTime = DateTime.UtcNow
                        };
                        _cache.Add(cachedCultureCategory);
                        if (!_options.LocalStorageOptions.CacheDisabled)
                        {
                            await localStorageService.SetItemAsync(categoryKey, cachedCultureCategory);
                        }
                    }
                }
            }

            cachedCultureCategory.Resources = new Dictionary <string, string>(cachedCultureCategory.Resources, comparer);
            if (cachedCultureCategory.Resources.TryGetValue(key, out var result))
            {
                _semaphore.Release();
                return(result);
            }
            if (cachedCultureCategory.Resources.TryAdd(key, key))
            {
                if (!_options.LocalStorageOptions.CacheDisabled)
                {
                    await localStorageService.SetItemAsync(categoryKey, cachedCultureCategory);
                }
            }
            _semaphore.Release();
            await provider.AddMissingKey(category, culture, key);

            return(key);
        }