2

web.configの出力キャッシュプロファイルを使用して構成されたアプリケーションの出力キャッシュがあります。キャッシュを必要とするすべての出力アイテムにキャッシュを設定し、すべてのキャッシュ設定を1か所で調整できると非常に便利です。

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

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

4

2 に答える 2

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
    }
于 2011-01-21T18:06:25.517 に答える
0

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

于 2011-01-21T17:37:43.967 に答える