using System; using System.Linq; using System.Runtime.Caching; using Neo.LegitimateLicences.Data.Models; namespace Neo.LegitimateLicences.Common { public static class SettingsHelper { private static readonly ObjectCache Cache = MemoryCache.Default; public static bool IsEnabled(string settingName, bool defaultValue = false) { var cacheKey = "setting:" + settingName; if (Cache.Contains(cacheKey)) { return (bool)Cache.Get(cacheKey); } var value = defaultValue; try { var db = new LegitimateDatabase(); var raw = db.Context.Database.SqlQuery( "SELECT SettingValue FROM [System].[Settings] WHERE SettingName = @p0", new object[] { settingName } ).FirstOrDefault(); if (!string.IsNullOrWhiteSpace(raw)) { value = string.Equals(raw, "TRUE", StringComparison.OrdinalIgnoreCase) || string.Equals(raw, "1"); } } catch { } Cache.Set(cacheKey, value, DateTimeOffset.UtcNow.AddSeconds(60)); return value; } } }