キャッシュにアイテムを手動で追加するときに、キャッシュプロファイルを使用する方法はありますか?

StackOverflow https://stackoverflow.com/questions/4761657

質問

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

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

代わりに、手動で追加されたアイテムのキャッシュプロファイルをリストするために、独自の構成セクションを構築できます。

役に立ちましたか?

解決

これを行う出力キャッシュプロファイルのリストを取得できます。

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
    }

他のヒント

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top