예제 #1
0
        private async Task SetCacheItemsAsync(
            string providerName,
            string providerKey,
            string currentName,
            SettingCacheItem currentCacheItem)
        {
            var settingDefinitions = SettingDefinitionManager.GetAll();
            var settingsDictionary = (await SettingRepository.GetListAsync(providerName, providerKey))
                                     .ToDictionary(s => s.Name, s => s.Value);

            var cacheItems = new List <KeyValuePair <string, SettingCacheItem> >();

            foreach (var settingDefinition in settingDefinitions)
            {
                var settingValue = settingsDictionary.GetOrDefault(settingDefinition.Name);

                cacheItems.Add(
                    new KeyValuePair <string, SettingCacheItem>(
                        CalculateCacheKey(settingDefinition.Name, providerName, providerKey),
                        new SettingCacheItem(settingValue)
                        )
                    );

                if (settingDefinition.Name == currentName)
                {
                    currentCacheItem.Value = settingValue;
                }
            }

            await Cache.SetManyAsync(cacheItems, considerUow : true);
        }
예제 #2
0
        public async Task Cache_Should_Invalidator_WhenSettingChanged_Between_Tenant_And_Host()
        {
            var tenantId = Guid.NewGuid();

            using (_currentTenant.Change(tenantId))
            {
                // GetOrNullAsync will cache language.
                await _settingManagementStore
                .GetOrNullAsync("MySetting2", GlobalSettingValueProvider.ProviderName, null)
                ;
            }

            using (_currentTenant.Change(null))
            {
                // SetAsync will make cache invalid.
                await _settingManagementStore
                .SetAsync("MySetting2", "MySetting2Value", GlobalSettingValueProvider.ProviderName, null);
            }

            using (_currentTenant.Change(tenantId))
            {
                // Assert
                (await _cache.GetAsync(
                     SettingCacheItem.CalculateCacheKey("MySetting2", GlobalSettingValueProvider.ProviderName, null))
                ).ShouldBeNull();
            }
        }
예제 #3
0
        public void GetSettingNameFormCacheKeyOrNull()
        {
            var key = SettingCacheItem.CalculateCacheKey("aaa", "bbb", "ccc");

            SettingCacheItem.GetSettingNameFormCacheKeyOrNull(key).ShouldBe("aaa");
            SettingCacheItem.GetSettingNameFormCacheKeyOrNull("aaabbbccc").ShouldBeNull();
        }
예제 #4
0
        public async Task GetOrNullAsync_Should_Cached()
        {
            // Act
            (await _cache.GetAsync(SettingCacheItem.CalculateCacheKey("MySetting2", UserSettingValueProvider.ProviderName, _testData.User1Id.ToString()))).ShouldBeNull();
            await _settingManagementStore.GetOrNullAsync("MySetting2", UserSettingValueProvider.ProviderName, _testData.User1Id.ToString());

            (await _cache.GetAsync(SettingCacheItem.CalculateCacheKey("MySetting2", UserSettingValueProvider.ProviderName, _testData.User1Id.ToString()))).ShouldNotBeNull();
        }
예제 #5
0
        public async Task Cache_Should_Invalidator_WhenSettingChanged()
        {
            // Arrange
            // GetOrNullAsync will cache language.
            await _settingManagementStore.GetOrNullAsync("MySetting2", UserSettingValueProvider.ProviderName, _testData.User1Id.ToString());

            // Act
            var lang = await _settingRepository.FindAsync("MySetting2", UserSettingValueProvider.ProviderName, _testData.User1Id.ToString());

            await _settingRepository.DeleteAsync(lang);

            // Assert
            (await _cache.GetAsync(
                 SettingCacheItem.CalculateCacheKey("MySetting2", UserSettingValueProvider.ProviderName, _testData.User1Id.ToString()))).ShouldBeNull();
        }
예제 #6
0
        protected virtual async Task <SettingCacheItem> GetCacheItemAsync(string name, string providerName, string providerKey)
        {
            var cacheKey  = CalculateCacheKey(name, providerName, providerKey);
            var cacheItem = await Cache.GetAsync(cacheKey, considerUow : true);

            if (cacheItem != null)
            {
                return(cacheItem);
            }

            cacheItem = new SettingCacheItem(null);

            await SetCacheItemsAsync(providerName, providerKey, name, cacheItem);

            return(cacheItem);
        }
예제 #7
0
        protected virtual async Task <SettingCacheItem> GetCacheItemAsync(string name, string providerName, string providerKey)
        {
            var cacheKey  = CalculateCacheKey(name, providerName, providerKey);
            var cacheItem = await Cache.GetAsync(cacheKey).ConfigureAwait(false);

            if (cacheItem != null)
            {
                return(cacheItem);
            }

            var setting = await SettingRepository.FindAsync(name, providerName, providerKey).ConfigureAwait(false);

            cacheItem = new SettingCacheItem(setting?.Value);

            await Cache.SetAsync(
                cacheKey,
                cacheItem
                ).ConfigureAwait(false);

            return(cacheItem);
        }
예제 #8
0
 protected virtual string CalculateCacheKey(string name, string providerName, string providerKey)
 {
     return(SettingCacheItem.CalculateCacheKey(name, providerName, providerKey));
 }
예제 #9
0
 protected virtual string GetSettingNameFormCacheKeyOrNull(string key)
 {
     //TODO: throw ex when name is null?
     return(SettingCacheItem.GetSettingNameFormCacheKeyOrNull(key));
 }