public static Add ( |
||
cachedImage | /// The cached image to add. /// | |
리턴 |
/// <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> /// Adds an image to the cache. /// </summary> /// <param name="cachedPath"> /// The path to the cached image. /// </param> public void AddImageToCache(string cachedPath) { string key = Path.GetFileNameWithoutExtension(cachedPath); CachedImage cachedImage = new CachedImage { Key = key, Path = cachedPath, CreationTimeUtc = DateTime.UtcNow }; CacheIndexer.Add(cachedImage); }
/// <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); }
/// <summary> /// Adds an image to the cache. /// </summary> /// <param name="creationAndLastWriteDateTimes"> /// The creation and last write times. /// </param> internal void AddImageToCache(Tuple <DateTime, DateTime> creationAndLastWriteDateTimes) { string key = Path.GetFileNameWithoutExtension(this.CachedPath); CachedImage cachedImage = new CachedImage { Key = key, Path = this.CachedPath, CreationTimeUtc = creationAndLastWriteDateTimes.Item1, LastWriteTimeUtc = creationAndLastWriteDateTimes.Item2 }; CacheIndexer.Add(cachedImage); }