コード例 #1
0
        ///// <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 cacheKey = CacheKeyFactory.GenerateKeyFromUri(componentUri, CacheRegion);

            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);
        }