FrameCache.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Coffee.UIParticleInternal
  5. {
  6. internal static class FrameCache
  7. {
  8. private static readonly Dictionary<Type, IFrameCache> s_Caches = new Dictionary<Type, IFrameCache>();
  9. static FrameCache()
  10. {
  11. s_Caches.Clear();
  12. UIExtraCallbacks.onLateAfterCanvasRebuild += ClearAllCache;
  13. }
  14. #if UNITY_EDITOR
  15. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
  16. private static void Clear()
  17. {
  18. s_Caches.Clear();
  19. }
  20. #endif
  21. /// <summary>
  22. /// Tries to retrieve a value from the frame cache with a specified key.
  23. /// </summary>
  24. public static bool TryGet<T>(object key1, string key2, out T result)
  25. {
  26. return GetFrameCache<T>().TryGet((key1.GetHashCode(), key2.GetHashCode()), out result);
  27. }
  28. /// <summary>
  29. /// Tries to retrieve a value from the frame cache with a specified key.
  30. /// </summary>
  31. public static bool TryGet<T>(object key1, string key2, int key3, out T result)
  32. {
  33. return GetFrameCache<T>().TryGet((key1.GetHashCode(), key2.GetHashCode() + key3), out result);
  34. }
  35. /// <summary>
  36. /// Sets a value in the frame cache with a specified key.
  37. /// </summary>
  38. public static void Set<T>(object key1, string key2, T result)
  39. {
  40. GetFrameCache<T>().Set((key1.GetHashCode(), key2.GetHashCode()), result);
  41. }
  42. /// <summary>
  43. /// Sets a value in the frame cache with a specified key.
  44. /// </summary>
  45. public static void Set<T>(object key1, string key2, int key3, T result)
  46. {
  47. GetFrameCache<T>().Set((key1.GetHashCode(), key2.GetHashCode() + key3), result);
  48. }
  49. private static void ClearAllCache()
  50. {
  51. foreach (var cache in s_Caches.Values)
  52. {
  53. cache.Clear();
  54. }
  55. }
  56. private static FrameCacheContainer<T> GetFrameCache<T>()
  57. {
  58. var t = typeof(T);
  59. if (s_Caches.TryGetValue(t, out var frameCache)) return frameCache as FrameCacheContainer<T>;
  60. frameCache = new FrameCacheContainer<T>();
  61. s_Caches.Add(t, frameCache);
  62. return (FrameCacheContainer<T>)frameCache;
  63. }
  64. private interface IFrameCache
  65. {
  66. void Clear();
  67. }
  68. private class FrameCacheContainer<T> : IFrameCache
  69. {
  70. private readonly Dictionary<(int, int), T> _caches = new Dictionary<(int, int), T>();
  71. public void Clear()
  72. {
  73. _caches.Clear();
  74. }
  75. public bool TryGet((int, int) key, out T result)
  76. {
  77. return _caches.TryGetValue(key, out result);
  78. }
  79. public void Set((int, int) key, T result)
  80. {
  81. _caches[key] = result;
  82. }
  83. }
  84. }
  85. }