caching
This commit is contained in:
parent
8b10c3b27e
commit
6333a95b25
2 changed files with 89 additions and 21 deletions
|
|
@ -1,6 +1,7 @@
|
|||
using System.Buffers;
|
||||
using System.IO.Pipelines;
|
||||
using Microsoft.AspNetCore.Http.Json;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using NetVips;
|
||||
using Oh.My.Stitcher;
|
||||
using Validation;
|
||||
|
|
@ -8,6 +9,7 @@ using ZLogger;
|
|||
|
||||
WebApplicationBuilder builder = WebApplication.CreateSlimBuilder(args);
|
||||
builder.Logging.ClearProviders().AddZLoggerConsole();
|
||||
builder.Services.AddMemoryCache();
|
||||
builder.Services.Configure<JsonOptions>(options =>
|
||||
{
|
||||
options.SerializerOptions.TypeInfoResolver = StitchSerializerContext.Default;
|
||||
|
|
@ -18,45 +20,71 @@ ILoggerFactory loggerFactory = app.Services.GetRequiredService<ILoggerFactory>()
|
|||
ILogger logger = loggerFactory.CreateLogger<Program>();
|
||||
|
||||
string? tilesDirectory = Environment.GetEnvironmentVariable("ASSET_PATH_RO");
|
||||
string cacheDirectory = Path.Combine(Path.GetTempPath(), "oh-my-stitch");
|
||||
Directory.CreateDirectory(cacheDirectory);
|
||||
|
||||
// sanity check
|
||||
Assumes.NotNullOrEmpty(tilesDirectory);
|
||||
Assumes.True(File.Exists(Path.Combine(tilesDirectory, "A1.png")));
|
||||
Assumes.True(File.Exists(Path.Combine(tilesDirectory, "AE55.png")));
|
||||
|
||||
|
||||
app.UseDefaultFiles();
|
||||
app.UseStaticFiles();
|
||||
app.MapPost("/api/image/generate", (Stitch request) =>
|
||||
app.MapPost("/api/image/generate", (Stitch request, IMemoryCache cache) =>
|
||||
{
|
||||
Pipe pipe = new();
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
Image? image = null;
|
||||
List<Image> images = [];
|
||||
try
|
||||
{
|
||||
using Image image = Tile.Create(in request, tilesDirectory, images, logger);
|
||||
bool created = Tile.TryCreate(in request, tilesDirectory, images, logger, cache, out image,
|
||||
out string? cacheKey, out string? cacheFile);
|
||||
if( !created && cacheFile != null )
|
||||
{
|
||||
logger.ZLogDebug($"cache hit key: {cacheKey}, file: {cacheFile}");
|
||||
await using FileStream cacheStream = new(cacheFile, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
await cacheStream.CopyToAsync(pipe.Writer.AsStream());
|
||||
return;
|
||||
}
|
||||
|
||||
if( cacheKey == null )
|
||||
{
|
||||
image?.WriteToStream(pipe.Writer.AsStream(), ".png");
|
||||
return;
|
||||
}
|
||||
|
||||
Pipe innerPipe = new();
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
string newCacheFile = Path.Combine(cacheDirectory, $"{cacheKey}.png");
|
||||
MemoryCacheEntryOptions cacheEntryOptions = new MemoryCacheEntryOptions()
|
||||
.SetSlidingExpiration(TimeSpan.FromMinutes(10))
|
||||
.RegisterPostEvictionCallback((_, value, _, _) =>
|
||||
{
|
||||
if( value is string path )
|
||||
File.Delete(path);
|
||||
});
|
||||
logger.ZLogDebug($"save cache key: {cacheKey}, file: {newCacheFile}");
|
||||
cache.Set(cacheKey!, newCacheFile, cacheEntryOptions);
|
||||
|
||||
await using FileStream cacheStream = new(newCacheFile, FileMode.Create, FileAccess.Write, FileShare.Read);
|
||||
while( true )
|
||||
{
|
||||
ReadResult result = await innerPipe.Reader.ReadAsync();
|
||||
ReadOnlySequence<byte> buffer = result.Buffer;
|
||||
if(!buffer.IsEmpty )
|
||||
{
|
||||
foreach( ReadOnlyMemory<byte> segment in buffer )
|
||||
{
|
||||
await pipe.Writer.WriteAsync(segment);
|
||||
}
|
||||
innerPipe.Reader.AdvanceTo(buffer.End);
|
||||
if (result.IsCompleted)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
foreach( ReadOnlyMemory<byte> segment in buffer )
|
||||
await Task.WhenAll(pipe.Writer.WriteAsync(segment).AsTask(), cacheStream.WriteAsync(segment).AsTask());
|
||||
|
||||
innerPipe.Reader.AdvanceTo(buffer.End);
|
||||
if( result.IsCompleted )
|
||||
break;
|
||||
}
|
||||
});
|
||||
image.WriteToStream(innerPipe.Writer.AsStream(), ".png");
|
||||
image?.WriteToStream(innerPipe.Writer.AsStream(), ".png");
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
|
|
@ -66,6 +94,7 @@ app.MapPost("/api/image/generate", (Stitch request) =>
|
|||
}
|
||||
finally
|
||||
{
|
||||
image?.Dispose();
|
||||
foreach( Image img in images )
|
||||
img.Dispose();
|
||||
await pipe.Writer.CompleteAsync();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue