使用缓存提高系统性能

This commit is contained in:
hello 2024-07-13 09:59:34 +08:00
parent 46a626a97e
commit b0b9491dfe
4 changed files with 7 additions and 2 deletions

View File

@ -16,5 +16,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Aspire.Hosting.AppHost" Version="8.0.2" /> <PackageReference Include="Aspire.Hosting.AppHost" Version="8.0.2" />
<PackageReference Include="Aspire.Hosting.Redis" Version="8.0.2" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -3,13 +3,15 @@
var builder = DistributedApplication.CreateBuilder(args); var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("cache", port:6379);
var identityService = builder.AddProject<Projects.HelloShop_IdentityService>("identityservice"); var identityService = builder.AddProject<Projects.HelloShop_IdentityService>("identityservice");
var orderingService = builder.AddProject<Projects.HelloShop_OrderingService>("orderingservice").WithReference(identityService); var orderingService = builder.AddProject<Projects.HelloShop_OrderingService>("orderingservice").WithReference(identityService);
var productService = builder.AddProject<Projects.HelloShop_ProductService>("productservice").WithReference(identityService); var productService = builder.AddProject<Projects.HelloShop_ProductService>("productservice").WithReference(identityService);
var basketService = builder.AddProject<Projects.HelloShop_BasketService>("basketservice").WithReference(identityService); var basketService = builder.AddProject<Projects.HelloShop_BasketService>("basketservice").WithReference(identityService).WithReference(cache);
var apiservice = builder.AddProject<Projects.HelloShop_ApiService>("apiservice") var apiservice = builder.AddProject<Projects.HelloShop_ApiService>("apiservice")
.WithReference(identityService) .WithReference(identityService)

View File

@ -13,10 +13,12 @@
<Protobuf Include="Protos\greet.proto" GrpcServices="Server" /> <Protobuf Include="Protos\greet.proto" GrpcServices="Server" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Aspire.StackExchange.Redis.DistributedCaching" Version="8.0.2" />
<PackageReference Include="Calzolari.Grpc.AspNetCore.Validation" Version="8.1.0" /> <PackageReference Include="Calzolari.Grpc.AspNetCore.Validation" Version="8.1.0" />
<PackageReference Include="Grpc.AspNetCore" Version="2.63.0" /> <PackageReference Include="Grpc.AspNetCore" Version="2.63.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.7" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.7" />
<PackageReference Include="Microsoft.AspNetCore.Grpc.Swagger" Version="0.8.7" /> <PackageReference Include="Microsoft.AspNetCore.Grpc.Swagger" Version="0.8.7" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="8.0.7" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\HelloShop.ServiceDefaults\HelloShop.ServiceDefaults.csproj" /> <ProjectReference Include="..\HelloShop.ServiceDefaults\HelloShop.ServiceDefaults.csproj" />

View File

@ -24,7 +24,7 @@ builder.Services.AddAuthentication().AddJwtBearer(options =>
}); });
builder.Services.AddHttpContextAccessor(); builder.Services.AddHttpContextAccessor();
builder.Services.AddDistributedMemoryCache(); builder.AddRedisDistributedCache("cache");
builder.Services.AddSingleton<IBasketRepository, DistributedCacheBasketRepository>(); builder.Services.AddSingleton<IBasketRepository, DistributedCacheBasketRepository>();
builder.Services.AddGrpc(options => options.EnableMessageValidation()).AddJsonTranscoding(); builder.Services.AddGrpc(options => options.EnableMessageValidation()).AddJsonTranscoding();