/// <summary> /// Adds the specified key and value to the dictionary or returns the value if it exists. /// </summary> /// <param name="cachedImage"> /// The cached image to add. /// </param> /// <returns> /// The value of the item to add or get. /// </returns> public static CachedImage Add(CachedImage cachedImage) { // Add the CachedImage. CacheItemPolicy policy = new CacheItemPolicy(); policy.ChangeMonitors.Add(new HostFileChangeMonitor(new List<string> { cachedImage.Path })); MemCache.AddItem(cachedImage.Key, cachedImage, policy); return cachedImage; }
/// <summary> /// Adds the specified key and value to the dictionary or returns the value if it exists. /// </summary> /// <param name="cachedImage"> /// The cached image to add. /// </param> /// <returns> /// The value of the item to add or get. /// </returns> public static CachedImage Add(CachedImage cachedImage) { // Add the CachedImage with a sliding expiration of 1 minutes. CacheItemPolicy policy = new CacheItemPolicy { SlidingExpiration = new TimeSpan(0, 1, 0) }; if (new Uri(cachedImage.Path).IsFile) { policy.ChangeMonitors.Add(new HostFileChangeMonitor(new List<string> { cachedImage.Path })); MemCache.AddItem(Path.GetFileNameWithoutExtension(cachedImage.Key), cachedImage, policy); } else { MemCache.AddItem(Path.GetFileNameWithoutExtension(cachedImage.Key), cachedImage, policy); } return cachedImage; }
/// <summary> /// Returns a value indicating whether the original file is new or has been updated. /// </summary> /// <param name="cachedPath"> /// The path to the cached image. /// </param> /// <returns> /// True if The original file is new or has been updated; otherwise, false. /// </returns> public bool IsNewOrUpdatedFile(string cachedPath) { bool isUpdated = false; CachedImage cachedImage = CacheIndexer.GetValue(cachedPath); 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 (IsExpired(cachedImage.CreationTimeUtc)) { CacheIndexer.Remove(cachedPath); isUpdated = true; } } return(isUpdated); }
/// <summary> /// Adds the specified key and value to the dictionary or returns the value if it exists. /// </summary> /// <param name="cachedImage"> /// The cached image to add. /// </param> /// <returns> /// The value of the item to add or get. /// </returns> public static CachedImage Add(CachedImage cachedImage) { // Add the CachedImage with a sliding expiration of 10 minutes. CacheItemPolicy policy = new CacheItemPolicy { SlidingExpiration = new TimeSpan(0, 10, 0) }; if (new Uri(cachedImage.Path).IsFile) { policy.ChangeMonitors.Add(new HostFileChangeMonitor(new List <string> { cachedImage.Path })); MemCache.AddItem(Path.GetFileNameWithoutExtension(cachedImage.Key), cachedImage, policy); } else { MemCache.AddItem(Path.GetFileNameWithoutExtension(cachedImage.Key), cachedImage, policy); } return(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() { 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="lastWriteTimeUtc"> /// The last write time. /// </param> internal void AddImageToCache(DateTime lastWriteTimeUtc) { string key = Path.GetFileNameWithoutExtension(this.CachedPath); CachedImage cachedImage = new CachedImage { Key = key, Path = this.CachedPath, LastWriteTimeUtc = lastWriteTimeUtc }; CacheIndexer.Add(cachedImage); }
/// <summary> /// Gets a value indicating whether the image is new or updated in an asynchronous manner. /// </summary> /// <returns> /// The asynchronous <see cref="Task"/> returning the value. /// </returns> public override async Task<bool> IsNewOrUpdatedAsync() { 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)); this.CachedPath = Path.Combine(this.cloudCachedBlobContainer.Uri.ToString(), pathFromKey, cachedFileName).Replace(@"\", "/"); this.cachedRewritePath = Path.Combine(this.cachedCdnRoot, this.cloudCachedBlobContainer.Name, pathFromKey, cachedFileName).Replace(@"\", "/"); bool isUpdated = false; CachedImage cachedImage = CacheIndexer.Get(this.CachedPath); if (new Uri(this.CachedPath).IsFile) { 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) { string blobPath = this.CachedPath.Substring(this.cloudCachedBlobContainer.Uri.ToString().Length + 1); CloudBlockBlob blockBlob = this.cloudCachedBlobContainer.GetBlockBlobReference(blobPath); if (await blockBlob.ExistsAsync()) { // Pull the latest info. await blockBlob.FetchAttributesAsync(); if (blockBlob.Properties.LastModified.HasValue) { cachedImage = new CachedImage { Key = Path.GetFileNameWithoutExtension(this.CachedPath), Path = this.CachedPath, CreationTimeUtc = blockBlob.Properties.LastModified.Value.UtcDateTime }; 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> /// Removes a cached image from the database. /// </summary> /// <param name="cachedImage"> /// The key for the cached image. /// </param> /// <returns> /// The true if the addition of the cached image is removed; otherwise, false. /// </returns> internal static async Task<int> RemoveImageAsync(CachedImage cachedImage) { try { SQLiteAsyncConnection connection = new SQLiteAsyncConnection(ConnectionString); return await connection.DeleteAsync(cachedImage); } catch { return 0; } }
/// <summary> /// Adds a cached image to the database. /// </summary> /// <param name="image"> /// The cached image to add. /// </param> /// <returns> /// The true if the addition of the cached image is added; otherwise, false. /// </returns> internal static async Task<int> AddImageAsync(CachedImage image) { try { SQLiteAsyncConnection connection = new SQLiteAsyncConnection(ConnectionString); return await connection.InsertAsync(image); } catch { return 0; } }
/// <summary> /// Adds an image to the cache. /// </summary> /// <param name="lastWriteTimeUtc"> /// The last write time. /// </param> /// <returns> /// The <see cref="T:System.Threading.Tasks.Task"/>. /// </returns> internal async Task AddImageToCacheAsync(DateTime lastWriteTimeUtc) { string key = Path.GetFileNameWithoutExtension(this.CachedPath); DateTime expires = DateTime.UtcNow.AddDays(MaxFileCachedDuration).ToUniversalTime(); CachedImage cachedImage = new CachedImage { Key = key, Path = this.CachedPath, MaxAge = MaxFileCachedDuration, LastWriteTimeUtc = lastWriteTimeUtc, ExpiresUtc = expires }; await PersistantDictionary.Instance.AddAsync(key, cachedImage); }
/// <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); }
/// <summary> /// Adds an image to the cache. /// </summary> internal void AddImageToCache() { string key = Path.GetFileNameWithoutExtension(this.CachedPath); CachedImage cachedImage = new CachedImage { Key = key, Path = this.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 asynchronous <see cref="Task"/> returning the value. /// </returns> public override async Task<bool> IsNewOrUpdatedAsync() { 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)); this.CachedPath = Path.Combine(this.cachedCdnRoot, this.imageProcessorCachePrefix, pathFromKey, cachedFileName) .Replace(@"\", "/"); // TODO: What is the S3 version of the following lines? The Above doesn't match what I expect //this.CachedPath = Path.Combine(this.cloudCachedBlobContainer.Uri.ToString(), pathFromKey, cachedFileName).Replace(@"\", "/"); //this.cachedRewritePath = Path.Combine(this.cachedCdnRoot, this.cloudCachedBlobContainer.Name, pathFromKey, cachedFileName).Replace(@"\", "/"); bool isUpdated = false; CachedImage cachedImage = CacheIndexer.Get(this.CachedPath); if (new Uri(this.CachedPath).IsFile) { 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) { try { string path = this.GetFolderStructureForAmazon(this.CachedPath); string filename = Path.GetFileName(this.CachedPath); string key = this.GetKey(path, filename); GetObjectMetadataRequest objectMetaDataRequest = new GetObjectMetadataRequest { BucketName = this.awsBucketName, Key = key, }; GetObjectMetadataResponse response = await this.amazonS3ClientCache.GetObjectMetadataAsync(objectMetaDataRequest); if (response != null) { cachedImage = new CachedImage { Key = key, Path = this.CachedPath, CreationTimeUtc = response.LastModified.ToUniversalTime() }; CacheIndexer.Add(cachedImage); } } catch (AmazonS3Exception) { // Nothing in S3 so we should return true. isUpdated = true; } } 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 a <see cref="CachedImage"/> to the cache. /// </summary> /// <param name="cachedImage"> /// The cached image to add. /// </param> /// <param name="expiry"> /// The number of minutes to cache the image, defaults to 1. /// </param> /// <returns> /// The value of the item to add or get. /// </returns> public static CachedImage Add(CachedImage cachedImage, int expiry) => Add(cachedImage, new TimeSpan(0, expiry, 0));