DiskCache plugin imageresizer in MVC3

故事扮演 提交于 2019-12-06 16:09:22

问题


For my MVC project (Image Server Application), I cannot do caching using imageresizer. I can access my images like this and the image source could be either FileSystem/Database (Dependency injeciton) :

localhost/images/123.jpg?width=500
localhost/images/123?width=500

I have an MVC 3 project with routes like

        routes.RouteExistingFiles = true;
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("favicon.ico");

        routes.MapRoute(
            "ImagesWithExtension", // Route name
            "images/{imageName}.{extension}/", // URL with parameters
            new { controller = "Home", action = "ViewImageWithExtension", imageName = "", extension = "", id = UrlParameter.Optional } // Parameter defaults
        );

        routes.MapRoute(
            "Images", // Route name
            "images/{imageName}/", // URL with parameters
            new { controller = "Home", action = "ViewImage", imageName = "", id = UrlParameter.Optional } // Parameter defaults
        );

I have two controllers to deal with images public ActionResult ViewImageWithExtension(string imageName, string extension) {} public ActionResult ViewImage(string imageName) {}

The caching is done when the URL is like : localhost/images/123.jpg?width=500 and the image source is FileSystem
localhost/images/123?width=500 Cache not working image source is Filesystem
localhost/images/123.jpg?width=500 Cache not working, image source DB
localhost/images/123?width=500 Cache not working , image source DB

My web config is like this:

<configSections>
    <section name="resizer" type="ImageResizer.ResizerSection" />   </configSections>


<resizer>
    <!-- Unless you (a) use Integrated mode, or (b) map all reqeusts to ASP.NET, 
         you'll need to add .ashx to your image URLs: image.jpg.ashx?width=200&height=20 
         Optional - this is the default setting -->
    <diagnostics enableFor="AllHosts" />
    <pipeline fakeExtensions=".ashx" />
    <DiskCache dir="~/MyCachedImages" autoClean="false" hashModifiedDate="true" enabled="true" subfolders="32" cacheAccessTimeout="15000" asyncWrites="true" asyncBufferSize="10485760" />
    <cleanupStrategy startupDelay="00:05" minDelay="00:00:20" maxDelay="00:05" optimalWorkSegmentLength="00:00:04" targetItemsPerFolder="400" maximumItemsPerFolder="1000" avoidRemovalIfCreatedWithin="24:00" avoidRemovalIfUsedWithin="4.00:00" prohibitRemovalIfUsedWithin="00:05" prohibitRemovalIfCreatedWithin="00:10" />
    <plugins>
      <add name="DiskCache" />
    </plugins>   </resizer>

Am I doing something wrong or Imageresizer doesnt support this scenario ? If not any good plugin to use disk based image cahce ?

Thanks in advance.


回答1:


As I explained in the other public forums which you simultaneously posted this question, the ImageResizer supports dependency injection from the ground up. You're trying to wrap dependency injection with more dependency injection, but backwards.

A) ASP.NET MVC 3 and 4 prevent efficient disk caching, by design. You need to work with the ImageResizer HttpModule, not against it, to get good performance. That means using the URL API, not the Managed API wrapped by your own MVC ActionResults. Listen to my podcast with Scott Hanselman for more info.

B) SqlReader, S3Reader, MongoReader, VirtualFolder, and AzureReader all support dynamic injection, and can (with a tiny bit of configuration), all use the same path syntax. The ImageResizer is designed to allow easy migration between data stores.

C) You can use the Config.Current.Pipeline.Rewrite event to make the URL API use any syntax you want. This is much more flexible than MVC routes (and less buggy).

D) If you want to add another layer of dependency injection, implement IPlugin, and dynamically pick the appropriate data store and configure it from the Install method.



来源:https://stackoverflow.com/questions/9463731/diskcache-plugin-imageresizer-in-mvc3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!