/// <summary> /// Deletes all cache files /// </summary> public static void DeleteCache() { LunaLog.Log("[LMP]: Deleting cache!"); foreach (var cacheFile in GetCachedFiles()) { File.Delete(cacheFile); } FileLengths.Clear(); FileCreationTimes.Clear(); CurrentCacheSize = 0; }
private long LengthGet(FileInfo instance) { var result = default(long); string path = null; if (FilePaths.TryGetValue(instance, out path)) { FileLengths.TryGetValue(path, out result); } return(result); }
/// <summary> /// Delete old cache files or if size is bigger than the limit. /// </summary> public static void ExpireCache() { LunaLog.Log("[LMP]: Expiring cache!"); //No folder, no delete. if (!Directory.Exists(CommonUtil.CombinePaths(CacheDirectory, "Incoming"))) { LunaLog.Log("[LMP]: No sync cache folder, skipping expire."); return; } //Delete partial incoming files var incomingFiles = Directory.GetFiles(CommonUtil.CombinePaths(CacheDirectory, "Incoming")); foreach (var incomingFile in incomingFiles) { LunaLog.Log($"[LMP]: Deleting partially cached object {incomingFile}"); File.Delete(incomingFile); } //Delete old files var cacheObjects = GetCachedObjects(); CurrentCacheSize = 0; foreach (var cacheObject in cacheObjects) { var cacheFile = CommonUtil.CombinePaths(CacheDirectory, $"{cacheObject}.txt"); //If the file is older than a week, delete it. if (File.GetCreationTime(cacheFile).AddDays(7d) < DateTime.Now) { LunaLog.Log($"[LMP]: Deleting cached object {cacheObject}, reason: Expired!"); File.Delete(cacheFile); } else { var fi = new FileInfo(cacheFile); FileCreationTimes[cacheObject] = fi.CreationTime; FileLengths[cacheObject] = fi.Length; CurrentCacheSize += fi.Length; } } //While the directory is over (cacheSize) MB while (CacheSizeExceeded()) { var deleteObject = string.Empty; //Find oldest file foreach (var testFile in FileCreationTimes) { if (string.IsNullOrEmpty(deleteObject)) { deleteObject = testFile.Key; } if (testFile.Value < FileCreationTimes[deleteObject]) { deleteObject = testFile.Key; } } LunaLog.Log($"[LMP]: Deleting cached object {deleteObject}, reason: Cache full!"); var deleteFile = CommonUtil.CombinePaths(CacheDirectory, $"{deleteObject}.txt"); File.Delete(deleteFile); CurrentCacheSize -= FileLengths[deleteObject]; if (FileCreationTimes.ContainsKey(deleteObject)) { FileCreationTimes.Remove(deleteObject); } if (FileLengths.ContainsKey(deleteObject)) { FileLengths.Remove(deleteObject); } } }