/// <summary> /// Removing oldest cache file (file, which last access time is smaller) /// </summary> private bool RemoveOldestCacheFile() { if (_lastAccessTimeDictionary.Count == 0) { return(false); } var oldestCacheFilePath = _lastAccessTimeDictionary.Aggregate((pair1, pair2) => (pair1.Value < pair2.Value)? pair1 : pair2).Key; if (String.IsNullOrEmpty(oldestCacheFilePath)) { return(false); } try { long fileSizeInBytes; using (var file = ISF.OpenFile(oldestCacheFilePath, FileMode.Open, FileAccess.Read)) { fileSizeInBytes = file.Length; } try { ISF.DeleteFile(oldestCacheFilePath); _lastAccessTimeDictionary.Remove(oldestCacheFilePath); CurrentCacheSizeInBytes -= fileSizeInBytes; // Updating current cache size JetImageLoader.Log("[delete] cache file " + oldestCacheFilePath); return(true); } catch { JetImageLoader.Log("[error] can not delete oldest cache file: " + oldestCacheFilePath); } } catch { JetImageLoader.Log("[error] can not get olders cache's file size: " + oldestCacheFilePath); } return(false); }
private void BeginCountCurrentCacheSize() { Task.Factory.StartNew(() => { // Pattern to match all innerFiles and innerDirectories inside absoluteDirPath var filesAndDirectoriesPattern = CacheDirectory + @"\*"; string[] cacheFileNames; try { cacheFileNames = ISF.GetFileNames(Path.Combine(CacheDirectory, filesAndDirectoriesPattern)); } catch { return; } long cacheSizeInBytes = 0; foreach (var cacheFileName in cacheFileNames) { var fullCacheFilePath = Path.Combine(CacheDirectory, cacheFileName); try { using (var file = ISF.OpenFile(fullCacheFilePath, FileMode.Open, FileAccess.Read)) { cacheSizeInBytes += file.Length; _lastAccessTimeDictionary.Add(fullCacheFilePath, DateTimeUtil.ConvertDateTimeToMillis(ISF.GetLastAccessTime(fullCacheFilePath).DateTime)); } } catch { JetImageLoader.Log("[error] can not get cache's file size: " + fullCacheFilePath); } } CurrentCacheSizeInBytes += cacheSizeInBytes; // Updating current cache size }); }