public override bool AddStream(Media media, MediaOptions options, MediaStream stream, out MediaStream cachedStream) { /* STOCK METHOD (Decompiled) */ Assert.ArgumentNotNull(media, "media"); Assert.ArgumentNotNull(options, "options"); Assert.ArgumentNotNull(stream, "stream"); cachedStream = null; if (!CanCache(media, options)) { return(false); } MediaCacheRecord cacheRecord = CreateCacheRecord(media, options, stream); if (cacheRecord == null) { return(false); } cachedStream = cacheRecord.GetStream(); if (cachedStream == null) { return(false); } AddToActiveList(cacheRecord); /* END STOCK */ // we store the site context because on the background thread: without the Sitecore context saved (on a worker thread), that disables the media cache var currentSite = Context.Site; cacheRecord.PersistAsync((() => OnAfterPersist(cacheRecord, currentSite))); return(true); }
protected virtual void OnAfterPersist(MediaCacheRecord record, SiteContext originalSiteContext) { string mediaPath = string.Empty; try { RemoveFromActiveList(record); // Housekeeping: since we call AddStream() to insert the optimized version, we have to keep AddStream() from calling OnAfterPersist() from that call, causing an optimization loop var id = record.Media.MediaData.MediaId; if (StreamsInOptimization.Contains(id)) { lock (OptimizeLock) { if (StreamsInOptimization.Contains(id)) { StreamsInOptimization.Remove(id); return; } } } lock (OptimizeLock) { StreamsInOptimization.Add(id); } // grab the stream from cache and optimize it if (!record.HasStream) return; var stream = record.GetStream(); if (!_optimizer.CanOptimize(stream)) return; var optimizedStream = _optimizer.Process(stream, record.Options); mediaPath = stream.MediaItem.MediaPath; if (optimizedStream == null) { Log.Info("Dianoga: async optimazation result was null, not doing any optimizing for {0}".FormatWith(mediaPath), this); return; } // we have to switch the site context because we're on a background thread, and Sitecore.Context is lost (so the site is always null) // if the site is null the media cache ignores all added entries using (new SiteContextSwitcher(originalSiteContext)) { for (int i = 0; i < 5; i++) { try { // only here to satisfy out param MediaStream dgafStream; bool success = AddStream(record.Media, record.Options, optimizedStream, out dgafStream); if (dgafStream != null) dgafStream.Dispose(); if (!success) Log.Warn("Dianoga: The media cache rejected adding {0}. This is unexpected!".FormatWith(mediaPath), this); } catch (Exception ex) { Log.Error("Dianoga: Error occurred adding stream to media cache. Will retry in 10 sec.", ex, this); Thread.Sleep(10000); continue; } break; } } } catch (Exception ex) { // this runs in a background thread, and an exception here would cause IIS to terminate the app pool. Bad! So we catch/log, just in case. Log.Error("Dianoga: Exception occurred on the background thread when optimizing: {0}".FormatWith(mediaPath), ex, this); } }
protected virtual void OnAfterPersist(MediaCacheRecord record, SiteContext originalSiteContext) { string mediaPath = string.Empty; try { RemoveFromActiveList(record); // Housekeeping: since we call AddStream() to insert the optimized version, we have to keep AddStream() from calling OnAfterPersist() from that call, causing an optimization loop var id = record.Media.MediaData.MediaId; if (StreamsInOptimization.Contains(id)) { lock (OptimizeLock) { if (StreamsInOptimization.Contains(id)) { StreamsInOptimization.Remove(id); return; } } } lock (OptimizeLock) { StreamsInOptimization.Add(id); } // grab the stream from cache and optimize it if (!record.HasStream) { return; } var stream = record.GetStream(); if (!_optimizer.CanOptimize(stream)) { return; } var optimizedStream = _optimizer.Process(stream, record.Options); mediaPath = stream.MediaItem.MediaPath; if (optimizedStream == null) { Log.Info("Dianoga: async optimazation result was null, not doing any optimizing for {0}".FormatWith(mediaPath), this); return; } // we have to switch the site context because we're on a background thread, and Sitecore.Context is lost (so the site is always null) // if the site is null the media cache ignores all added entries using (new SiteContextSwitcher(originalSiteContext)) { for (int i = 0; i < 5; i++) { try { // only here to satisfy out param MediaStream dgafStream; bool success = AddStream(record.Media, record.Options, optimizedStream, out dgafStream); if (dgafStream != null) { dgafStream.Dispose(); } if (!success) { Log.Warn("Dianoga: The media cache rejected adding {0}. This is unexpected!".FormatWith(mediaPath), this); } } catch (Exception ex) { Log.Error("Dianoga: Error occurred adding stream to media cache. Will retry in 10 sec.", ex, this); Thread.Sleep(10000); continue; } break; } } } catch (Exception ex) { // this runs in a background thread, and an exception here would cause IIS to terminate the app pool. Bad! So we catch/log, just in case. Log.Error("Dianoga: Exception occurred on the background thread when optimizing: {0}".FormatWith(mediaPath), ex, this); } }