【问题标题】:Sourcing AppSettings from database & cache从数据库和缓存中获取 AppSettings
【发布时间】:2011-01-31 15:53:58
【问题描述】:

在我的办公室,我们认为我们要将 Web.Config 的 AppSettings 放入数据库中。因此,我创建了以下代码,但对代码的几个方面有一些疑问。

所以我的问题是:
UTILITY 类中包含“Cache cache = new Cache()”的行可能是错误的,因为它创建了一个新的缓存对象。

问:那么,我应该为那条线做什么?

...感谢任何帮助。

总体目标是能够像这样拨打电话:

Utility.GetConfigurationValue(ConfigurationSection.AppSettings, "myVariable");

...让它自动从缓存或数据库中检索。

实用程序代码:

public static class Utility
{
    #region "Configurations"

    public static String GetConfigurationValue(ConfigurationSection section, String key)
    {
        Configurations config = new Configurations();
        Cache cache = new Cache(); // <--- This is probably wrong!!!!

        if (!cache.TryGetItemFromCache<Configurations>(out config))
        {
            config.List(SNCLavalin.US.Common.Enumerations.ConfigurationSection.AppSettings);
            cache.AddToCache<Configurations>(config, DateTime.Now.AddMinutes(15));
        }

        var result = (from record in config
                      where record.Key == key
                      select record).FirstOrDefault();

        return (result == null) ? null : result.Value;
    }

    #endregion
}

扩展代码:

public static class Extensions
{
    #region "System.Web.Caching"

    public static void Remove<T>(this Cache cache) where T : class
    {
        cache.Remove(typeof(T).Name);
    }
    public static void AddToCache<T>(this Cache cache, object item, DateTime absoluteExpiration) where T : class
    {
        T outItem = null;
        if (cache.TryGetItemFromCache<T>(out outItem))
            return;

        cache.Insert(typeof(T).Name,
                item,
                null,
                absoluteExpiration,
                System.Web.Caching.Cache.NoSlidingExpiration,
                System.Web.Caching.CacheItemPriority.Normal,
                null);
    }
    public static bool TryGetItemFromCache<T>(this Cache cache, out T item) where T : class
    {
        item = cache.Get(typeof(T).Name) as T;
        return item != null;
    }

    #endregion
}

列表类代码:

public class Configurations : List<Configuration>
{
    #region CONSTRUCTORS

    public Configurations() : base()
    {
        initialize();
    }
    public Configurations(int capacity) : base(capacity)
    {
        initialize();
    }
    public Configurations(IEnumerable<Configuration> collection) : base(collection)
    {
        initialize();
    }

    #endregion

    #region PROPERTIES & FIELDS

    private Crud _crud;

    #endregion

    #region EVENTS
    #endregion

    #region METHODS

    private void initialize()
    {
        _crud = new Crud("CurrentDbConnection");
    }

    public Configurations List(ConfigurationSection section)
    {
        using (DbCommand dbCommand = _crud.Db.GetStoredProcCommand("spa_LIST_SecConfiguration"))
        {
            _crud.Db.AddInParameter(dbCommand, "@Section", DbType.String, section.ToString());

            _crud.List(dbCommand, PopulateFrom);
        }

        return this;
    }

    public void PopulateFrom(DataTable table)
    {
        this.Clear();

        foreach (DataRow row in table.Rows)
        {
            Configuration instance = new Configuration();
            instance.PopulateFrom(row);
            this.Add(instance);
        }
    }

    #endregion
}

项目分类代码:

公共类配置 { #region 构造函数

public Configuration()
{
    initialize();
}

#endregion

#region PROPERTIES & FIELDS

private Crud _crud;

public string Section { get; set; }
public string Key { get; set; }
public string Value { get; set; }

#endregion

#region EVENTS
#endregion

#region METHODS

private void initialize()
{
    _crud = new Crud("CurrentDbConnection");
    Clear();
}

public void Clear()
{
    this.Section = "";
    this.Key = "";
    this.Value = "";
}
public void PopulateFrom(DataRow row)
{
    Clear();

    this.Section = row["Section"].ToString();
    this.Key = row["Key"].ToString();
    this.Value = row["Value"].ToString();
}

#endregion

}

【问题讨论】:

  • 为什么认为我们应该将我们的AppSettings放入数据库?它在应用程序启动后被缓存。
  • @Frennky:感谢您的评论...我的老板认为我们必须从数据库中获取所有 AppSettings 有两个原因:更改 web.config 中的任何内容都会让人们从应用程序中启动;人们一直在搞砸发布时的 web.config。因此,这是我第一次尝试解决这个问题。所以...关于上述问题的任何 cmets 都会很棒!

标签: c# asp.net caching refactoring web-config


【解决方案1】:

您已正确识别问题 - 当前代码在每次调用 GetConfigurationValue 时创建一个新的 Cache 实例,这违背了缓存的目的。您需要将 Cache 实例设为静态,而不是每次都创建一个新实例。

public static class Utility
{
    private static Cache cache = new Cache();  // Static class variable

    #region "Configurations"

    public static String GetConfigurationValue(ConfigurationSection section, String key)
    {
        Configurations config = new Configurations();
        // Cache cache = new Cache(); --- removed

        ...
    }
}

【讨论】:

  • 非常感谢...我今天会试试的!
  • 最初,我认为我需要指向一个已经存在的对象。但我认为 HttpContext.Current.Cache 将是传入和使用的错误对象......所以我在这里提示人们。再次感谢!
【解决方案2】:

答案附录:
我确实(实际上)需要将缓存变量指向其他东西。在我在 Utility 类中执行以下操作之前,它不会起作用。

private static Cache cache = System.Web.HttpRuntime.Cache;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-24
    • 2020-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多