예제 #1
0
        private bool GetFromDatabaseSubscription <TValue>(OptionDefinition <TValue> pref, int subId, out TValue value)
        {
            value = default;
            if (pref.Key == null)
            {
                return(false);
            }

            var dbPref = dataContext.SubscriptionOptions.Find(pref.Key, subId);

            if (dbPref == null)
            {
                return(false);
            }

            value = JsonSerializer.Deserialize <TValue>(dbPref.Value);
            return(true);
        }
예제 #2
0
        private bool GetFromDatabaseUser <TValue>(OptionDefinition <TValue> pref, string userId, out TValue value)
        {
            value = default;
            if (pref.Key == null)
            {
                return(false);
            }

            var dbPref = dataContext.UserOptions.Find(pref.Key, userId);

            if (dbPref == null)
            {
                return(false);
            }

            value = JsonSerializer.Deserialize <TValue>(dbPref.Value);
            return(true);
        }
예제 #3
0
        /// <summary>
        /// Gets the value of a global option.
        /// </summary>
        /// <remarks>
        /// Options are resolved in this order: cache, database, environment variable, appsettings.json, default value
        /// </remarks>
        /// <typeparam name="TValue">Data type of option</typeparam>
        /// <param name="pref">Option definition</param>
        /// <returns>Option value</returns>
        public TValue GetGlobal <TValue>(OptionDefinition <TValue> pref)
        {
            // Correct order is: database, environment variable, appsettings.json, default
            // No need to update the cache here
            if (globalCache.Get(pref.Key, out TValue value))
            {
                return(value);
            }

            bool found = GetFromDatabase(pref, out value) || GetFromEnvironment(pref, out value);

            if (!found)
            {
                value = GetFromConfiguration(pref, pref.DefaultValue);
            }

            // Update cache
            globalCache.Set(pref.Key, value);
            return(value);
        }
예제 #4
0
 public bool GetForSubscriptionFolderNoResolve <TValue>(OptionDefinition <TValue> pref, int folderId, out TValue value)
 {
     return(GetFromDatabaseFolder(pref, folderId, out value));
 }
예제 #5
0
 public bool GetForSubscriptionNoResolve <TValue>(OptionDefinition <TValue> pref, int subId, out TValue value)
 {
     return(GetFromDatabaseSubscription(pref, subId, out value));
 }
예제 #6
0
 public void SetForSubscription <TValue>(OptionDefinition <TValue> pref, int subId, TValue value)
 {
     subCache.Set(new SubscriptionCacheKey(subId, pref.Key), value);
     SetInDatabaseSubscription(pref, subId, value);
 }