gpt4 book ai didi

asp.net - 手动将项目添加到缓存时,是否可以使用缓存配置文件?

转载 作者:行者123 更新时间:2023-12-04 06:36:59 25 4
gpt4 key购买 nike

我使用 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
}

关于asp.net - 手动将项目添加到缓存时,是否可以使用缓存配置文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4761657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com