Asp.net 手动将项目添加到缓存时,是否有方法使用缓存配置文件?

Asp.net 手动将项目添加到缓存时,是否有方法使用缓存配置文件?,asp.net,asp.net-caching,asp.net-cache,Asp.net,Asp.net Caching,Asp.net Cache,我在应用程序中使用web.config中的输出缓存配置文件配置了输出缓存。能够在所有需要缓存的输出项上设置缓存,然后能够在一个位置调整所有缓存设置,这非常方便 但是,我还在数据层和逻辑层中为某些项实现缓存。如果我也可以引用一个概要文件,而不是对要缓存的数据和逻辑项的缓存参数进行硬编码,那会很方便,但是似乎没有办法在缓存对象的Insert()方法中引用概要文件 或者,我可以建立我自己的配置部分来列出手动添加项目的缓存配置文件。如果您指的是C#的缓存。插入对象您可以将GUID附加到键,以便每个配置文

我在应用程序中使用web.config中的输出缓存配置文件配置了输出缓存。能够在所有需要缓存的输出项上设置缓存,然后能够在一个位置调整所有缓存设置,这非常方便

但是,我还在数据层和逻辑层中为某些项实现缓存。如果我也可以引用一个概要文件,而不是对要缓存的数据和逻辑项的缓存参数进行硬编码,那会很方便,但是似乎没有办法在缓存对象的Insert()方法中引用概要文件


或者,我可以建立我自己的配置部分来列出手动添加项目的缓存配置文件。

如果您指的是C#的
缓存。插入
对象您可以将GUID附加到键,以便每个配置文件都有相应的GUID,您可以在以后检索配置文件时从缓存中提取该GUID。

您可以获得执行此操作的输出缓存配置文件列表:

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;
        }
    }
}
private Dictionary\u outputCacheProfiles;
/// 
///使用中的设置进行初始化
///“system.web\caching\outputCacheSettings”
/// 
无效初始化OutputCacheProfiles(
System.Configuration.Configuration-appConfig,
NameValueCollection提供程序配置)
{
_outputCacheProfiles=新字典();
outputCacheSettings部分outputCacheSettings=
(OutputCacheSettingsSection)appConfig.GetSection(“system.web/caching/outputCacheSettings”);
如果(outputCacheSettings!=null)
{
foreach(outputCacheSettings.OutputCacheProfiles中的OutputCacheProfile配置文件)
{
_outputCacheProfiles[profile.Name]=配置文件;
}
}
}
然后将其用于插入:

/// <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
    }
//
///获取具有指定名称的输出缓存配置文件
/// 
公共OutputCacheProfile GetOutputCacheProfile(字符串名称)
{
if(!\u outputCacheProfiles.ContainsKey(名称))
{
抛出新的ArgumentException(String.Format(“输出缓存配置文件“{0}”未注册,名称));
}
返回_outputCacheProfiles[名称];
}
/// 
///使用输出缓存配置文件的规范插入键/值对
/// 
public void InsertItemUsing(字符串outputCacheProfileName、字符串键、对象值)
{
OutputCacheProfile profile=GetOutputCacheProfile(outputCacheProfileName);
//从配置文件中获取用于插入的设置,而不是硬编码
}