Sinet.CacheService/Program.cs

46 lines
1.2 KiB
C#
Raw Normal View History

2023-08-25 14:10:51 +08:00
using Microsoft.Extensions.Caching.Distributed;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddStackExchangeRedisCache(options =>
{
options.Configuration = builder.Configuration["Redis"];
options.InstanceName = "CacheData_";
});
var app = builder.Build();
var CacheDictionary = new Dictionary<string, string>();
// Configure the HTTP request pipeline.
app.MapPost("/CacheData/Set", async (HttpRequest request, IDistributedCache cache, string key) =>
{
string body = "";
using (StreamReader stream = new StreamReader(request.Body))
{
body = await stream.ReadToEndAsync();
}
cache.SetString(key, body);
2023-09-14 11:29:55 +08:00
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} [{key}[update to [{body}] ");
2023-08-25 14:10:51 +08:00
return "success";
});
app.MapGet("/CacheData/Get", (HttpContext httpContext, IDistributedCache cache, string key) =>
{
var cacheString = cache.GetString(key);
if (!string.IsNullOrWhiteSpace(cacheString))
{
httpContext.Response.ContentType = "application/json";
return cacheString;
}
else
{
httpContext.Response.StatusCode = 404;
return string.Empty;
}
});
app.Run();