【问题标题】:Avoid SQL lookup with ImageResizer and DiskCache plugin避免使用 ImageResizer 和 DiskCache 插件进行 SQL 查找
【发布时间】:2014-05-06 00:48:22
【问题描述】:

我目前在弄清楚如何使用 ImageResizer 插件以正确使用 SQL 和 DiskCache 插件时遇到问题。

我的命名策略如下:

/myimagetitle-4319560-100x100.jpg 被 IIS URL 重写模块重写为 /4319560.jpg?id=4319560&title=myimagetitle&height=100&width=100。 这按预期工作。

现在,为了找到图像的文件名,我需要使用 SQL 翻译 id。我创建了一个IVirtualImageProvider 插件,它实现了FileExistsGetFile 方法。

public IVirtualFile GetFile(string virtualPath, NameValueCollection queryString)
{
    var path = this.GetOriginalFilePath(queryString);
    return new VirtualFileWrapper(new ProductPhotoVirtualFile(path));
}

public bool FileExists(string virtualPath, NameValueCollection queryString)
{
    if (File.Exists(this.GetCachedFilePath(queryString)))
    {
        return true;
    }

    if (File.Exists(this.GetOriginalFilePath(queryString)))
    {
        return true;
    }

    return false;
}

private string GetCachedFilePath(NameValueCollection queryString)
{
    // Get customized cache file path based on the query string
    // "cache\4319\560\4319560_w_100_h_100.jpg"
}

private string GetOriginalFilePath(NameValueCollection queryString)
{
    // Perform SQL lookup to translate the id from the query to a file name
}

我正在使用 DiskCache 插件来确保我的图像使用 IIS 等进行缓存。 不幸的是,FileExists 方法始终运行,对每个请求执行 SQL。

我想要达到的目标如下:

  1. 让 DiskCache 插件FileExists 方法之前运行,这样如果文件被缓存,则跳过实际的 SQL 查找
  2. 处理缓存文件命名策略,以便其他工具在正确的文件夹中生成图像。

以上是否有可能和/或我做错了什么?

谢谢

【问题讨论】:

    标签: imageresizer imageresizer-diskcache


    【解决方案1】:

    FileExists 将始终被调用,因为没有其他方法可以确定哪个 IVirtualImageProvider 应该负责请求 - 因此哪个负责提供缓存细节,例如修改日期和密钥。一个更好的名字是IsHandled

    实际上,可以放在 FileExists 方法中,因为稍后在 .Open() 期间抛出 FileNotFoundException 也会被处理为 404。如果它会阻止另一个 IVirtualImageProvider 工作,则不能放在 FileExists 中。

    在您的情况下,如果图像 URL 采用您期望的格式(即具有 ID,或者位于专用于 SQL 图像 blob 的路径结构内),您应该从 FileExists 返回 true。但是,通常最好使用路径前缀,因为很难可靠地预测更复杂的模式。

    【讨论】:

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