刷新旧的缓存数据
我在 ASP.NET MVC 3 应用程序中的控制器操作之一上方有 Outputcache 属性:
[OutputCache(Duration=86400)] // One day
public JsonResult GetCompanyName(int companyId)
{
var company = _session.Get<Company>(companyId);
if (company == null)
throw new Exception();
return Json(company.Name);
}
缓存背后的原因是我们支持的公司不会经常更改,因此我们不需要查询每个公司的数据库具有相同参数的 HttpRequest。
如果我们添加一家新公司,我如何强制“重新检查”?
是否只能执行此操作,或者我必须删除所有应用程序缓存?
更新:我的数据库是Oracle
,我的ORM是NHibernate
I have Outputcache attribute above one of my controller action in an ASP.NET MVC 3 application:
[OutputCache(Duration=86400)] // One day
public JsonResult GetCompanyName(int companyId)
{
var company = _session.Get<Company>(companyId);
if (company == null)
throw new Exception();
return Json(company.Name);
}
The reason behind the cache is that the companies we support doesn't change so often, so we don't need to query the data base for each HttpRequest with the same parameter.
If we add a new company how can I force the "re-check"?
Can it be done to this action only or I have to delete all my app cache?
Update: My data base is Oracle
and my ORM is NHibernate
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 RemoveOutputCacheItem 方法。因此,假设您已调用这样的方法来获取公司:
为了使缓存中的该 url 过期,以便下次调用它时获得新结果:
但恕我直言,对于这种情况,最好使用NHibernate 的二级缓存用于缓存结果,而不是使用 OutputCache 属性缓存整个操作。此操作所做的只是返回来自数据库的 JSON 序列化对象。昂贵的不是操作调用而是数据库调用。因此,仅缓存此数据库调用的结果(使用 ORM 缓存或系统缓存提供程序),因为这将为您提供对此缓存的过期策略的更强大的控制。
You could use the RemoveOutputCacheItem method. So let's suppose that you have called the method like this to fetch the companies:
In order to expire this url from the cache so that the next time it is called it gets fresh results:
But IMHO for this scenario it would be better to use the second level cache of NHibernate to cache the results instead of caching the entire action with the OutputCache attribute. All that this action does is to return the JSON serialized object that came from the database. What is expensive is not the action call but the database call. So cache only the results of this database call (either using your ORMs cache or the system cache providers) as this will provide you with more robust control over the expiration policies of this cache.