0

我正在尝试在 ASP.Net MVC 4 网站中使用 ImageResizer (来自http://imageresizing.net )。我创建了一个 IVirtualImageProvider 来在远程服务器上获取我的图像(实际上通过 Entity 连接到 SQL 服务器)。

一切皆好。奇怪的是,我的提供程序为每个图像请求调用了两次,但视觉结果还可以。

现在,我想测试 DiskCache 插件。所以 Nuget + config... 和... 一切似乎都表现得完全一样:我的远程服务器每次请求都会被调用两次。

不是有什么我不明白的吗?使用 diskcache 应该会阻止我的图像的正常渲染和调用我的提供程序,不是吗?

我的配置:

<resizer>
    <pipeline fakeExtensions=".ashx" vppUsage="Always" />
    <diskCache dir="~/c_dynimages" autoClean="false" hashModifiedDate="true" enabled="true" subfolders="32" cacheAccessTimeout="15000" />

    <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="MvcRoutingShim" />
        <add name="TwinklazDb" />
        <add name="DiskCache" />
    </plugins>
</resizer>

我的插件:

/// <summary>
/// Actually install plugin into plugin collection of ImageReizer
/// </summary>
/// <param name="c">config</param>
/// <returns>this</returns>
public IPlugin Install(Configuration.Config c)
{
    c.Plugins.add_plugin(this);
    return this;
}

/// <summary>
/// Remove plugin from list
/// </summary>
/// <param name="c">ImageResizer configuration</param>
/// <returns>true</returns>
public bool Uninstall(Configuration.Config c)
{
    c.Plugins.remove_plugin(this);
    return true;
}

#endregion

/// <summary>
/// Returns true if virtualPath contains prefix (dynimages/) 
/// </summary>
/// <param name="virtualPath"></param>
/// <param name="queryString"></param>
/// <returns></returns>
public bool FileExists(string virtualPath, System.Collections.Specialized.NameValueCollection queryString)
{
    bool result = false;
    if (virtualPath.StartsWith(PREFIX))
    {
        long id;
        result = long.TryParse(virtualPath.Substring(PREFIX_LENGTH).Replace(".jpg",""), out id);
    }
    return result;
}

/// <summary>
/// Ask binary service to return a file
/// </summary>
/// <param name="virtualPath">Path as read from http request</param>
/// <param name="queryString">Unused</param>
/// <returns>A dbfile</returns>
public IVirtualFile GetFile(string virtualPath, System.Collections.Specialized.NameValueCollection queryString)
{
    long id;
    if (long.TryParse(virtualPath.Substring(PREFIX_LENGTH).Replace(".jpg", ""), out id))
        return new DbFile(virtualPath, Services.Binary.GetBinary(id));
    return null;
}

和 DbFile 类:

private class DbFile : IVirtualFile
{
    #region Private members

    /// <summary>
    /// virtual path of the file
    /// </summary>
    private string _virtualPath;

    /// <summary>
    /// File content (blob)
    /// </summary>
    private byte[] _content;

    #endregion
    #region Constructor

    /// <summary>
    /// Builds a DBFile from path and content
    /// </summary>
    /// <param name="path">Path to file (as read from http handler)</param>
    /// <param name="content">Content of the file read from database</param>
    public DbFile(string path, byte[] content)
    {
        _virtualPath = path;
        _content = content;
    }

    #endregion

    /// <summary>
    /// Opens a stream on file content and returns it
    /// </summary>
    /// <returns>A memory stream on content array</returns>
    public System.IO.Stream Open()
    {
        return new System.IO.MemoryStream(_content);
    }

    /// <summary>
    /// Gets virtual path
    /// </summary>
    public string VirtualPath
    {
        get { return _virtualPath; }
    }
}

我的请求 :

http://localhost:36782/dynimages/1.jpg.ashx?width=100&height=50&mode=stretch

欢迎任何帮助!

4

1 回答 1

1

延迟所有工作,直到 .Open() 被调用

IVirtualImageProvider 实现应该延迟所有工作,直到 .Open() 被调用。

DiskCache 需要一个 IVirtualFile 实例来准确派生缓存哈希,因为一些提供程序还提供修改日期以启用失效。

于 2013-09-14T17:31:48.890 回答