Example #1
0
        /// <summary>
        /// Deletes all expired cache entity.
        /// <remarks>Call it only if there no requests currently processed, because cache entries can be deleted while a server sends back a 304 result, so there will be no data to read from the cache!</remarks>
        /// </summary>
        public static void BeginMaintainence(HTTPCacheMaintananceParams maintananceParam)
        {
            if (maintananceParam == null)
            {
                throw new ArgumentNullException("maintananceParams == null");
            }

            if (!HTTPCacheService.IsSupported)
            {
                return;
            }

            if (InMaintainenceThread)
            {
                return;
            }

            InMaintainenceThread = true;

            SetupCacheFolder();

            PlatformSupport.Threading.ThreadedRunner.RunShortLiving(MaintananceImpl, maintananceParam);
        }
        /// <summary>
        /// Deletes all expired cache entity.
        /// <remarks>Call it only if there no requests currently processed, becouse cache entries can be deleted while a server sends back a 304 result, so there will be no data to read from the cache!</remarks>
        /// </summary>
        public static void BeginMaintainence(HTTPCacheMaintananceParams maintananceParam)
        {
            #if !UNITY_WEBPLAYER
            if (maintananceParam == null)
                throw new ArgumentNullException("maintananceParams == null");

            if (InMaintainenceThread)
                return;

            InMaintainenceThread = true;

            SetupCacheFolder();

            #if !NETFX_CORE
            ThreadPool.QueueUserWorkItem(new WaitCallback((param) =>
            #else
            Windows.System.Threading.ThreadPool.RunAsync((param) =>
            #endif
                {
                    try
                    {
                        lock (Library)
                        {
                            // Delete cache entries older than the given time.
                            DateTime deleteOlderAccessed = DateTime.UtcNow - maintananceParam.DeleteOlder;
                            List<Uri> removedEntities = new List<Uri>();
                            foreach (var kvp in Library)
                                if (kvp.Value.LastAccess < deleteOlderAccessed)
                                {
                                    if (DeleteEntity(kvp.Key, false))
                                        removedEntities.Add(kvp.Key);
                                }

                            for (int i = 0; i < removedEntities.Count; ++i)
                                Library.Remove(removedEntities[i]);
                            removedEntities.Clear();

                            ulong cacheSize = GetCacheSize();

                            // This step will delete all entries starting with the oldest LastAccess property while the cache size greater then the MaxCacheSize in the given param.
                            if (cacheSize > maintananceParam.MaxCacheSize)
                            {
                                List<HTTPCacheFileInfo> fileInfos = new List<HTTPCacheFileInfo>(library.Count);

                                foreach(var kvp in library)
                                    fileInfos.Add(kvp.Value);

                                fileInfos.Sort();

                                int idx = 0;
                                while (cacheSize >= maintananceParam.MaxCacheSize && idx < fileInfos.Count)
                                {
                                    try
                                    {
                                        var fi = fileInfos[idx];
                                        ulong length = (ulong)fi.BodyLength;

                                        DeleteEntity(fi.Uri);

                                        cacheSize -= length;
                                    }
                                    catch
                                    {}
                                    finally
                                    {
                                        ++idx;
                                    }
                                }
                            }
                        }
                    }
                    finally
                    {
                        SaveLibrary();
                        InMaintainenceThread = false;
                    }
                }
            #if !NETFX_CORE
                )
            #endif
                );
            #endif
        }
Example #3
0
        /// <summary>
        /// Deletes all expired cache entity.
        /// <remarks>Call it only if there no requests currently processed, becouse cache entries can be deleted while a server sends back a 304 result, so there will be no data to read from the cache!</remarks>
        /// </summary>
        public static void BeginMaintainence(HTTPCacheMaintananceParams maintananceParam)
        {
#if !UNITY_WEBPLAYER
            if (maintananceParam == null)
            {
                throw new ArgumentNullException("maintananceParams == null");
            }

            if (InMaintainenceThread)
            {
                return;
            }

            InMaintainenceThread = true;

            SetupCacheFolder();

    #if !NETFX_CORE
            //ThreadPool.QueueUserWorkItem(new WaitCallback((param) =>
            new Thread((param) =>
    #else
            Windows.System.Threading.ThreadPool.RunAsync((param) =>
    #endif
            {
                try
                {
                    lock (Library)
                    {
                        // Delete cache entries older than the given time.
                        DateTime deleteOlderAccessed = DateTime.UtcNow - maintananceParam.DeleteOlder;
                        List <Uri> removedEntities   = new List <Uri>();
                        foreach (var kvp in Library)
                        {
                            if (kvp.Value.LastAccess < deleteOlderAccessed)
                            {
                                if (DeleteEntity(kvp.Key, false))
                                {
                                    removedEntities.Add(kvp.Key);
                                }
                            }
                        }

                        for (int i = 0; i < removedEntities.Count; ++i)
                        {
                            Library.Remove(removedEntities[i]);
                        }
                        removedEntities.Clear();

                        ulong cacheSize = GetCacheSize();

                        // This step will delete all entries starting with the oldest LastAccess property while the cache size greater then the MaxCacheSize in the given param.
                        if (cacheSize > maintananceParam.MaxCacheSize)
                        {
                            List <HTTPCacheFileInfo> fileInfos = new List <HTTPCacheFileInfo>(library.Count);

                            foreach (var kvp in library)
                            {
                                fileInfos.Add(kvp.Value);
                            }

                            fileInfos.Sort();

                            int idx = 0;
                            while (cacheSize >= maintananceParam.MaxCacheSize && idx < fileInfos.Count)
                            {
                                try
                                {
                                    var fi       = fileInfos[idx];
                                    ulong length = (ulong)fi.BodyLength;

                                    DeleteEntity(fi.Uri);

                                    cacheSize -= length;
                                }
                                catch
                                {}
                                finally
                                {
                                    ++idx;
                                }
                            }
                        }
                    }
                }
                finally
                {
                    SaveLibrary();
                    InMaintainenceThread = false;
                }
            }
    #if !NETFX_CORE
                       ).Start();
Example #4
0
        private static void MaintananceImpl(HTTPCacheMaintananceParams maintananceParam)
        {
            CheckSetup();

            rwLock.EnterWriteLock();
            try
            {
                // Delete cache entries older than the given time.
                DateTime deleteOlderAccessed             = DateTime.UtcNow - maintananceParam.DeleteOlder;
                List <HTTPCacheFileInfo> removedEntities = new List <HTTPCacheFileInfo>();
                foreach (var kvp in library)
                {
                    if (kvp.Value.LastAccess < deleteOlderAccessed)
                    {
                        if (DeleteEntity(kvp.Key, false))
                        {
                            removedEntities.Add(kvp.Value);
                        }
                    }
                }

                for (int i = 0; i < removedEntities.Count; ++i)
                {
                    library.Remove(removedEntities[i].Uri);
                    UsedIndexes.Remove(removedEntities[i].MappedNameIDX);
                }
                removedEntities.Clear();

                ulong cacheSize = GetCacheSize();

                // This step will delete all entries starting with the oldest LastAccess property while the cache size greater then the MaxCacheSize in the given param.
                if (cacheSize > maintananceParam.MaxCacheSize)
                {
                    List <HTTPCacheFileInfo> fileInfos = new List <HTTPCacheFileInfo>(library.Count);

                    foreach (var kvp in library)
                    {
                        fileInfos.Add(kvp.Value);
                    }

                    fileInfos.Sort();

                    int idx = 0;
                    while (cacheSize >= maintananceParam.MaxCacheSize && idx < fileInfos.Count)
                    {
                        try
                        {
                            var   fi     = fileInfos[idx];
                            ulong length = (ulong)fi.BodyLength;

                            DeleteEntity(fi.Uri);

                            cacheSize -= length;
                        }
                        catch
                        { }
                        finally
                        {
                            ++idx;
                        }
                    }
                }
            }
            finally
            {
                InMaintainenceThread = false;
                rwLock.ExitWriteLock();

                SaveLibrary();
            }
        }
 public static void BeginMaintainence(HTTPCacheMaintananceParams maintananceParams)
 {
     if (maintananceParams == null)
     {
         throw new ArgumentNullException("maintananceParams == null");
     }
     if (!InMaintainenceThread)
     {
         InMaintainenceThread = true;
         SetupCacheFolder();
         ThreadPool.QueueUserWorkItem(delegate(object maintananceParam)
         {
             HTTPCacheMaintananceParams hTTPCacheMaintananceParams = maintananceParam as HTTPCacheMaintananceParams;
             try
             {
                 lock (Library)
                 {
                     DateTime t = DateTime.UtcNow - hTTPCacheMaintananceParams.DeleteOlder;
                     foreach (KeyValuePair <Uri, HTTPCacheFileInfo> item in Library)
                     {
                         if (item.Value.LastAccess < t)
                         {
                             DeleteEntity(item.Key);
                         }
                     }
                     ulong num = GetCacheSize();
                     if (num > hTTPCacheMaintananceParams.MaxCacheSize)
                     {
                         List <HTTPCacheFileInfo> list = new List <HTTPCacheFileInfo>(library.Count);
                         foreach (KeyValuePair <Uri, HTTPCacheFileInfo> item2 in library)
                         {
                             list.Add(item2.Value);
                         }
                         list.Sort();
                         int num2 = 0;
                         while (num >= hTTPCacheMaintananceParams.MaxCacheSize && num2 < list.Count)
                         {
                             try
                             {
                                 HTTPCacheFileInfo hTTPCacheFileInfo = list[num2];
                                 ulong num3 = (ulong)hTTPCacheFileInfo.BodyLength;
                                 DeleteEntity(hTTPCacheFileInfo.Uri);
                                 num -= num3;
                             }
                             catch
                             {
                             }
                             finally
                             {
                                 num2++;
                             }
                         }
                     }
                 }
             }
             finally
             {
                 SaveLibrary();
                 InMaintainenceThread = false;
             }
         }, maintananceParams);
     }
 }
Example #6
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();
 }