main
forget_the_bright 2 years ago
parent f27ac51a79
commit 8cde999b5b

@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using WebMVCApi.Models; using WebMVCApi.Models;
namespace WebMVCApi.Controllers namespace WebMVCApi.Controllers
@ -12,6 +13,13 @@ namespace WebMVCApi.Controllers
[ApiController] [ApiController]
public class TestController : ControllerBase public class TestController : ControllerBase
{ {
private IMemoryCache _memoryCache;
public TestController(IMemoryCache _memoryCache) {
this._memoryCache= _memoryCache;
}
[HttpGet("GetPerson")] [HttpGet("GetPerson")]
public Person GetPerson() public Person GetPerson()
{ {
@ -61,9 +69,11 @@ namespace WebMVCApi.Controllers
return new { Id = classNom+6, schoolName=schoolName + "扛把子", }; return new { Id = classNom+6, schoolName=schoolName + "扛把子", };
} }
[HttpPut("UpdatePerson")] [HttpPut("UpdatePerson")]
public ActionResult<Object> UpdatePerson(int id, Person p1) public async Task<Object> UpdatePerson(int id, Person p1)
{ {
return $"id {id} 的用户{p1.Name}更新成功"; return _memoryCache.GetOrCreateAsync<String>("val", async (e)=>{
return $"id {id} 的用户{p1.Name}更新成功 {DateTime.Now}";
});
} }
} }
} }

@ -6,7 +6,7 @@ builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
builder.Services.AddMemoryCache();
var app = builder.Build(); var app = builder.Build();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
@ -19,7 +19,7 @@ if (app.Environment.IsDevelopment())
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.UseAuthorization(); app.UseAuthorization();
app.UseResponseCaching(); //app.UseResponseCaching();
app.MapControllers(); app.MapControllers();
app.Run(); app.Run();

Loading…
Cancel
Save