Skip to content

Commit b6ec0c5

Browse files
authored
Merge pull request #13 from IvanMurzak/concurrent-memory-cache
Concurrent memory cache
2 parents 343a6ef + 09dd7b9 commit b6ec0c5

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

Assets/_PackageRoot/Runtime/ImageLoader.MemoryCache.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
using System.Collections.Concurrent;
12
using System.Collections.Generic;
23
using UnityEngine;
34

45
namespace Extensions.Unity.ImageLoader
56
{
67
public static partial class ImageLoader
78
{
8-
internal static Dictionary<string, Sprite> memorySpriteCache = new Dictionary<string, Sprite>();
9+
internal static ConcurrentDictionary<string, Sprite> memorySpriteCache = new ConcurrentDictionary<string, Sprite>();
910

1011
#if UNITY_EDITOR
1112
[UnityEditor.InitializeOnEnterPlayMode]
@@ -60,11 +61,11 @@ public static Sprite LoadFromMemoryCache(string url)
6061
/// <param name="url">URL to the picture, web or local</param>
6162
public static void ClearMemoryCache(string url)
6263
{
63-
var cache = memorySpriteCache.GetValueOrDefault(url);
64-
if (cache?.texture != null)
65-
UnityEngine.Object.DestroyImmediate(cache.texture);
66-
67-
memorySpriteCache.Remove(url);
64+
if (memorySpriteCache.Remove(url, out var cache))
65+
{
66+
if (cache?.texture != null)
67+
UnityEngine.Object.DestroyImmediate(cache.texture);
68+
}
6869
}
6970
/// <summary>
7071
/// Clear Memory cache for all urls

0 commit comments

Comments
 (0)