2011-01-31 3 views
2

私のオフィスでは、AppSettingsをWeb.Configのデータベースに配置することになっていると見なされています。このように、私は以下を作成しましたが、コードの2つの側面について疑問を抱いています。データベースとキャッシュからAppSettingsを入手する

だから私の質問です:それは新しいキャッシュオブジェクトを作成するため
UTILITYクラスで「キャッシュキャッシュは=新しいキャッシュを()」を含む行は、おそらく間違っています。

Q:だから、私はその行のために何をしていますか?

...助けていただければ幸いです。

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

...そしてそれがキャッシュまたは自動魔法のように、データベースから取得する必要があり:

全体的な目的は、次のように電話をかけることができるようにしました。

UTILITYコード:

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 
} 

ITEM-CLASSのCODE:

パブリッククラスコンフィギュ { #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 

}

+0

私たちはAppSettingsをデータベースに置く必要があると思われるのはなぜですか?アプリケーションの起動後にキャッシュされます。 – frennky

+0

@Frennky:コメントありがとう...私の上司はWEが2つの理由からデータベースからすべてのAppSettingsを取得しなければならないと判断しました。web.config内の何かを変更すると、アプリケーションから人々が起動します。人々はリリース上でweb.configを台無しにし続けました。このように、これは私の最初の試みです。だから...上の質問に対するコメントは素晴らしいだろう! –

答えて

1

あなたは正しく、問題を特定した - 現在のコードが作成されます新しいCacheのインスタンスは、キャッシュの目的を無効にするGetConfigurationValueへの各呼び出しでインスタンス化されます。そのたびに新しいインスタンスを作成するのではなく、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 

     ... 
    } 
} 
+0

ありがとうございました...私は今日それを試しています! –

+0

元々、私はすでに存在していたオブジェクトを指す必要があると思っていました。しかし、私はHttpContext.Current.Cacheが間違ったオブジェクトを渡して使用すると思っていたので、私はここで人々に頼んだ。再度、感謝します! –

0

補遺:
私がやった(実際には)何か他のものにキャッシュ変数を指すようにする必要があります。私がUtilityクラスで以下のことをするまではうまくいかないでしょう。

private static Cache cache = System.Web.HttpRuntime.Cache; 
関連する問題