Sinet.CacheService/Program.cs

45 lines
1.1 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);
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();