2011-01-21 13 views
2

web.configの出力キャッシュプロファイルを使用してアプリケーションに出力キャッシュを設定しました。それを必要とするすべての出力項目にキャッシュを設定し、すべてのキャッシュ設定を1か所で調整できるようにすることは非常に便利です。アイテムをキャッシュに手動で追加するときにキャッシュプロファイルを使用する方法はありますか?

しかし、私はまた、特定のアイテムのデータとロジックレイヤにキャッシュを実装しています。キャッシュするデータとロジック項目のキャッシュパラメータをハードコーディングするのではなく、プロファイルを参照することもできますが、Insert()メソッドのプロファイルを参照する方法はないようですキャッシュオブジェクト。

また、自分で設定したセクションを作成して、手動で追加したアイテムのキャッシュプロファイルを一覧表示することもできます。

答えて

3

あなたはこれをやって、あなたの出力キャッシュプロファイルのリストを取得することができます。

private Dictionary<string, OutputCacheProfile> _outputCacheProfiles; 
/// <summary> 
/// Initializes <see cref="OutputCacheProfiles"/> using the settings found in 
/// "system.web\caching\outputCacheSettings" 
/// </summary> 
void InitializeOutputCacheProfiles(
      System.Configuration.Configuration appConfig, 
      NameValueCollection providerConfig) 
{ 
    _outputCacheProfiles = new Dictionary<string, OutputCacheProfile>(); 

    OutputCacheSettingsSection outputCacheSettings = 
      (OutputCacheSettingsSection)appConfig.GetSection("system.web/caching/outputCacheSettings"); 

    if(outputCacheSettings != null) 
    { 
     foreach(OutputCacheProfile profile in outputCacheSettings.OutputCacheProfiles) 
     { 
      _outputCacheProfiles[profile.Name] = profile; 
     } 
    } 
} 

そして、あなたの挿入時にそれを使用する:

/// <summary> 
/// Gets the output cache profile with the specified name 
/// </summary> 
public OutputCacheProfile GetOutputCacheProfile(string name) 
{ 
    if(!_outputCacheProfiles.ContainsKey(name)) 
    { 
     throw new ArgumentException(String.Format("The output cache profile '{0}' is not registered", name)); 
    } 
    return _outputCacheProfiles[name]; 
} 

    /// <summary> 
    /// Inserts the key/value pair using the specifications of the output cache profile 
    /// </summary> 
    public void InsertItemUsing(string outputCacheProfileName, string key, object value) 
    { 
     OutputCacheProfile profile = GetOutputCacheProfile(outputCacheProfileName); 
     //Get settings from profile to use on your insert instead of hard coding them 
    } 
0

C#のCache.Insertオブジェクトを参照する場合、後でプロファイルを取得するときにキャッシュから抽出できる対応するGUIDをすべてのプロファイルに持つように、GUIDをキーに追加できます。