public void CacheProvider_Fetch_PassComplexKey_GetResource_AreEqualExpectedValue( TranslationTypeEnum translationType) { //arrange InitTranslationRepo(_expectedValues[0]); var cacheProvider = new CacheProvider(_staticTranslationsRepository, _dynamicTranslationsRepository, _mapObjRepositoryTranslations, null); //act string result = cacheProvider.Fetch(translationType, _complexCacheKey); //assert Assert.AreEqual(_expectedValues[0], result); }
public void CacheProvider_Fetch_PassEmptyKey_GetEmptyString_AreEqualExpectedValue( TranslationTypeEnum translationType) { //arrange InitTranslationRepo(String.Empty); var cacheProvider = new CacheProvider(_staticTranslationsRepository, _dynamicTranslationsRepository, _mapObjRepositoryTranslations, null); //act string result = cacheProvider.Fetch(translationType, String.Empty); //assert Assert.AreEqual(String.Empty, result); }
/// <summary> /// Fetches the specified translation type. /// </summary> /// <param name="translationType">Type of the translation.</param> /// <param name="cacheKey">The cache key.</param> /// <returns> /// The data through the cache. /// </returns> public string Fetch(TranslationTypeEnum translationType, string cacheKey) { return FetchAndCache(translationType, cacheKey); }
/// <summary> /// Tries to get value from cache. /// </summary> /// <param name="key">The key.</param> /// <param name="value">The value.</param> /// <param name="translationType"></param> /// <returns> /// The result of trying to get value from cache. /// </returns> private bool TryGetValue(string key, out string value, TranslationTypeEnum translationType) { var cachedValue = new object(); switch (translationType) { case TranslationTypeEnum.StaticTranslation: cachedValue = _staticObjectCache.Get(key); break; case TranslationTypeEnum.DynamicTranslation: cachedValue = _dynamicObjectCache.Get(key); break; case TranslationTypeEnum.MapObjectTranslation: cachedValue = _mapObjectCache.Get(key); break; } value = cachedValue as String; if (value != null) { return true; } value = String.Empty; return false; }
/// <summary> /// Fetches and cache data. /// </summary> /// <param name="translationType">Type of the translation.</param> /// <param name="cacheKey">The cache key.</param> /// <returns> /// The data through the cache. /// </returns> private string FetchAndCache(TranslationTypeEnum translationType, string cacheKey) { if (cacheKey.Length < 3) { return String.Empty; } string value; if (!TryGetValue(cacheKey, out value, translationType)) { string culture = cacheKey.Substring(0, 2); string dbKey = cacheKey.Substring(3); switch (translationType) { case TranslationTypeEnum.StaticTranslation: value = _staticTranslationsRepository.GetValue(dbKey, culture); break; case TranslationTypeEnum.DynamicTranslation: value = _dynamicTranslationsRepository.GetValue(dbKey, culture); break; case TranslationTypeEnum.MapObjectTranslation: value = _mapObjectTranslationsRepository.GetValue(dbKey, culture); break; } if (value == null) { value = String.Empty; } //Add data to the cache switch (translationType) { case TranslationTypeEnum.StaticTranslation: _staticObjectCache.Add(cacheKey, value, _policy); break; case TranslationTypeEnum.DynamicTranslation: _dynamicObjectCache.Add(cacheKey, value, _policy); break; case TranslationTypeEnum.MapObjectTranslation: _mapObjectCache.Add(cacheKey, value, _policy); break; } } return value; }
/// <summary> /// Resets the cache object. /// </summary> /// <param name="translationType">Type of the translation data.</param> public void ResetCacheObject(TranslationTypeEnum translationType) { switch (translationType) { case TranslationTypeEnum.StaticTranslation: _staticObjectCache = new MemoryCache("static"); break; case TranslationTypeEnum.DynamicTranslation: _dynamicObjectCache = new MemoryCache("dynamic"); break; case TranslationTypeEnum.MapObjectTranslation: _mapObjectCache = new MemoryCache("map"); break; } }
/// <summary> /// Fetches the specified translation type. /// </summary> /// <param name="translationType">Type of the translation.</param> /// <param name="complexCacheKey">The complex cache key.</param> /// <returns> /// The data through the cache. /// </returns> public string Fetch(TranslationTypeEnum translationType, string[] complexCacheKey) { return FetchAndCache(translationType, CacheKeyGenerator.ComposeKey(complexCacheKey)); }