Skip to content

Commit

Permalink
Fixed concurrency issue. Added ClearCache(url)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanMurzak committed May 12, 2024
1 parent 0301812 commit 488b7d1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
10 changes: 8 additions & 2 deletions Assets/_PackageRoot/Runtime/ImageLoader.LoadSprite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,19 @@ static async void InternalLoadSprite(Future<Sprite> future, Vector2 pivot, Textu
request = UnityWebRequestTexture.GetTexture(future.Url);
request.timeout = (int)Math.Ceiling(settings.timeout.TotalSeconds);
var asyncOperation = request.SendWebRequest();
await asyncOperation.WithCancellation(future.CancellationToken);
future = future.Canceled(request.Abort);
await request.SendWebRequest();
}
catch (OperationCanceledException)
{
future.Cancel();
}
catch (TimeoutException e)
{
RemoveLoading(future); // LOADING REMOVED
future.FailToLoad(e);
return;
}
catch (Exception e)
{
if (settings.debugLevel <= DebugLevel.Exception && !ignoreImageNotFoundError)
Expand Down
35 changes: 29 additions & 6 deletions Assets/_PackageRoot/Runtime/ImageLoader.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using System.Linq;

namespace Extensions.Unity.ImageLoader
{
public static partial class ImageLoader
{
private static Dictionary<string, Future<Sprite>> loadingInProcess = new Dictionary<string, Future<Sprite>>();
private static ConcurrentDictionary<string, Future<Sprite>> loadingInProcess = new ConcurrentDictionary<string, Future<Sprite>>();
private static void AddLoading(Future<Sprite> future)
{
loadingInProcess.Add(future.Url, future);
if (!loadingInProcess.TryAdd(future.Url, future))
throw new Exception($"[ImageLoader] AddLoading: {future.Url} already loading");

if (settings.debugLevel <= DebugLevel.Log)
Debug.Log($"[ImageLoader] AddLoading: {future.Url}, total {loadingInProcess.Count} loading tasks");
}
private static void RemoveLoading(Future<Sprite> future)
private static void RemoveLoading(Future<Sprite> future) => RemoveLoading(future.Url);
private static void RemoveLoading(string url)
{
if (loadingInProcess.Remove(future.Url))
if (loadingInProcess.TryRemove(url, out var future))
{
if (settings.debugLevel <= DebugLevel.Log)
Debug.Log($"[ImageLoader] RemoveLoading: {future.Url}, left {loadingInProcess.Count} loading tasks");
Debug.Log($"[ImageLoader] RemoveLoading: {url}, left {loadingInProcess.Count} loading tasks");
}
else
{
if (settings.debugLevel <= DebugLevel.Warning)
Debug.LogWarning($"[ImageLoader] RemoveLoading: {url} not found in loading tasks");
}
}

Expand Down Expand Up @@ -48,17 +59,29 @@ public static void Init()
/// Return all current loading Futures
/// </summary>
/// <returns>Returns read only list of all current loading Futures</returns>
public static IReadOnlyCollection<Future<Sprite>> GetLoadingFutures() => loadingInProcess.Values;
public static IReadOnlyCollection<Future<Sprite>> GetLoadingFutures() => loadingInProcess.Values.ToArray();

/// <summary>
/// Clear cache from Memory and Disk layers for all urls
/// </summary>
/// <returns>Returns task of the disk cache clearing process</returns>
public static Task ClearCache()
{
ClearMemoryCache();
return ClearDiskCache();
}

/// <summary>
/// Clear cache from Memory and Disk layers for all urls
/// </summary>
/// <param name="url">URL to the picture, web or local</param>
/// <returns>Returns task of the disk cache clearing process</returns>
public static Task ClearCache(string url)
{
ClearMemoryCache(url);
return ClearDiskCache(url);
}

/// <summary>
/// Checks cache at Memory and at Disk by the url
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Assets/_PackageRoot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "Ivan Murzak",
"url": "https://github.com/IvanMurzak"
},
"version": "5.1.3",
"version": "5.2.0",
"unity": "2019.2",
"description": "Asynchronous image loading from remote or local destination. It has two layers of configurable Memory and Disk cache systems.",
"dependencies": {
Expand Down

0 comments on commit 488b7d1

Please sign in to comment.