【问题标题】:DiskCache plugin imageresizer in MVC3MVC3 中的 DiskCache 插件 imageresizer
【发布时间】:2012-02-27 10:50:15
【问题描述】:

对于我的 MVC 项目(图像服务器应用程序),我无法使用 imageresizer 进行缓存。我可以像这样访问我的图像,图像源可以是文件系统/数据库(依赖注入):

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

我有一个 MVC 3 项目,其路线类似于

        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
        );

我有两个控制器来处理图像 public ActionResult ViewImageWithExtension(string imageName, string extension) {} 公共 ActionResult ViewImage(string imageName) {}

当 URL 如下时缓存完成: localhost/images/123.jpg?width=500 并且图片来源是 FileSystem
localhost/images/123?width=500 缓存不工作图像源是文件系统
localhost/images/123.jpg?width=500 缓存不起作用,图像源数据库
localhost/images/123?width=500 缓存不工作,图片源数据库

我的网络配置是这样的:

<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>

是我做错了什么还是 Imageresizer 不支持这种情况?如果没有任何好的插件来使用基于磁盘的图像cahce?

提前致谢。

【问题讨论】:

    标签: asp.net-mvc image-processing imageresizer image-caching


    【解决方案1】:

    正如我在您同时发布此问题的其他公共论坛中所解释的,ImageResizer 从头开始​​支持依赖注入。您正在尝试用更多的依赖注入来包装依赖注入,但反过来。

    A)ASP.NET MVC 3 and 4 prevent efficient disk caching, by design。您需要使用 ImageResizer HttpModule,而不是反对它,以获得良好的性能。这意味着使用 URL API,而不是由您自己的 MVC ActionResults 包装的托管 API。听我的podcast with Scott Hanselman 了解更多信息。

    B) SqlReader、S3Reader、MongoReader、VirtualFolder 和 AzureReader 都支持动态注入,并且可以(通过少量配置)都使用相同的路径语法。 ImageResizer 旨在允许在数据存储之间轻松迁移。

    C) 您可以使用Config.Current.Pipeline.Rewrite 事件使 URL API 使用您想要的任何语法。这比 MVC 路由灵活得多(并且错误更少)。

    D) 如果要添加另一层依赖注入,请实现IPlugin,并动态选择适当的数据存储并通过Install 方法对其进行配置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-25
      • 1970-01-01
      • 1970-01-01
      • 2021-10-03
      • 2014-01-18
      • 2013-04-27
      • 1970-01-01
      相关资源
      最近更新 更多