简体   繁体   English

ASP.Net MVC中的不可见缓存

[英]Invisible caching in ASP.Net MVC

I'm creating a page that has some dynamically generated images built from data that comes from web services. 我正在创建一个页面,其中包含一些动态生成的图像,这些图像是根据来自Web服务的数据构建的。 The image generation takes a fair amount of time due to the time involved in hitting the web services, so I need to do some caching. 由于命中Web服务需要花费大量时间,因此图像生成需要花费大量时间,因此我需要进行一些缓存。

One option would be to use the OutputCache parameter to cache the images, but I don't like forcing some unlucky user to wait for a really long time. 一种选择是使用OutputCache参数来缓存图像,但是我不希望强迫某些不幸的用户等待很长时间。 I'd rather write the images to files in the background and serve static html. 我宁愿将图像写到后台文件中并提供静态html。

Whats the best way to do this? 最好的方法是什么? I'm thinking about creating a special url to trigger refreshes that writes the images to disk and setting up a scheduled task of some sort to hit the refresh url. 我正在考虑创建一个特殊的URL来触发将图像写入磁盘的刷新,并设置某种计划任务以命中刷新URL。 Any better ideas? 还有更好的主意吗?

It seems its possible to use memcached with ASP.Net, how hard would that be to set up? 似乎可以将memcached与ASP.Net一起使用,设置起来有多困难? Seems like it may be overkill for this situation (internal tool) and I've already got the disk based version working, but I'm curious. 似乎对于这种情况(内部工具)而言,这可能是过大的了,而且我已经可以使用基于磁盘的版本,但是我很好奇。

We do something similar, although we simply precompute the files/images and store them in the HttpRuntime.Cache. 尽管我们只是预先计算文件/图像并将它们存储在HttpRuntime.Cache中,但是我们执行类似的操作。 This way our views can still be generated as-is, but they typically pull from cached data rather than generating on-the-fly. 这样,我们的视图仍然可以按原样生成,但是它们通常是从缓存的数据中提取的,而不是即时生成的。

On the off chance that the cached data isn't available, we have getter functions to generate them: 如果无法获得缓存的数据,我们可以使用getter函数来生成它们:

public static GetGraph(int id)
{
    if (HttpRuntime.Cache["image_"+id] == null)
        HttpRuntime.Cache["image_"+id] = _imageGen.GenerateGraph(id);
    return HttpRuntime.Cache["image_"+id];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM