2012-02-14 104 views
1

我在我的网站上显示图像,这些图像存储在数据库中。从我的剃刀视角来看,我写了这样一行。存储在数据库中的MVC3高速缓存图像

<img alt ="" src='@Url.Action("ShowImage", 
      "Controller", new { imageID = Model.ImageID })'/> 

被调用的函数看起来像这样。

public ActionResult ShowImage(string imageID) 
    { 
     WebServiceClient client = new WebServiceClient(); 
     byte[] image = client.GetImage(Convert.ToInt64(imageID)); 
     if (image == null) 
     { 
      return new EmptyResult(); 
     } 
     return File(image,"image/png"); 
    } 

这工作得很好,但我的问题是如果有可能以某种方式缓存这些图像。我已经看到了使用自定义HttpHandler缓存图像的方法,但所有这些例子都是如果您有实际文件而不是二进制数据。

+0

你有没有尝试在高速缓存中使用构建.NET? – Paul 2012-02-14 17:38:27

+0

我已经使用AppFabric缓存这个:http://msdn.microsoft.com/en-us/gg457898 – rboarman 2012-02-14 17:41:25

回答

1

你可以尝试用[OutputCache]属性装饰你的控制器动作:

[OutputCache(Duration = 3600, VaryByParam = "imageID")] 
public ActionResult ShowImage(string imageID) 
{ 
    WebServiceClient client = new WebServiceClient(); 
    byte[] image = client.GetImage(Convert.ToInt64(imageID)); 
    if (image == null) 
    { 
     return new EmptyResult(); 
    } 
    return File(image,"image/png"); 
} 

您也可以决定你想要的图像,使用Location财产被缓存。缺省值是Any,这意味着输出缓存可以位于浏览器客户端(发起请求的位置),参与请求的代理服务器(或任何其他服务器)上,也可以位于处理请求的服务器上。

+0

