2012-02-14 18 views
1

データベースに格納されている自分のWebサイトにイメージを表示しています。私の剃刀の眺めから、私はこのような行を書く。データベースに格納されている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

+0

私はこれのためにAppFabricキャッシングを使用しました:http://msdn.microsoft.com/en-us/gg457898 – rboarman

答えて

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

こんにちはDarin sir、私は[Jeff Atwood](http://stackoverflow.com/questions/) 642954/iis7-cache-control)sir、Web.configを使用してイメージとCSSファイルの有効期限を設定していますが、有効期限のヘッダーは設定されていません。 [Here](http://stackoverflow.com/questions/35704618/asp-mvc-expire-headers-not-set-for-static-contents)は私の編集した質問です。 – stom

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"); 
    } 
関連する問題