46 lines
1.2 KiB
C#
46 lines
1.2 KiB
C#
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);
|
|
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} [{key}[update to [{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(); |