嗨达林先生,我试图说[杰夫阿特伍德](http://stackoverflow.com/questions/ 642954/iis7-cache-control)先生,我使用Web.config为图像和css文件设置过期期限,但过期头文件未设置,如果您可以查看问题,那将是非常好的。 [这里](http://stackoverflow.com/questions/35704618/asp-mvc-expire-headers-not-set-for-static-contents)是我编辑的问题。 – stom 2016-03-12 12:07:42

2

这里是一个高速缓存说唱歌手,你可以使用:

public class CacheManager 
{ 
    private static readonly string keyPrefix = typeof(CacheManager).FullName; 
    private static readonly object syncLock = new object(); 

    private readonly Cache cache; 

    public CacheManager(Cache cache) 
    { 
     this.cache = HttpRuntime.Cache; 
    } 

    public TValue Get<TValue>(string key) 
    { 
     return (TValue)HttpRuntime.Cache[MakeKey(key)]; 
    } 

    public void Set<TValue>(string key, Func<TValue> value) 
    { 
     Set(key, null, null, null, value, null); 
    } 

    public void Set<TValue>(string key, DateTime timestamp, TValue value) 
    { 
     Set(key, timestamp, null, null, value, null); 
    } 

    public void Set<TValue>(string key, TimeSpan duration, TValue value) 
    { 
     Set(key, null, duration, null, value, null); 
    } 

    public void Set<TValue>(string key, TValue value, Action<bool> onRemoveCallback) 
    { 
     Set(key, null, null, null, value, onRemoveCallback); 
    } 

    public void Set<TValue>(string key, DateTime timestamp, TValue value, Action<bool> onRemoveCallback) 
    { 
     Set(key, timestamp, null, null, value, onRemoveCallback); 
    } 

    public void Set<TValue>(string key, TimeSpan duration, TValue value, Action<bool> onRemoveCallback) 
    { 
     Set(key, null, duration, null, value, onRemoveCallback); 
    } 

    public void Set<TValue>(string key, IEnumerable<string> fileDependencies, TValue value) 
    { 
     Set(key, null, null, fileDependencies, value, null); 
    } 

    public void Set<TValue>(string key, DateTime timestamp, IEnumerable<string> fileDependencies, TValue value) 
    { 
     Set(key, timestamp, null, fileDependencies, value, null); 
    } 

    public void Set<TValue>(string key, TimeSpan duration, IEnumerable<string> fileDependencies, TValue value) 
    { 
     Set(key, null, duration, fileDependencies, value, null); 
    } 

    public void Set<TValue>(string key, IEnumerable<string> fileDependencies, TValue value, Action<bool> onRemoveCallback) 
    { 
     Set(key, null, null, fileDependencies, value, onRemoveCallback); 
    } 

    public void Set<TValue>(string key, DateTime timestamp, IEnumerable<string> fileDependencies, TValue value, Action<bool> onRemoveCallback) 
    { 
     Set(key, timestamp, null, fileDependencies, value, onRemoveCallback); 
    } 

    public void Set<TValue>(string key, TimeSpan duration, IEnumerable<string> fileDependencies, TValue value, Action<bool> onRemoveCallback) 
    { 
     Set(key, null, duration, fileDependencies, value, onRemoveCallback); 
    } 

    public TValue GetOrCreate<TValue>(string key, Func<TValue> factory) 
    { 
     return GetOrCreate(key, null, null, null, factory, null); 
    } 

    public TValue GetOrCreate<TValue>(string key, DateTime timestamp, Func<TValue> factory) 
    { 
     return GetOrCreate(key, timestamp, null, null, factory, null); 
    } 

    public TValue GetOrCreate<TValue>(string key, TimeSpan duration, Func<TValue> factory) 
    { 
     return GetOrCreate(key, null, duration, null, factory, null); 
    } 

    public TValue GetOrCreate<TValue>(string key, Func<TValue> factory, Action<bool> onRemoveCallback) 
    { 
     return GetOrCreate(key, null, null, null, factory, onRemoveCallback); 
    } 

    public TValue GetOrCreate<TValue>(string key, DateTime timestamp, Func<TValue> factory, Action<bool> onRemoveCallback) 
    { 
     return GetOrCreate(key, timestamp, null, null, factory, onRemoveCallback); 
    } 

    public TValue GetOrCreate<TValue>(string key, TimeSpan duration, Func<TValue> factory, Action<bool> onRemoveCallback) 
    { 
     return GetOrCreate(key, null, duration, null, factory, onRemoveCallback); 
    } 

    public TValue GetOrCreate<TValue>(string key, IEnumerable<string> fileDependencies, Func<TValue> factory) 
    { 
     return GetOrCreate(key, null, null, fileDependencies, factory, null); 
    } 

    public TValue GetOrCreate<TValue>(string key, DateTime timestamp, IEnumerable<string> fileDependencies, Func<TValue> factory) 
    { 
     return GetOrCreate(key, timestamp, null, fileDependencies, factory, null); 
    } 

    public TValue GetOrCreate<TValue>(string key, TimeSpan duration, IEnumerable<string> fileDependencies, Func<TValue> factory) 
    { 
     return GetOrCreate(key, null, duration, fileDependencies, factory, null); 
    } 

    public TValue GetOrCreate<TValue>(string key, IEnumerable<string> fileDependencies, Func<TValue> factory, Action<bool> onRemoveCallback) 
    { 
     return GetOrCreate(key, null, null, fileDependencies, factory, onRemoveCallback); 
    } 

    public TValue GetOrCreate<TValue>(string key, DateTime timestamp, IEnumerable<string> fileDependencies, Func<TValue> factory, Action<bool> onRemoveCallback) 
    { 
     return GetOrCreate(key, timestamp, null, fileDependencies, factory, onRemoveCallback); 
    } 

    public TValue GetOrCreate<TValue>(string key, TimeSpan duration, IEnumerable<string> fileDependencies, Func<TValue> factory, Action<bool> onRemoveCallback) 
    { 
     return GetOrCreate(key, null, duration, fileDependencies, factory, onRemoveCallback); 
    } 

    public void Remove(string key) 
    { 
     HttpRuntime.Cache.Remove(MakeKey(key)); 
    } 

    private static string MakeKey(string key) 
    { 
     Check.Argument.IsNotNullOrEmpty(key, "key"); 

     return keyPrefix + ":" + key; 
    } 

    private void Set<TValue>(string key, DateTime? timestamp, TimeSpan? duration, IEnumerable<string> fileDependencies, TValue value, Action<bool> onRemoveCallback) 
    { 
     string fullKey = MakeKey(key); 

     lock (syncLock) 
     { 
      cache.Remove(fullKey); 

      InsertInCache(fullKey, value, fileDependencies, timestamp, duration, onRemoveCallback); 
     } 
    } 

    private TValue GetOrCreate<TValue>(string key, DateTime? timestamp, TimeSpan? duration, IEnumerable<string> fileDependencies, Func<TValue> factory, Action<bool> onRemoveCallback) 
    { 
     string fullKey = MakeKey(key); 

     object value = cache.Get(fullKey); 

     if (value == null) 
     { 
      lock (syncLock) 
      { 
       value = cache.Get(fullKey); 

       if (value == null) 
       { 
        value = factory(); 

        if (value != null) 
        { 
         InsertInCache(fullKey, value, fileDependencies, timestamp, duration, onRemoveCallback); 
        } 
       } 
      } 
     } 

     return (TValue)value; 
    } 

    private void InsertInCache(string key, object value, IEnumerable<string> fileDependencies, DateTime? timestamp, TimeSpan? duration, Action<bool> onRemoveCallback) 
    { 
     Action<string, object, CacheItemRemovedReason> raiseOnRemoveCallback = (cacheKey, state, reason) => onRemoveCallback(reason == CacheItemRemovedReason.DependencyChanged); 

     cache.Add(key, value, fileDependencies != null ? new CacheDependency(fileDependencies.ToArray()) : null, timestamp ?? Cache.NoAbsoluteExpiration, duration ?? Cache.NoSlidingExpiration, CacheItemPriority.Normal, onRemoveCallback != null ? new CacheItemRemovedCallback(raiseOnRemoveCallback) : null); 
    } 
} 

现在,您可以更改您的代码如下所示:

public ActionResult ShowImage(string imageID) 
    { 
     CacheManager cacheManager = new CacheManager(); 
     byte[] image = cacheManager.GetOrCreate(
        imageID, //Cache Key 
        DateTime.Now.AddMinutes(20), //Will be in Cache for 20 min 
        () => { 
        WebServiceClient client = new WebServiceClient(); 
        return client.GetImage(Convert.ToInt64(imageID)); 
     }); //Delegate to call if the cache is empty 

     if (image == null) 
     { 
      return new EmptyResult(); 
     } 
     return File(image,"image/png"); 
    }