CacheMaintenanceSample.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using BestHTTP;
  5. #if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
  6. using BestHTTP.Caching;
  7. #endif
  8. namespace BestHTTP.Examples
  9. {
  10. public sealed class CacheMaintenanceSample : MonoBehaviour
  11. {
  12. /// <summary>
  13. /// An enum for better readability
  14. /// </summary>
  15. enum DeleteOlderTypes
  16. {
  17. Days,
  18. Hours,
  19. Mins,
  20. Secs
  21. };
  22. #region Private Fields
  23. #if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
  24. /// <summary>
  25. /// What methode to call on the TimeSpan
  26. /// </summary>
  27. DeleteOlderTypes deleteOlderType = DeleteOlderTypes.Secs;
  28. /// <summary>
  29. /// The value for the TimeSpan.
  30. /// </summary>
  31. int value = 10;
  32. /// <summary>
  33. /// What's our maximum cache size
  34. /// </summary>
  35. int maxCacheSize = 5 * 1024 * 1024;
  36. #endif
  37. #endregion
  38. #region Unity Events
  39. void OnGUI()
  40. {
  41. GUIHelper.DrawArea(GUIHelper.ClientArea, true, () =>
  42. {
  43. #if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
  44. GUILayout.BeginHorizontal();
  45. GUILayout.Label("Delete cached entities older then");
  46. GUILayout.Label(value.ToString(), GUILayout.MinWidth(50));
  47. value = (int)GUILayout.HorizontalSlider(value, 1, 60, GUILayout.MinWidth(100));
  48. GUILayout.Space(10);
  49. deleteOlderType = (DeleteOlderTypes)(int)GUILayout.SelectionGrid((int)deleteOlderType, new string[] { "Days", "Hours", "Mins", "Secs" }, 4);
  50. GUILayout.FlexibleSpace();
  51. GUILayout.EndHorizontal();
  52. GUILayout.Space(10);
  53. GUILayout.BeginHorizontal();
  54. GUILayout.Label("Max Cache Size (bytes): ", GUILayout.Width(150));
  55. GUILayout.Label(maxCacheSize.ToString("N0"), GUILayout.Width(70));
  56. maxCacheSize = (int)GUILayout.HorizontalSlider(maxCacheSize, 1024, 10 * 1024 * 1024);
  57. GUILayout.EndHorizontal();
  58. GUILayout.Space(10);
  59. if (GUILayout.Button("Maintenance"))
  60. {
  61. TimeSpan deleteOlder = TimeSpan.FromDays(14);
  62. switch (deleteOlderType)
  63. {
  64. case DeleteOlderTypes.Days: deleteOlder = TimeSpan.FromDays(value); break;
  65. case DeleteOlderTypes.Hours: deleteOlder = TimeSpan.FromHours(value); break;
  66. case DeleteOlderTypes.Mins: deleteOlder = TimeSpan.FromMinutes(value); break;
  67. case DeleteOlderTypes.Secs: deleteOlder = TimeSpan.FromSeconds(value); break;
  68. }
  69. // Call the BeginMaintainence function. It will run on a thread to do not block the main thread.
  70. HTTPCacheService.BeginMaintainence(new HTTPCacheMaintananceParams(deleteOlder, (ulong)maxCacheSize));
  71. }
  72. #endif
  73. });
  74. }
  75. #endregion
  76. }
  77. }