新增WebDemo项目,演示如何在WebApi中使用Redis

This commit is contained in:
智能大石头 2025-04-12 01:32:50 +08:00
parent f800262e22
commit d533bc9bea
9 changed files with 164 additions and 0 deletions

View File

@ -26,6 +26,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QueueDemo", "Samples\QueueD
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Benchmark", "Samples\Benchmark\Benchmark.csproj", "{FAC752B8-087D-44A9-88E8-3B759A72F936}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebDemo", "Samples\WebDemo\WebDemo.csproj", "{E91C582B-DB9D-4489-A36B-2C850A426B63}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -56,6 +58,10 @@ Global
{FAC752B8-087D-44A9-88E8-3B759A72F936}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FAC752B8-087D-44A9-88E8-3B759A72F936}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FAC752B8-087D-44A9-88E8-3B759A72F936}.Release|Any CPU.Build.0 = Release|Any CPU
{E91C582B-DB9D-4489-A36B-2C850A426B63}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E91C582B-DB9D-4489-A36B-2C850A426B63}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E91C582B-DB9D-4489-A36B-2C850A426B63}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E91C582B-DB9D-4489-A36B-2C850A426B63}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -63,6 +69,7 @@ Global
GlobalSection(NestedProjects) = preSolution
{AC75EE22-B722-418F-A2A7-66020E29FE4C} = {E136AE06-927B-4D10-BB83-B607ED0B1FD4}
{FAC752B8-087D-44A9-88E8-3B759A72F936} = {E136AE06-927B-4D10-BB83-B607ED0B1FD4}
{E91C582B-DB9D-4489-A36B-2C850A426B63} = {E136AE06-927B-4D10-BB83-B607ED0B1FD4}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {323831A1-A95B-40AB-B9AD-36A0BC10C2CB}

View File

@ -0,0 +1,42 @@
using Microsoft.AspNetCore.Mvc;
using NewLife;
using NewLife.Caching;
namespace WebDemo.Controllers;
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ICacheProvider _cacheProvider;
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ICacheProvider cacheProvider, ILogger<WeatherForecastController> logger)
{
_cacheProvider = cacheProvider;
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
var cache = _cacheProvider.Cache;
var v = cache.Get<String>("test");
if (v.ToInt() <= 0) cache.Remove("test");
cache.SetExpire("test", TimeSpan.FromSeconds(60));
var v2 = _cacheProvider.Cache.Increment("test", 1);
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = (Int32)v2,
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}

View File

@ -0,0 +1,28 @@
using NewLife.Log;
XTrace.UseConsole();
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRedisCacheProvider();
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseAuthorization();
app.MapControllers();
app.Run();

View File

@ -0,0 +1,31 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:28634",
"sslPort": 0
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5072",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@ -0,0 +1,13 @@
namespace WebDemo
{
public class WeatherForecast
{
public DateOnly Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string? Summary { get; set; }
}
}

View File

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\NewLife.Redis.Extensions\NewLife.Redis.Extensions.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,6 @@
@WebDemo_HostAddress = http://localhost:5072
GET {{WebDemo_HostAddress}}/weatherforecast/
Accept: application/json
###

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@ -0,0 +1,12 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"RedisCache": "server=127.0.0.1;password=;db=1"
//"RedisQueue": "server=127.0.0.1:6379;password=123456;db=2"
}