在 MVC2 中使用缓存存储
我有一个类,它持有用户的有效许可证,并且每 15 分钟对其进行一次“验证”,以确保当前许可证有效并添加/删除任何可能已更改的许可证。
目前,这是在我的 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...
}
验证过程: (发生在 ApplicationController 的 Initialize() 方法内)
LicenseModel licenseInformation = new LicenseModel();
if (Session["License"] != null)
{
licenseInformation = Session["License"] as LicenseModel;
if (licenseInformation.NeedsValidation)
licenseInformation.ValidLicenses = Service.GetLicenses();
licenseInformation.LastValidated = DateTime.Now;
Session["License"] = licenseInformation;
}
else
{
licenseInformation = new LicenseModel(Service.GetLicenses());
Session["License"] = licenseInformation;
}
摘要:
当您可以看到,这个过程当前使用 Session 来存储 LicenseModel,但是我想知道使用 Cache 来存储它是否会更容易/更高效。 (或者可能是 OutputCache?) 以及我如何实现它。
I have a class that holds valid licenses for a user and every 15 minutes it is "validated" to ensure the current licenses are valid and add/remove any that may have changed.
Currently, this is accessed in my ApplicationController, which every other controller in the application inherits from, so that whenever the user performs any operations, it ensures they have the valid licenses / permissions to do so.
Licensing Model:
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...
}
Validation Process: (occurs inside the Initialize() method of the ApplicationController)
LicenseModel licenseInformation = new LicenseModel();
if (Session["License"] != null)
{
licenseInformation = Session["License"] as LicenseModel;
if (licenseInformation.NeedsValidation)
licenseInformation.ValidLicenses = Service.GetLicenses();
licenseInformation.LastValidated = DateTime.Now;
Session["License"] = licenseInformation;
}
else
{
licenseInformation = new LicenseModel(Service.GetLicenses());
Session["License"] = licenseInformation;
}
Summary:
As you can see, this process currently uses the Session to store the LicenseModel, however I was wondering if it might be easier / more-efficient to use the Cache to store this. (Or possibly the OutputCache?) and how I might go about implementing it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果许可证在应用程序范围内使用并且不特定于任何用户会话,那么缓存肯定会更有意义。缓存可以为您处理 15 分钟过期的情况,您将不再需要 LicenseModel 类上的 NeedsValidation 或 LastValidated 属性。您可能可以完全取消该模型,只存储有效许可证列表,如下所示:
The cache would definitely make more sense if the licenses are used application wide and are not specific to any user session. The cache can handle the 15 minute expiration for you and you will no longer need the the NeedsValidation or LastValidated properties on your LicenseModel class. You could probably do away with the model all together and just store the List of valid licenses like so: