用于 asp.net 输出缓存的 Windows Server Appfabric 提供程序

发布于 2024-12-10 04:35:08 字数 58 浏览 0 评论 0原文

Windows Server AppFabric 1.0 是否存在 asp.net 输出缓存提供程序?

Does asp.net output cache provider exist for Windows Server AppFabric 1.0?

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

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

发布评论

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

评论(1

慕烟庭风 2024-12-17 04:35:08

不会。但是由于输出缓存在 ASP.NET 4.0 中是可插入的(使用提供程序模型),因此您可以编写自己的缓存。

要创建新的输出缓存提供程序,您需要继承 System.Web.Caching.OutputCacheProvider,您需要引用 System.Web系统.配置

然后,主要是重写基础提供程序中的四个方法:Add、Get、Remove 和 Set。

由于您的网站可能会获得相当多的点击,因此您肯定会希望为 DataCacheFactory 使用 Singleton,此代码使用 Jon Skeet 的单例模式(假设我理解正确)。

using System;
using System.Web.Caching;
using Microsoft.ApplicationServer.Caching;

namespace AppFabricOutputCache
{
public sealed class AppFabricOutputCacheProvider : OutputCacheProvider
{
    private static readonly AppFabricOutputCacheProvider instance = new AppFabricOutputCacheProvider();
    private DataCacheFactory factory;
    private DataCache cache;

    static AppFabricOutputCacheProvider()
    { }

    private AppFabricOutputCacheProvider()
    {
        // Constructor - new up the factory and get a reference to the cache based 
        // on a setting in web.config
        factory = new DataCacheFactory();
        cache = factory.GetCache(System.Web.Configuration.WebConfigurationManager.AppSettings["OutputCacheName"]);
    }

    public static AppFabricOutputCacheProvider Instance
    {
        get { return instance; }
    }

    public override object Add(string key, object entry, DateTime utcExpiry)
    {
        // Add an object into the cache. 
        // Slight disparity here in that we're passed an absolute expiry but AppFabric wants
        // a TimeSpan. Subtract Now from the expiry we get passed to create the TimeSpan
        cache.Add(key, entry, utcExpiry - DateTime.UtcNow);
    }

    public override object Get(string key)
    {
        return cache.Get(key);
    }

    public override void Remove(string key)
    {
        cache.Remove(key);
    }

    public override void Set(string key, object entry, DateTime utcExpiry)
    {
        // Set here means 'add it if it doesn't exist, update it if it does'
        // We can do this by using the AppFabric Put method
        cache.Put(key, entry, utcExpiry - DateTime.UtcNow);
    }
}
}

编写完此内容后,您需要配置应用程序以在 web.config 中使用它:

<system.web>
    <caching>
        <outputCache defaultProvider="AppFabricOutputCache">
            <providers>
                <add name="AppFabricOutputCache" type="AppFabricOutputCache, AppFabricOutputCacheProvider" />
            </providers>
        </outputCache>
     </caching>
</system.web>

MSDN:OutputCacheProvider
ScottGu 关于创建 OutputCacheProvider 的博客

No. But because output caching is pluggable in ASP.NET 4.0 (using the provider model), you can write your own.

To create a new output cache provider, you'll need to inherit from System.Web.Caching.OutputCacheProvider, you'll need to reference System.Web and System.Configuration.

Then it's largely a case of overriding the four methods from the base provider, Add, Get, Remove and Set.

As your site is probably going to getting quite a few hits, you'll definitely want to use a Singleton for the DataCacheFactory, this code uses Jon Skeet's singleton pattern (assuming I've understood it correctly).

using System;
using System.Web.Caching;
using Microsoft.ApplicationServer.Caching;

namespace AppFabricOutputCache
{
public sealed class AppFabricOutputCacheProvider : OutputCacheProvider
{
    private static readonly AppFabricOutputCacheProvider instance = new AppFabricOutputCacheProvider();
    private DataCacheFactory factory;
    private DataCache cache;

    static AppFabricOutputCacheProvider()
    { }

    private AppFabricOutputCacheProvider()
    {
        // Constructor - new up the factory and get a reference to the cache based 
        // on a setting in web.config
        factory = new DataCacheFactory();
        cache = factory.GetCache(System.Web.Configuration.WebConfigurationManager.AppSettings["OutputCacheName"]);
    }

    public static AppFabricOutputCacheProvider Instance
    {
        get { return instance; }
    }

    public override object Add(string key, object entry, DateTime utcExpiry)
    {
        // Add an object into the cache. 
        // Slight disparity here in that we're passed an absolute expiry but AppFabric wants
        // a TimeSpan. Subtract Now from the expiry we get passed to create the TimeSpan
        cache.Add(key, entry, utcExpiry - DateTime.UtcNow);
    }

    public override object Get(string key)
    {
        return cache.Get(key);
    }

    public override void Remove(string key)
    {
        cache.Remove(key);
    }

    public override void Set(string key, object entry, DateTime utcExpiry)
    {
        // Set here means 'add it if it doesn't exist, update it if it does'
        // We can do this by using the AppFabric Put method
        cache.Put(key, entry, utcExpiry - DateTime.UtcNow);
    }
}
}

Once you've got this written, you need to configure your application to use it in your web.config:

<system.web>
    <caching>
        <outputCache defaultProvider="AppFabricOutputCache">
            <providers>
                <add name="AppFabricOutputCache" type="AppFabricOutputCache, AppFabricOutputCacheProvider" />
            </providers>
        </outputCache>
     </caching>
</system.web>

MSDN: OutputCacheProvider
ScottGu's blog on creating OutputCacheProviders

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