Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: disk cache flow #3271

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
using ECS.StreamableLoading.Cache.Disk;
using ECS.StreamableLoading.Cache.InMemory;
using System;
using System.Collections.Generic;
using System.Threading;
using Utility;
using Utility.Types;

namespace ECS.StreamableLoading.Cache.Generic
Expand All @@ -14,13 +16,15 @@ public class GenericCache<T, TKey> : IGenericCache<T, TKey>
private readonly IDiskCache<T> diskCache;
private readonly Func<TKey, string> stringifyFunc;
private readonly string extension;
private readonly Dictionary<string, object> readingKeys;

public GenericCache(IMemoryCache<T, TKey> memoryCache, IDiskCache<T> diskCache, Func<TKey, string> stringifyFunc, string extension)
{
this.memoryCache = memoryCache;
this.diskCache = diskCache;
this.stringifyFunc = stringifyFunc;
this.extension = extension;
readingKeys = new Dictionary<string, object>();
}

public UniTask<EnumResult<TaskError>> PutAsync(TKey key, T value, CancellationToken token)
Expand All @@ -29,13 +33,20 @@ public UniTask<EnumResult<TaskError>> PutAsync(TKey key, T value, CancellationTo
return diskCache.PutAsync(stringifyFunc(key)!, extension, value, token);
}

public async UniTask<EnumResult<Option<T>, TaskError>> ContentAsync(TKey key, CancellationToken token)
public UniTask<EnumResult<Option<T>, TaskError>> ReadFromCache(TKey key, CancellationToken token)
{
if (memoryCache.TryGet(key, out T result))
return EnumResult<Option<T>, TaskError>.SuccessResult(Option<T>.Some(result));
return UniTask.FromResult(EnumResult<Option<T>, TaskError>.SuccessResult(Option<T>.Some(result)));

return UniTask.FromResult(EnumResult<Option<T>, TaskError>.SuccessResult(Option<T>.None));
}

public async UniTask<EnumResult<Option<T>, TaskError>> ReadFromDisk(TKey key, CancellationToken token)
{
string stringKey = stringifyFunc(key)!;

readingKeys!.SyncTryAdd(stringKey, null);

var diskResult = await diskCache.ContentAsync(stringKey, extension, token);

if (diskResult.Success)
Expand All @@ -54,7 +65,10 @@ public async UniTask<EnumResult<Option<T>, TaskError>> ContentAsync(TKey key, Ca
$"Error getting disk cache content for '{key}' - {diskResult.Error!.Value.State} {diskResult.Error!.Value.Message}"
);

readingKeys.SyncRemove(stringKey);

return EnumResult<Option<T>, TaskError>.SuccessResult(Option<T>.None);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ public interface IGenericCache<T, TKey>
{
UniTask<EnumResult<TaskError>> PutAsync(TKey key, T value, CancellationToken token);

UniTask<EnumResult<Option<T>, TaskError>> ContentAsync(TKey key, CancellationToken token);
UniTask<EnumResult<Option<T>, TaskError>> ReadFromCache(TKey key, CancellationToken token);

UniTask<EnumResult<Option<T>, TaskError>> ReadFromDisk(TKey key, CancellationToken token);

}

public static class GenericCacheExtensions
Expand All @@ -25,7 +28,7 @@ public static async UniTask<EnumResult<Option<T>, TaskError>> ContentOrFetchAsyn
CancellationToken token
)
{
var result = await cache.ContentAsync(key, token);
var result = await cache.ReadFromCache(key, token);

if (result.Success == false || result.Value.Has)
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System;
using System.Runtime.CompilerServices;
using System.Threading;
using UnityEngine;
using Utility;

namespace ECS.StreamableLoading.Common.Systems
Expand Down Expand Up @@ -150,20 +151,19 @@ CancellationToken disposalCt
}
}

var cachedContent = await genericCache.ContentAsync(intention, disposalCt);

// Try load from cache first
var cachedContent = await genericCache.ReadFromCache(intention, disposalCt);

if (cachedContent.Success)
{
var option = cachedContent.Value;

if (option.Has)
{
result = new StreamableLoadingResult<TAsset>(option.Value);
return;
}
}

// Try load from cache first

// If the given URL failed irrecoverably just return the failure
if (cache.IrrecoverableFailures.TryGetValue(intention.CommonArguments.GetCacheableURL(), out var failure))
Expand Down Expand Up @@ -278,20 +278,31 @@ private void IncreaseRefCount(in TIntention intention, TAsset asset)

try
{
result = await RepeatLoopAsync(intention, acquiredBudget, partition, ct);

var cachedContentFromDisk = await genericCache.ReadFromDisk(intention, ct);
if (cachedContentFromDisk.Success)
{
var option = cachedContentFromDisk.Value;
if (option.Has)
result = new StreamableLoadingResult<TAsset>(option.Value);
else
result = await RepeatLoopAsync(intention, acquiredBudget, partition, ct);
}
else
{
result = await RepeatLoopAsync(intention, acquiredBudget, partition, ct);
}

// Ensure that we returned to the main thread
await UniTask.SwitchToMainThread(ct);

// before firing the continuation of the ongoing request
// Add result to the cache
if (result is { Succeeded: true })
genericCache
.PutAsync(intention, result.Value.Asset!, ct)
.Forget(
static e =>
ReportHub.LogError(ReportCategory.STREAMABLE_LOADING, $"Error putting cache content: {e.Message}")
);
{
await genericCache
.PutAsync(intention, result.Value.Asset!, ct);
}


// Set result for the reusable source
// Remove from the ongoing requests immediately because finally will be called later than
Expand Down
Loading