我在使用web.config中的輸出緩存配置文件配置的應用程序中輸出緩存。能夠在需要它的所有輸出項目上設置緩存並且能夠在一個地方調整所有緩存設置非常方便。手動添加項目到緩存時有沒有辦法使用使用緩存配置文件?
但是,我也在我的數據和邏輯層實現某些項目的緩存。如果我也可以引用一個配置文件而不是對我想要緩存的數據和邏輯項的緩存參數進行硬編碼,但是似乎沒有辦法在Insert()方法中引用一個配置文件緩存對象。
另外,我可以建立自己的配置部分列出手動添加項目的緩存配置文件。
我在使用web.config中的輸出緩存配置文件配置的應用程序中輸出緩存。能夠在需要它的所有輸出項目上設置緩存並且能夠在一個地方調整所有緩存設置非常方便。手動添加項目到緩存時有沒有辦法使用使用緩存配置文件?
但是,我也在我的數據和邏輯層實現某些項目的緩存。如果我也可以引用一個配置文件而不是對我想要緩存的數據和邏輯項的緩存參數進行硬編碼,但是似乎沒有辦法在Insert()方法中引用一個配置文件緩存對象。
另外,我可以建立自己的配置部分列出手動添加項目的緩存配置文件。
你可以得到你的輸出緩存配置文件列表,這樣做:
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
對象,則可以將GUID附加到該鍵,以便每個配置文件都有相應的GUID,您可以在稍後檢索配置文件時從緩存中提取該GUID。