public virtual bool TryFindPage(string url, out IPage page) { LoggerService.Debug(">>TryFindPage ({0}", LoggingCategory.Performance, url); page = null; string cacheKey = CacheKeyFactory.GenerateKey(CacheRegion, url, Convert.ToString(PublicationId)); LoggerService.Debug("about to load page from cache with key {0}", LoggingCategory.Performance, cacheKey); page = (IPage)CacheAgent.Load(cacheKey); LoggerService.Debug("finished loading page from cache with key {0}, page found = {1}", LoggingCategory.Performance, cacheKey, Convert.ToString(page != null)); if (page != null) { if (page.Title == CacheValueNullTitle) { page = null; return(false); } LoggerService.Debug("<<TryFindPage ({0}", LoggingCategory.Performance, url); return(true); } else { LoggerService.Debug("about to load page content from provider with url {0}", LoggingCategory.Performance, url); string pageContentFromBroker = PageProvider.GetContentByUrl(url); LoggerService.Debug("finished loading page content from provider with url {0}, has value: {1}", LoggingCategory.Performance, url, Convert.ToString(!(string.IsNullOrEmpty(pageContentFromBroker)))); if (string.IsNullOrEmpty(pageContentFromBroker)) { CacheAgent.Store(cacheKey, CacheRegion404, CacheValueNull); } else { LoggerService.Debug("about to create IPage from content for url {0}", LoggingCategory.Performance, url); page = GetIPageObject(pageContentFromBroker); if (IncludeLastPublishedDate) { ((Page)page).LastPublishedDate = PageProvider.GetLastPublishedDateByUrl(url); } LoggerService.Debug("finished creating IPage from content for url {0}", LoggingCategory.Performance, url); LoggerService.Debug("about to store page in cache with key {0}", LoggingCategory.Performance, cacheKey); CacheAgent.Store(cacheKey, CacheRegion, page, new List <string> { page.Id }); LoggerService.Debug("finished storing page in cache with key {0}", LoggingCategory.Performance, cacheKey); LoggerService.Debug("<<TryFindPage ({0}", LoggingCategory.Performance, url); return(true); } } LoggerService.Debug("<<TryFindPage ({0}", LoggingCategory.Performance, url); return(false); }
///// <summary> ///// Get or set the CacheAgent ///// </summary> //public override ICacheAgent CacheAgent //{ // get // { // if (_cacheAgent == null) // { // _cacheAgent = new NullCacheAgent(); // // the next line is the only reason we are overriding this property: to set a callback // _cacheAgent.GetLastPublishDateCallBack = GetLastPublishedDateCallBack; // } // return _cacheAgent; // } // set // { // _cacheAgent = value; // _cacheAgent.GetLastPublishDateCallBack = GetLastPublishedDateCallBack; // } //} public bool TryGetComponentPresentation(out IComponentPresentation cp, string componentUri, string templateUri = "") { cp = null; string[] cacheUris = { componentUri }; if (!String.IsNullOrEmpty(templateUri)) { cacheUris = new string[] { componentUri, templateUri }; } string cacheKey = CacheKeyFactory.GenerateKey(CacheRegion, cacheUris); cp = (IComponentPresentation)CacheAgent.Load(cacheKey); if (cp != null) { LoggerService.Debug("<<TryGetComponentPresentation ({0}) - from cache", LoggingCategory.Performance, componentUri); return(true); } string content = !String.IsNullOrEmpty(templateUri) ? ComponentPresentationProvider.GetContent(componentUri, templateUri) : ComponentPresentationProvider.GetContent(componentUri); if (string.IsNullOrEmpty(content)) { LoggerService.Debug("<<TryGetComponentPresentationOrComponent - no content found by provider for uris {0} and {1}", LoggingCategory.Performance, componentUri, templateUri); return(false); } LoggerService.Debug("about to create IComponentPresentation from content ({0})", LoggingCategory.Performance, componentUri); cp = GetIComponentPresentationObject(content); LoggerService.Debug("finished creating IComponentPresentation from content ({0})", LoggingCategory.Performance, componentUri); // if there is no ComponentTemplate, the content of this CP probably represents a component instead of a component PRESENTATION // in that case, we should at least add the template uri method parameter (if there is one) to the object if (cp.ComponentTemplate == null) { ((ComponentPresentation)cp).ComponentTemplate = new ComponentTemplate(); } if (cp.ComponentTemplate.Id == null) { ((ComponentPresentation)cp).ComponentTemplate.Id = templateUri; } LoggerService.Debug("about to store IComponentPresentation in cache ({0})", LoggingCategory.Performance, componentUri); CacheAgent.Store(cacheKey, CacheRegion, cp, new List <string> { cp.Component.Id }); LoggerService.Debug("finished storing IComponentPresentation in cache ({0})", LoggingCategory.Performance, componentUri); LoggerService.Debug("<<TryGetComponentPresentation ({0})", LoggingCategory.Performance, componentUri); return(cp != null); }
public IBinaryMeta FindBinaryMeta(string url) { string cacheKey = CacheKeyFactory.GenerateKey(url); IBinaryMeta binaryMeta = CacheAgent.Load(cacheKey) as IBinaryMeta; if (binaryMeta != null) { return(binaryMeta); } binaryMeta = BinaryProvider.GetBinaryMetaByUrl(url); if (binaryMeta == null) // TODO: cache null result { return(null); } CacheAgent.Store(cacheKey, "Binary", binaryMeta, new List <string> { binaryMeta.Id }); return(binaryMeta); }
private bool TryFindBinary(string url, string localPath, bool retrieveData, out IBinary binary) { string physicalPath = localPath ?? Path.Combine(Configuration.BinaryFileSystemCachePath, Path.GetFileName(url)); LoggerService.Debug($"Using physical path {physicalPath}"); binary = new Binary(); Dimensions dimensions = null; string urlWithoutDimensions = StripDimensions(url, out dimensions); if (LoadBinariesAsStream) { LoggerService.Information("retrieving binaries as a stream is obsolete; support will be dropped in future versions of DD4T"); binary.BinaryStream = BinaryProvider.GetBinaryStreamByUrl(urlWithoutDimensions); if (binary.BinaryStream == null) { return(false); } } string cacheKey = CacheKeyFactory.GenerateKey(urlWithoutDimensions); try { IBinaryMeta binaryMeta = CacheAgent.Load(cacheKey) as IBinaryMeta; bool metaWasInCache = true; if (binaryMeta == null) { metaWasInCache = false; binaryMeta = BinaryProvider.GetBinaryMetaByUrl(urlWithoutDimensions); if (binaryMeta == null) { throw new BinaryNotFoundException(); } CacheAgent.Store(cacheKey, "Binary", binaryMeta, new List <string> { binaryMeta.Id }); } if (FileExistsAndIsNotEmpty(physicalPath)) { if (binaryMeta.HasLastPublishedDate || metaWasInCache) { DateTime fileModifiedDate = File.GetLastWriteTime(physicalPath); if (fileModifiedDate.CompareTo(binaryMeta.LastPublishedDate) >= 0) { LoggerService.Debug("binary {0} is still up to date, no action required", urlWithoutDimensions); // TODO: load bytes from file system into binary if (retrieveData) { FillBinaryFromLocalFS(binary, physicalPath); } CopyBinaryMetaToBinary(binaryMeta, binary); return(true); } } } // the normal situation (where a binary is still in Tridion and it is present on the file system already and it is up to date) is now covered // Let's handle the exception situations. byte[] bytes = BinaryProvider.GetBinaryByUrl(urlWithoutDimensions); if (bytes == null || bytes.Length == 0) { throw new BinaryNotFoundException(); } bool fileIsCreated = WriteBinaryToFile(bytes, physicalPath, dimensions); if (!fileIsCreated) { LoggerService.Warning($"file '{physicalPath}' could not be created, binary {binary.Id} cannot be returned"); return(false); } if (retrieveData) { if (dimensions == null) { binary.BinaryData = bytes; } else { FillBinaryFromLocalFS(binary, physicalPath); } } CopyBinaryMetaToBinary(binaryMeta, binary); return(true); } catch (BinaryNotFoundException) { LoggerService.Debug("Binary with url {0} not found", urlWithoutDimensions); // binary does not exist in Tridion, it should be removed from the local file system too DeleteFile(physicalPath); return(false); } catch (Exception e) { LoggerService.Warning($"Caught unexpected exception while retrieving binary with url {urlWithoutDimensions} (requested url: {url}. Error message: {e.Message}\r\n{e.StackTrace}"); // in case of error, the binary should be removed from the local file system too DeleteFile(physicalPath); throw e; } }