public static Get ( string cachedPath ) : |
||
cachedPath | string | /// The cached path of the value to get. /// |
return |
/// <summary> /// Gets a value indicating whether the image is new or updated in an asynchronous manner. /// </summary> /// <returns> /// The <see cref="Task"/>. /// </returns> public override async Task <bool> IsNewOrUpdatedAsync() { // TODO: Before this check is performed it should be throttled. For example, only perform this check // if the last time it was checked is greater than 5 seconds. This would be much better for perf // if there is a high throughput of image requests. string cachedFileName = await this.CreateCachedFileNameAsync(); // Collision rate of about 1 in 10000 for the folder structure. // That gives us massive scope to store millions of files. string pathFromKey = string.Join("\\", cachedFileName.ToCharArray().Take(6)); string virtualPathFromKey = pathFromKey.Replace(@"\", "/"); this.CachedPath = Path.Combine(this.absoluteCachePath, pathFromKey, cachedFileName); this.virtualCachedFilePath = Path.Combine(this.virtualCachePath, virtualPathFromKey, cachedFileName).Replace(@"\", "/"); bool isUpdated = false; CachedImage cachedImage = CacheIndexer.Get(this.CachedPath); if (cachedImage == null) { FileInfo fileInfo = new FileInfo(this.CachedPath); if (fileInfo.Exists) { // Pull the latest info. fileInfo.Refresh(); cachedImage = new CachedImage { Key = Path.GetFileNameWithoutExtension(this.CachedPath), Path = this.CachedPath, CreationTimeUtc = fileInfo.CreationTimeUtc }; CacheIndexer.Add(cachedImage); } } if (cachedImage == null) { // Nothing in the cache so we should return true. isUpdated = true; } else { // Check to see if the cached image is set to expire. if (this.IsExpired(cachedImage.CreationTimeUtc)) { CacheIndexer.Remove(this.CachedPath); isUpdated = true; } } return(isUpdated); }
/// <summary> /// Gets a value indicating whether the image is new or updated in an asynchronous manner. /// </summary> /// <returns> /// The <see cref="Task"/>. /// </returns> public override async Task <bool> IsNewOrUpdatedAsync() { // TODO: Before this check is performed it should be throttled. For example, only perform this check // if the last time it was checked is greater than 5 seconds. This would be much better for perf // if there is a high throughput of image requests. string cachedFileName = await this.CreateCachedFileNameAsync().ConfigureAwait(false); this.CachedPath = CachedImageHelper.GetCachedPath(this.absoluteCachePath, cachedFileName, false, this.FolderDepth); this.virtualCachedFilePath = CachedImageHelper.GetCachedPath(this.virtualCachePath, cachedFileName, true, this.FolderDepth); bool isUpdated = false; CachedImage cachedImage = CacheIndexer.Get(this.CachedPath); if (cachedImage == null) { var info = new FileInfo(this.CachedPath); if (info.Exists) { cachedImage = new CachedImage { Key = Path.GetFileNameWithoutExtension(this.CachedPath), Path = this.CachedPath, CreationTimeUtc = info.LastWriteTimeUtc }; CacheIndexer.Add(cachedImage, this.ImageCacheMaxMinutes); } } if (cachedImage == null) { // Nothing in the cache so we should return true. isUpdated = true; } else { // Check to see if the cached image is set to expire // or a new file with the same name has replaced our current image if (this.IsExpired(cachedImage.CreationTimeUtc) || await this.IsUpdatedAsync(cachedImage.CreationTimeUtc).ConfigureAwait(false)) { CacheIndexer.Remove(this.CachedPath); isUpdated = true; } else { // Set cachedImageCreationTimeUtc so we can sender Last-Modified or ETag header when using Response.TransmitFile() this.cachedImageCreationTimeUtc = cachedImage.CreationTimeUtc; } } return(isUpdated); }