/// <summary> /// Get setting value by key /// </summary> /// <typeparam name="T">Type</typeparam> /// <param name="key">Key</param> /// <param name="defaultValue">Default value</param> /// <param name="storeId">Store identifier</param> /// <returns>Setting value</returns> public virtual T GetSettingByKey <T>(string key, T defaultValue = default, string storeId = "") { if (String.IsNullOrEmpty(key)) { return(defaultValue); } string keyCache = string.Format(CacheKey.SETTINGS_BY_KEY, key, storeId); return(_cacheBase.Get <T>(keyCache, () => { var settings = GetSettingsByName(key); key = key.Trim().ToLowerInvariant(); if (settings.Any()) { var setting = settings.FirstOrDefault(x => x.StoreId == storeId); //load shared value? if (setting == null && !String.IsNullOrEmpty(storeId)) { setting = settings.FirstOrDefault(x => x.StoreId == ""); } if (setting != null) { return JsonSerializer.Deserialize <T>(setting.Metadata); } } return defaultValue; })); }
public UserInfo GetUser(string userId) { try { return(_cache.Get <UserInfo>(GetUserKey(userId))); } catch (Exception) { return(null); } }
/// <summary> /// Gets a resource string based on the specified ResourceKey property. /// </summary> /// <param name="name">A string representing a name.</param> /// <param name="languageId">Language identifier</param> /// <param name="defaultValue">Default value</param> /// <returns>A string representing the requested resource string.</returns> public virtual string GetResource(string name, string languageId, string defaultValue = "", bool returnEmptyIfNotFound = false) { string result = string.Empty; if (name == null) { name = string.Empty; } name = name.Trim().ToLowerInvariant(); if (_allTranslateResource != null) { if (_allTranslateResource.ContainsKey(name)) { result = _allTranslateResource[name].Value; } } else { string key = string.Format(CacheKey.TRANSLATERESOURCES_ALL_KEY, languageId); _allTranslateResource = _cacheBase.Get(key, () => { var dictionary = new Dictionary <string, TranslationResource>(); var locales = GetAllResources(languageId); foreach (var locale in locales) { var resourceName = locale.Name.ToLowerInvariant(); if (!dictionary.ContainsKey(resourceName)) { dictionary.Add(resourceName.ToLowerInvariant(), locale); } else { _translationRepository.Delete(locale); } } return(dictionary); }); if (_allTranslateResource.ContainsKey(name)) { result = _allTranslateResource[name].Value; } } if (string.IsNullOrEmpty(result)) { if (!string.IsNullOrEmpty(defaultValue)) { result = defaultValue; } else { if (!returnEmptyIfNotFound) { result = name; } } } return(result); }
public async Task Invoke( HttpContext context, ICacheBase cacheBase, IRepository <GrandNodeVersion> repository) { if (context == null || context.Request == null) { await _next(context); return; } var version = cacheBase.Get(CacheKey.GRAND_NODE_VERSION, () => repository.Table.FirstOrDefault()); if (version == null) { await context.Response.WriteAsync($"The database does not exist."); return; } if (!version.DataBaseVersion.Equals(GrandVersion.SupportedDBVersion)) { await context.Response.WriteAsync($"The database version is not supported in this software version. " + $"Supported version: {GrandVersion.SupportedDBVersion} , your version: {version.DataBaseVersion}"); } else { await _next(context); } }
/// <summary> /// Gets all stores /// </summary> /// <returns>Stores</returns> public virtual IList <Store> GetAll() { if (_allStores == null) { _allStores = _cacheBase.Get(CacheKey.STORES_ALL_KEY, () => { return(_storeRepository.Table.OrderBy(x => x.DisplayOrder).ToList()); }); } return(_allStores); }
/// <summary> /// Gets all stores /// </summary> /// <returns>Stores</returns> public virtual IList <Store> GetAll() { if (_allStores == null) { _allStores = _cacheBase.Get(CacheKey.STORES_ALL_KEY, () => { return(_storeRepository.Collection.Find(new BsonDocument()).SortBy(x => x.DisplayOrder).ToList()); }); } return(_allStores); }
public async Task <decimal?> Handle(GetPriceByCustomerProductQuery request, CancellationToken cancellationToken) { var key = string.Format(CacheKey.CUSTOMER_PRODUCT_PRICE_KEY_ID, request.CustomerId, request.ProductId); var productprice = _cacheBase.Get(key, () => { var pp = _customerProductPriceRepository.Table .Where(x => x.CustomerId == request.CustomerId && x.ProductId == request.ProductId) .FirstOrDefault(); if (pp == null) { return(null, false); }
/// <summary> /// Gets all settings /// </summary> /// <returns>Settings</returns> protected virtual IDictionary <string, IList <SettingForCaching> > GetAllSettingsCached() { if (_allSettings != null) { return(_allSettings); } //cache string key = string.Format(CacheKey.SETTINGS_ALL_KEY); _allSettings = _cacheBase.Get(key, () => { //we use no tracking here for performance optimization //anyway records are loaded only for read-only operations var query = from s in _settingRepository.Table orderby s.Name, s.StoreId select s; var settings = query.ToList(); var dictionary = new Dictionary <string, IList <SettingForCaching> >(); foreach (var s in settings) { var resourceName = s.Name.ToLowerInvariant(); var settingForCaching = new SettingForCaching { Id = s.Id, Name = s.Name, Value = s.Value, StoreId = s.StoreId }; if (!dictionary.ContainsKey(resourceName)) { //first setting dictionary.Add(resourceName, new List <SettingForCaching> { settingForCaching }); } else { //already added //most probably it's the setting with the same name but for some certain store (storeId > 0) dictionary[resourceName].Add(settingForCaching); } } return(dictionary); }); return(_allSettings); }
public List <ArticleEntity> GetList() { try { var key = KeyCahe.GenCacheKey("GetListArticles"); var listArticle = cacheBase.Get <List <ArticleEntity> >(key); if (listArticle == null) { listArticle = articlesBo.GetList(); cacheBase.Add <List <ArticleEntity> >(listArticle, key); } return(listArticle); } catch (Exception ex) { return(null); } }
/// <summary> /// Gets a resource string based on the specified ResourceKey property. /// </summary> /// <param name="resourceKey">A string representing a ResourceKey.</param> /// <param name="languageId">Language identifier</param> /// <param name="logIfNotFound">A value indicating whether to log error if locale string resource is not found</param> /// <param name="defaultValue">Default value</param> /// <param name="returnEmptyIfNotFound">A value indicating whether an empty string will be returned if a resource is not found and default value is set to empty string</param> /// <returns>A string representing the requested resource string.</returns> public virtual string GetResource(string resourceKey, string languageId, bool logIfNotFound = true, string defaultValue = "", bool returnEmptyIfNotFound = false) { string result = string.Empty; if (resourceKey == null) { resourceKey = string.Empty; } resourceKey = resourceKey.Trim().ToLowerInvariant(); if (_localizationSettings.LoadAllLocaleRecordsOnStartup) { //load all records (cached) if (_alllocaleStringResource != null) { if (_alllocaleStringResource.ContainsKey(resourceKey.ToLowerInvariant())) { result = _alllocaleStringResource[resourceKey.ToLowerInvariant()].ResourceValue; } } else { string key = string.Format(CacheKey.LOCALSTRINGRESOURCES_ALL_KEY, languageId); _alllocaleStringResource = _cacheBase.Get(key, () => { var dictionary = new Dictionary <string, LocaleStringResource>(); var locales = GetAllResources(languageId); foreach (var locale in locales) { var resourceName = locale.ResourceName.ToLowerInvariant(); if (!dictionary.ContainsKey(resourceName)) { dictionary.Add(resourceName.ToLowerInvariant(), locale); } else { _lsrRepository.Delete(locale); } } return(dictionary); }); if (_alllocaleStringResource.ContainsKey(resourceKey.ToLowerInvariant())) { result = _alllocaleStringResource[resourceKey.ToLowerInvariant()].ResourceValue; } } } else { //gradual loading string key = string.Format(CacheKey.LOCALSTRINGRESOURCES_BY_RESOURCENAME_KEY, languageId, resourceKey); string lsr = _cacheBase.Get(key, () => { var builder = Builders <LocaleStringResource> .Filter; var filter = builder.Eq(x => x.LanguageId, languageId); filter = filter & builder.Eq(x => x.ResourceName, resourceKey.ToLowerInvariant()); return(_lsrRepository.Collection.Find(filter).FirstOrDefault()?.ResourceValue); }); if (lsr != null) { result = lsr; } } if (string.IsNullOrEmpty(result)) { if (logIfNotFound) { _logger.Warning(string.Format("Resource string ({0}) is not found. Language ID = {1}", resourceKey, languageId)); } if (!string.IsNullOrEmpty(defaultValue)) { result = defaultValue; } else { if (!returnEmptyIfNotFound) { result = resourceKey; } } } return(result); }
public ConfigurationViewModel GetConfiguration() { return(_cache.Get <ConfigurationViewModel>(_ConfigurationKey)); }