Asp.net mvc 2 在MVC2中使用缓存存储

Asp.net mvc 2 在MVC2中使用缓存存储,asp.net-mvc-2,session,caching,outputcache,Asp.net Mvc 2,Session,Caching,Outputcache,我有一个类,它持有用户的有效许可证,每15分钟“验证”一次,以确保当前许可证有效,并添加/删除任何可能已更改的许可证 目前,这是在my ApplicationController中访问的,应用程序中的每个其他控制器都继承自my ApplicationController,因此每当用户执行任何操作时,它都会确保他们拥有执行此操作的有效许可证/权限 许可模式: public class LicenseModel { public DateTime LastValidated { get; s

我有一个类,它持有用户的有效许可证,每15分钟“验证”一次,以确保当前许可证有效,并添加/删除任何可能已更改的许可证

目前,这是在my ApplicationController中访问的,应用程序中的每个其他控制器都继承自my ApplicationController,因此每当用户执行任何操作时,它都会确保他们拥有执行此操作的有效许可证/权限

许可模式:

public class LicenseModel
{
    public DateTime LastValidated { get; set; }
    public List<License> ValidLicenses { get; set; }
    public bool NeedsValidation
    {
        get{ return ((DateTime.Now - this.LastValidated).Minutes >= 15);}
    }

    //Constructor etc...
}
摘要:

public class LicenseModel
{
    public DateTime LastValidated { get; set; }
    public List<License> ValidLicenses { get; set; }
    public bool NeedsValidation
    {
        get{ return ((DateTime.Now - this.LastValidated).Minutes >= 15);}
    }

    //Constructor etc...
}

正如您所看到的,这个过程目前使用会话来存储LicenseModel,但是我想知道使用缓存来存储它是否更容易/更高效。(或者可能是OutputCache?)以及我如何实现它。

如果许可证在应用程序范围内使用,并且不特定于任何用户会话,那么缓存肯定会更有意义。缓存可以为您处理15分钟的过期时间,您将不再需要LicenseModel类上的NeedsValidation或LastValidated属性。您可以将该模型全部删除,只需存储有效许可证列表,如下所示:

if (HttpContext.Cache["License"] == null)
{
    HttpContext.Cache.Insert("License",Service.GetLicenses(), null,
    DateTime.Now.AddMinutes(15), Cache.NoSlidingExpiration);
}

var licenses = HttpContext.Cache["License"] as List<License>;
if(HttpContext.Cache[“License”]==null)
{
HttpContext.Cache.Insert(“License”,Service.GetLicenses(),null,
DateTime.Now.AddMinutes(15),Cache.NoSlidingExpiration);
}
var licenses=HttpContext.Cache[“License”]作为列表;