diff --git a/src/Orchard.Web/Modules/Orchard.MediaProcessing/Services/ImageProcessingFileNameProvider.cs b/src/Orchard.Web/Modules/Orchard.MediaProcessing/Services/ImageProcessingFileNameProvider.cs index fd760d9fe..5cf703e88 100644 --- a/src/Orchard.Web/Modules/Orchard.MediaProcessing/Services/ImageProcessingFileNameProvider.cs +++ b/src/Orchard.Web/Modules/Orchard.MediaProcessing/Services/ImageProcessingFileNameProvider.cs @@ -16,30 +16,38 @@ namespace Orchard.MediaProcessing.Services { } public string GetFileName(string profile, string path) { - var fileNames = GetFileNames(profile); + var cacheKey = GetCacheKey(profile, path); + var profileCache = GetProfileCache(profile); + string fileName; - if (!fileNames.TryGetValue(path, out fileName)) { + if (!profileCache.TryGetValue(cacheKey, out fileName)) { var profilePart = _imageProfileService.GetImageProfileByName(profile); if (profilePart != null) { foreach (var fileNameRecord in profilePart.FileNames) { - fileNames.Add(path, fileNameRecord.FileName); + var fileNameRecordCacheKey = GetCacheKey(fileNameRecord.ImageProfilePartRecord.Name, fileNameRecord.Path); + profileCache.Add(fileNameRecordCacheKey, fileNameRecord.FileName); } - // now the cache has been initialized, call the same method again - return GetFileName(profile, path); + //now the cache has been initialized, look in the dictionary again + if (!profileCache.TryGetValue(cacheKey, out fileName)) { + return null; + } } } return fileName; } public void UpdateFileName(string profile, string path, string fileName) { - var fileNames = GetFileNames(profile); + var cacheKey = GetCacheKey(profile, path); + var profileCache = GetProfileCache(profile); + string existingFileName; - if (fileNames.TryGetValue(path, out existingFileName) && existingFileName == fileName) { + if (profileCache.TryGetValue(cacheKey, out existingFileName) && existingFileName == fileName) { return; } - fileNames[path] = fileName; + + profileCache[cacheKey] = fileName; var profilePart = _imageProfileService.GetImageProfileByName(profile); // profile might not exist in the db if its a dynamic profile @@ -56,7 +64,11 @@ namespace Orchard.MediaProcessing.Services { } } - private Dictionary GetFileNames(string profile) { + private static string GetCacheKey(string profile, string path) { + return profile + "_" + path; + } + + private IDictionary GetProfileCache(string profile) { return _cacheManager.Get("MediaProcessing_" + profile, ctx => { ctx.Monitor(_signals.When("MediaProcessing_" + profile + "_Saved")); return new Dictionary(); diff --git a/src/Orchard.Web/Modules/Orchard.MediaProcessing/Shapes/MediaShapes.cs b/src/Orchard.Web/Modules/Orchard.MediaProcessing/Shapes/MediaShapes.cs index acc4b5fad..ef3ef031a 100644 --- a/src/Orchard.Web/Modules/Orchard.MediaProcessing/Shapes/MediaShapes.cs +++ b/src/Orchard.Web/Modules/Orchard.MediaProcessing/Shapes/MediaShapes.cs @@ -74,34 +74,32 @@ namespace Orchard.MediaProcessing.Shapes { [Shape] public void MediaUrl(dynamic Shape, dynamic Display, TextWriter Output, string Profile, string Path, ContentItem ContentItem, FilterRecord CustomFilter) { - + try { Shape.IgnoreShapeTracer = true; + var services = new Lazy(() => _services.Value); + var storageProvider = new Lazy(() => _storageProvider.Value); - var services = new Lazy(() => _services.Value); - var storageProvider = new Lazy(() => _storageProvider.Value); + // try to load the processed filename from cache + var filePath = _fileNameProvider.Value.GetFileName(Profile, Path); + bool process = false; - // try to load the processed filename from cache - var filePath = _fileNameProvider.Value.GetFileName(Profile, Path); - bool process = false; - - // if the filename is not cached, process it - if (!string.IsNullOrEmpty(filePath)) { - process = true; - } - - // the processd file doesn't exist anymore, process it - else if (!storageProvider.Value.FileExists(filePath)) { + // if the filename is not cached, process it + if (!string.IsNullOrEmpty(filePath)) { process = true; - } + } + + // the processd file doesn't exist anymore, process it + else if (!storageProvider.Value.FileExists(filePath)) { + process = true; + } - // if the original file is more recent, process it - else if (storageProvider.Value.GetFile(Path).GetLastUpdated() > storageProvider.Value.GetFile(filePath).GetLastUpdated()) { - process = true; - } - - // todo: regenerate the file if the profile is newer, by deleting the associated filename cache entries. - if (process) { - try { + // if the original file is more recent, process it + else if (storageProvider.Value.GetFile(Path).GetLastUpdated() > storageProvider.Value.GetFile(filePath).GetLastUpdated()) { + process = true; + } + + // todo: regenerate the file if the profile is newer, by deleting the associated filename cache entries. + if (process) { ImageProfilePart profilePart; if (CustomFilter == null) { @@ -158,16 +156,15 @@ namespace Orchard.MediaProcessing.Shapes { filePath = filterContext.FilePath; } } - catch (Exception ex) { - Logger.Error(ex, "An error occured while processing {0} for image {1}", Profile, Path); - return; - } - } - // generate a timestamped url to force client caches to update if the file has changed - var publicUrl = storageProvider.Value.GetPublicUrl(filePath); - var timestamp = storageProvider.Value.GetFile(storageProvider.Value.GetLocalPath(filePath)).GetLastUpdated().Ticks; - Output.Write(publicUrl + "?v=" + timestamp.ToString(CultureInfo.InvariantCulture)); + // generate a timestamped url to force client caches to update if the file has changed + var publicUrl = storageProvider.Value.GetPublicUrl(filePath); + var timestamp = storageProvider.Value.GetFile(storageProvider.Value.GetLocalPath(filePath)).GetLastUpdated().Ticks; + Output.Write(publicUrl + "?v=" + timestamp.ToString(CultureInfo.InvariantCulture)); + } + catch (Exception ex) { + Logger.Error(ex, "An error occured while rendering shape {0} for image {1}", Profile, Path); + } } // TODO: Update this method once the storage provider has been updated