Self-hosted git - made by qwen 3.8 27b
12345678910111213141516171819202122232425262728293031323334
using Microsoft.Extensions.Caching.Memory;
namespace Forge.Features.Browse;
/// <summary>
/// Cache for things derived from git objects. Keys include the object's SHA, and content under
/// a SHA never changes, so entries never need invalidating; they only age out to bound memory.
/// It owns its <see cref="MemoryCache"/> so the size limit applies to these entries alone.
/// </summary>
public sealed class ContentCache : IDisposable
{
private readonly MemoryCache _cache = new(new MemoryCacheOptions { SizeLimit = 64 * 1024 * 1024 });
public IMemoryCache Cache => _cache;
public async Task<T> GetOrCreateAsync<T>(string key, Func<Task<T>> create, Func<T, long> sizeOf)
where T : notnull
{
if (_cache.TryGetValue(key, out T? cached) && cached is not null)
{
return cached;
}
var value = await create();
_cache.Set(key, value, new MemoryCacheEntryOptions
{
Size = Math.Max(1, sizeOf(value)),
SlidingExpiration = TimeSpan.FromHours(6),
});
return value;
}
public void Dispose() => _cache.Dispose();
}