private static void ClearImpl(object param) { try { string[] files = Directory.GetFiles(HTTPCacheService.CacheFolder); for (int i = 0; i < files.Length; i++) { try { string fileName = Path.GetFileName(files[i]); HTTPCacheService.DeleteEntity(HTTPCacheService.GetUriFromFileName(fileName), true); } catch { } } } finally { HTTPCacheService.SaveLibrary(); HTTPCacheService.InClearThread = false; } }
private static void DeleteUnusedFiles() { HTTPCacheService.CheckSetup(); string[] files = Directory.GetFiles(HTTPCacheService.CacheFolder); for (int i = 0; i < files.Length; i++) { try { string fileName = Path.GetFileName(files[i]); Uri uriFromFileName = HTTPCacheService.GetUriFromFileName(fileName); Dictionary <Uri, HTTPCacheFileInfo> obj = HTTPCacheService.Library; lock (obj) { if (!HTTPCacheService.Library.ContainsKey(uriFromFileName)) { File.Delete(files[i]); } } } catch { } } }
internal static Stream PrepareStreamed(Uri uri, HTTPResponse response) { Dictionary <Uri, HTTPCacheFileInfo> obj = HTTPCacheService.Library; Stream saveStream; lock (obj) { HTTPCacheFileInfo hTTPCacheFileInfo; if (!HTTPCacheService.Library.TryGetValue(uri, out hTTPCacheFileInfo)) { HTTPCacheService.Library.Add(uri, hTTPCacheFileInfo = new HTTPCacheFileInfo(uri)); } try { saveStream = hTTPCacheFileInfo.GetSaveStream(response); } catch { HTTPCacheService.DeleteEntity(uri, true); throw; } } return(saveStream); }
private static void LoadLibrary() { if (HTTPCacheService.library != null) { return; } HTTPCacheService.library = new Dictionary <Uri, HTTPCacheFileInfo>(); if (!File.Exists(HTTPCacheService.LibraryPath)) { HTTPCacheService.DeleteUnusedFiles(); return; } try { Dictionary <Uri, HTTPCacheFileInfo> obj = HTTPCacheService.library; lock (obj) { using (FileStream fileStream = new FileStream(HTTPCacheService.LibraryPath, FileMode.Open)) { using (BinaryReader binaryReader = new BinaryReader(fileStream)) { int version = binaryReader.ReadInt32(); int num = binaryReader.ReadInt32(); for (int i = 0; i < num; i++) { Uri uri = new Uri(binaryReader.ReadString()); bool flag = File.Exists(Path.Combine(HTTPCacheService.CacheFolder, HTTPCacheService.GetFileNameFromUri(uri))); if (flag) { HTTPCacheService.library.Add(uri, new HTTPCacheFileInfo(uri, binaryReader, version)); } } } } } HTTPCacheService.DeleteUnusedFiles(); } catch { } }
public static void BeginMaintainence(HTTPCacheMaintananceParams maintananceParam) { if (maintananceParam == null) { throw new ArgumentNullException("maintananceParams == null"); } if (HTTPCacheService.InMaintainenceThread) { return; } HTTPCacheService.InMaintainenceThread = true; HTTPCacheService.SetupCacheFolder(); new Thread(delegate(object param) { try { Dictionary <Uri, HTTPCacheFileInfo> obj = HTTPCacheService.Library; lock (obj) { DateTime t = DateTime.UtcNow - maintananceParam.DeleteOlder; List <Uri> list = new List <Uri>(); foreach (KeyValuePair <Uri, HTTPCacheFileInfo> current in HTTPCacheService.Library) { if (current.Value.LastAccess < t && HTTPCacheService.DeleteEntity(current.Key, false)) { list.Add(current.Key); } } for (int i = 0; i < list.Count; i++) { HTTPCacheService.Library.Remove(list[i]); } list.Clear(); ulong num = HTTPCacheService.GetCacheSize(); if (num > maintananceParam.MaxCacheSize) { List <HTTPCacheFileInfo> list2 = new List <HTTPCacheFileInfo>(HTTPCacheService.library.Count); foreach (KeyValuePair <Uri, HTTPCacheFileInfo> current2 in HTTPCacheService.library) { list2.Add(current2.Value); } list2.Sort(); int num2 = 0; while (num >= maintananceParam.MaxCacheSize && num2 < list2.Count) { try { HTTPCacheFileInfo hTTPCacheFileInfo = list2[num2]; ulong num3 = (ulong)((long)hTTPCacheFileInfo.BodyLength); HTTPCacheService.DeleteEntity(hTTPCacheFileInfo.Uri, true); num -= num3; } catch { } finally { num2++; } } } } } finally { HTTPCacheService.SaveLibrary(); HTTPCacheService.InMaintainenceThread = false; } }).Start(); }