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

发布于 2024-10-13 10:23:32 字数 239 浏览 0 评论 0原文

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

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

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

I have output caching in my application configured using output cache profiles in the web.config. It is very convenient to be able to setup caching on all the output items that need it and then be able to adjust all the cache settings in one place.

However, I am also implementing caching in my data and logic layers for certain items. It would be convenient if I could also reference a profile instead of hard coding the caching parameters for data and logic items I want to cache, but there doesn't seem to be a way to reference a profile in the Insert() method on the cache object.

Alternativly, I could build my own configuration section to list cache profiles for manually added items.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

指尖凝香 2024-10-20 10:23:32

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

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
    }

You can get a list of your output cache profiles doing this:

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;
        }
    }
}

And then use it on your insert:

/// <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
    }
葬﹪忆之殇 2024-10-20 10:23:32

如果您引用 C# 的 Cache.Insert 对象,您可以将 GUID 附加到键,以便每个配置文件都有一个相应的 GUID,当您稍后想要检索配置文件时,您可以从缓存中提取该 GUID 。

If you are referring to C#'s Cache.Insert object you can append a GUID to the key so that every profile has a corresponding GUID which you can extract out of the cache when you want to retrieve the profile later on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文