/// <summary>
        /// Tries to get a cached value for a given key and cache region.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="region">The name of the cache region. Different cache regions can have different retention policies.</param>
        /// <param name="value">The cached value (output).</param>
        /// <typeparam name="T">The type of the value to get.</typeparam>
        /// <returns><c>true</c> if a cached value was found for the given key and cache region.</returns>
        public override bool TryGet <T>(string key, string region, out T value)
        {
            string cacheAgentKey = GetQualifiedKey(key, region);
            object cachedValue   = _cacheAgent.Load(cacheAgentKey);

            if (cachedValue == null)
            {
                // Maybe another thread is just adding a value for the key/region...
                bool valueAdded = AwaitAddingValue(key, region);
                if (valueAdded)
                {
                    // Another thread has just added a value for the key/region. Retry.
                    cachedValue = _cacheAgent.Load(cacheAgentKey);
                }

                if (cachedValue == null)
                {
                    Log.Debug("No value cached for key '{0}' in region '{1}'.", key, region);
                    value = default(T);
                    return(false);
                }
            }

            if (!(cachedValue is T))
            {
                throw new DxaException(
                          $"Cached value for key '{key}' in region '{region}' is of type {cachedValue.GetType().FullName} instead of {typeof (T).FullName}."
                          );
            }

            value = (T)cachedValue;
            return(true);
        }
        public override SiteMapNode BuildSiteMap()
        {
            debugCounter++;
            object result = _cacheAgent.Load(CacheKey);

            if (result != null)
            {
                if (result is string && ((string)result).Equals(CacheNullValue))
                {
                    return(null);
                }
                return(result as SiteMapNode);
            }
            SiteMapNode rootNode;

            lock (lock1)
            {
                base.Clear();
                rootNode = ReadSitemapFromXml(SiteMapPath);
                if (rootNode == null)
                {
                    // cache special 'null value' (so we do not try to load the sitemap from an invalid or non-existing XML every time!)
                    _cacheAgent.Store(CacheKey, CacheRegion, CacheNullValue);
                }
                else
                {
                    // Store the root node in the cache.
                    _cacheAgent.Store(CacheKey, CacheRegion, rootNode);
                }
            }

            return(rootNode);
        }
        /// <summary>
        /// Main worker method reads binary from Broker and stores it in file-system
        /// </summary>
        /// <returns></returns>
        public bool ProcessRequest(HttpRequest request)
        {
            string urlPath = request.Url.AbsolutePath.Replace("/BinaryData", "");

            LoggerService.Debug("Start processing " + urlPath);
            Dimensions dimensions = null;

            urlPath = StripDimensions(urlPath, out dimensions);

            String   physicalPath      = request.PhysicalPath;
            string   cacheKey          = GetCacheKey(urlPath);
            DateTime?lastPublishedDate = CacheAgent.Load(cacheKey) as DateTime?;


            if (lastPublishedDate == null)
            {
                DateTime lpb = BinaryFactory.FindLastPublishedDate(urlPath);
                if (lpb != DateTime.MinValue.AddSeconds(1)) // this is the secret code for 'does not exist'
                {
                    lastPublishedDate = new DateTime?(lpb);
                    CacheAgent.Store(cacheKey, "Binary", lastPublishedDate);
                }
            }
            if (lastPublishedDate != null)
            {
                if (File.Exists(physicalPath))
                {
                    FileInfo fi = new FileInfo(physicalPath);
                    if (fi.Length > 0)
                    {
                        DateTime fileModifiedDate = File.GetLastWriteTime(physicalPath);
                        if (fileModifiedDate.CompareTo(lastPublishedDate) >= 0)
                        {
                            LoggerService.Debug("binary {0} is still up to date, no action required", urlPath);
                            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.
            IBinary binary = null;

            try
            {
                BinaryFactory.TryFindBinary(urlPath, out binary);
            }
            catch (BinaryNotFoundException)
            {
                LoggerService.Debug("Binary with url {0} not found", urlPath);
                // binary does not exist in Tridion, it should be removed from the local file system too
                if (File.Exists(physicalPath))
                {
                    DeleteFile(physicalPath);
                }
                return(false);
            }
            return(WriteBinaryToFile(binary, physicalPath, dimensions));
        }
        public override object Get(string key, string region, IEnumerable <string> dependencies = null)
        {
            string cacheAgentKey = GetQualifiedKey(key, region);

            return(_cacheAgent.Load(cacheAgentKey));
        }