public string TryGetFromStore(string tag, out bool exists, TimeSpan timeout)
        {
            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);

            ImageBuilder.TraceSource.WriteInfo(
                TraceType,
                "Downloading {0} from store.",
                tag);

            exists = false;
            string value = null;

            string tempFileName = ImageBuilderUtility.GetTempFileName(this.localImageBuilderRoot);

            try
            {
                if (this.ImageStore.DoesContentExist(tag, timeoutHelper.GetRemainingTime()))
                {
                    this.ImageStore.DownloadContent(tag, tempFileName, timeoutHelper.GetRemainingTime(), CopyFlag.AtomicCopy);

                    ImageBuilderUtility.RemoveReadOnlyFlag(tempFileName);
                    value  = ImageBuilderUtility.ReadStringFromFile(tempFileName);
                    exists = true;
                }
            }
            finally
            {
                ImageBuilderUtility.DeleteTempLocation(tempFileName);
            }

            return(value);
        }
        public async Task <T> GetFromStoreAsync <T>(string tag, TimeSpan timeout)
        {
            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);

            ImageBuilder.TraceSource.WriteInfo(
                TraceType,
                "Downloading {0} from store async.",
                tag);

            string tempFileName = ImageBuilderUtility.GetTempFileName(this.localImageBuilderRoot);

            try
            {
                bool doesExist = await this.ImageStore.DoesContentExistAsync(tag, timeoutHelper.GetRemainingTime());

                if (doesExist)
                {
                    await this.ImageStore.DownloadContentAsync(tag, tempFileName, timeoutHelper.GetRemainingTime(), CopyFlag.AtomicCopy);

                    ImageBuilderUtility.RemoveReadOnlyFlag(tempFileName);
                    return(ImageBuilderUtility.ReadXml <T>(tempFileName));
                }
                else
                {
                    throw new FileNotFoundException(
                              string.Format(CultureInfo.CurrentCulture, StringResources.ImageStoreError_FileNotFound, tag));
                }
            }
            finally
            {
                ImageBuilderUtility.DeleteTempLocation(tempFileName);
            }
        }
        public async Task SetToStoreAsync <T>(string tag, T value, TimeSpan timeout, bool shouldOverwrite = true)
        {
            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);

            ImageBuilder.TraceSource.WriteInfo(
                TraceType,
                "Uploading {0} to store. ShouldOverwrite: {1}.",
                tag,
                shouldOverwrite);

            if (!shouldOverwrite &&
                await this.ImageStore.DoesContentExistAsync(tag, timeoutHelper.GetRemainingTime()))
            {
                return;
            }

            string tempFileName = ImageBuilderUtility.GetTempFileName(this.localImageBuilderRoot);

            try
            {
                ImageBuilderUtility.WriteXml <T>(tempFileName, value);
                await this.ImageStore.UploadContentAsync(tag, tempFileName, timeoutHelper.GetRemainingTime(), CopyFlag.AtomicCopy, false /*acquireSourceLock*/);
            }
            finally
            {
                ImageBuilderUtility.DeleteTempLocation(tempFileName);
            }
        }
        public async Task <Tuple <T, bool> > TryGetFromStoreAsync <T>(string tag, TimeSpan timeout)
        {
            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);

            ImageBuilder.TraceSource.WriteInfo(
                TraceType,
                "Downloading {0} from store.",
                tag);

            bool exists = false;
            T    value  = default(T);

            string tempFileName = ImageBuilderUtility.GetTempFileName(this.localImageBuilderRoot);

            try
            {
                if (await this.ImageStore.DoesContentExistAsync(tag, timeoutHelper.GetRemainingTime()))
                {
                    await this.ImageStore.DownloadContentAsync(tag, tempFileName, timeoutHelper.GetRemainingTime(), CopyFlag.AtomicCopy);

                    ImageBuilderUtility.RemoveReadOnlyFlag(tempFileName);
                    value  = ImageBuilderUtility.ReadXml <T>(tempFileName);
                    exists = true;
                }
            }
            finally
            {
                ImageBuilderUtility.DeleteTempLocation(tempFileName);
            }

            return(new Tuple <T, bool>(value, exists));
        }