예제 #1
0
 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;
     }
 }
예제 #2
0
 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
         {
         }
     }
 }
예제 #3
0
        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);
        }
예제 #4
0
 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
     {
     }
 }
예제 #5
0
 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();
 }