private byte[] TryGetFromFileSystemCache(string path, DateTime remoteLastModified) { byte[] data = null; var fileLastModified = DateTime.MinValue; var tempFilePath = AzurePathHelper.GetTempBlobPath(path); if (System.IO.File.Exists(tempFilePath)) { fileLastModified = System.IO.File.GetLastWriteTimeUtc(tempFilePath); using (var stream = new System.IO.FileStream(tempFilePath, System.IO.FileMode.Open)) { data = new byte[stream.Length]; stream.Read(data, 0, data.Length); } } // no data in cache or outdated if (data == null || fileLastModified < remoteLastModified) { if (data != null) { Discard(path); } return(null); } return(data); }
private void End_Execute(object sender, EventArgs e) { // remove azure temp folder var tempFolder = AzurePathHelper.GetTempBlobPath(string.Empty); if (!string.IsNullOrEmpty(tempFolder) && System.IO.Directory.Exists(tempFolder)) { System.IO.Directory.Delete(tempFolder, true); } }
private void AddToFileSystem(string path, byte[] data, DateTime created) { var tempFilePath = AzurePathHelper.GetTempBlobPath(path); System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(tempFilePath)); using (var stream = new System.IO.FileStream(tempFilePath, System.IO.FileMode.Create)) { stream.Write(data, 0, data.Length); } // make sure the last write time matches cloud timestamp System.IO.File.SetLastWriteTimeUtc(tempFilePath, created); }
public void Discard(string path) { switch (_cacheType) { case BlobCacheType.Memory: CacheHelper.TouchKey(BlobCacheHelper.GetCacheDependency(path)); break; case BlobCacheType.FileSystem: var tempFilePath = AzurePathHelper.GetTempBlobPath(path); if (System.IO.File.Exists(tempFilePath)) { System.IO.File.Delete(tempFilePath); } break; } }
public string Execute(TaskInfo task) { if (_blobCacheService.CacheType != BlobCacheType.FileSystem) { return("Caching of Azure data is disabled or is set to memory. No need to run this task."); } var blobCacheService = Service <IBlobCacheService> .Entry(); var minutes = AccountInfo.Instance.BlobCacheMinutes; var dateThreshold = DateTime.UtcNow.AddMinutes(-minutes); var blobsToDelete = BlobCollection.Instance.GetOutdatedBlobPaths(dateThreshold); var blobsDeleted = 0; var directoriesUninitialized = 0; foreach (var path in blobsToDelete) { // remove the blob blobCacheService.Discard(path); blobsDeleted++; } // clear empty folders in file system if (blobsDeleted > 0) { var folders = System.IO.Directory.GetDirectories(AzurePathHelper.GetTempBlobPath(string.Empty), "*", System.IO.SearchOption.AllDirectories).OrderByDescending(p => p.Length); foreach (var subFolder in folders) { if (System.IO.Directory.Exists(subFolder) && !System.IO.Directory.GetFiles(subFolder).Any() && !System.IO.Directory.EnumerateDirectories(subFolder).Any()) { System.IO.Directory.Delete(subFolder, false); } } } return("OK, discarded metadata of " + blobsDeleted + " blobs, " + directoriesUninitialized + " dirs"); }
public void RemovesCachedFile() { _blobCacheService.SetCacheType(BlobCacheType.FileSystem); using (var ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes("string"))) { Get("RemovesCachedFile.txt").Upload(ms); } var cachePath = AzurePathHelper.GetTempBlobPath(Get("RemovesCachedFile.txt").Path); // cache was created in file system Assert.IsTrue(System.IO.File.Exists(cachePath)); Get("RemovesCachedFile.txt").Delete(); // cache was removed Assert.IsFalse(System.IO.File.Exists(cachePath)); Assert.AreEqual(3, _cloudBlobService.History.Count); }
public bool IsCached(string path) { var tempFilePath = AzurePathHelper.GetTempBlobPath(path); return(System.IO.File.Exists(tempFilePath)); }