使用缓存提高系统性能

视频免费1:41:00大约 2 年前

私有专用缓存,分布式共享缓存,使用 Redis 分布式缓存,使用微软 Garnet 分布式缓存,使用 Window Service 运行 Garnet 缓存,购物车服务使用分布式缓存,高可用性分布式缓存集群,缓存最佳实践,缓存技术点,分布式缓存监控工具,使用 Aspire 分布式缓存组件。

正在加载视频…

使用缓存提高系统性能

缓存是一种常见的技术,目标是提高系统的性能和伸缩性。 为缓存数据,它会暂时会经常访问的数据复制到位置靠近应用程序的快速存储。 如果这种快速数据存储比原始源更靠近应用程序,则缓存可以通过更快速提供数据,大幅改善客户端应用程序的响应时间。

cache-aside

缓存类型

私有专用缓存

专用缓存是指缓存服务运行在应用程序的同一台服务器上,它是应用程序的一部分,通常是一个库或模块。专用缓存通常是一个内存缓存,如 MemoryCache 或 ConcurrentDictionary,它是一个轻量级的缓存,适用于小型应用程序。专用缓存的优点是简单易用,缺点是无法跨服务器共享数据,不适用于大型应用程序,使用 IMemoryCache 接口可以在 .NET 中使用专用缓存。

privatecache

 
services.AddMemoryCache();
 
public class MyService(IMemoryCache cache) : IService
{
    public async Task Test()
    {
        cache.Set("key", "value");
        var value = cache.Get("key");
    }
}
 
csharp

分布式共享缓存

分布式缓存是指缓存服务运行在多台服务器上,它是一个独立的服务,可以跨服务器共享数据。分布式缓存通常是一个内存缓存,如 Redis、Memcached、Garnet 等,它是一个高性能、低延迟的缓存,适用于大型应用程序。分布式缓存的优点是高性能、低延迟,支持大规模数据存储和访问,缺点是复杂度高,需要额外的服务器资源,使用 IDistributedCache 接口可以在 .NET 中使用分布式缓存。

cache-shared

 
services.AddMemoryDistributedCache();
 
public class MyService(IDistributedCache cache) : IService
{
    public async Task Test()
    {
        await cache.SetStringAsync("key", "value");
        var value = await cache.GetStringAsync("key");
    }
}
csharp

使用 Redis 分布式缓存

Redis 是一个高性能、开源的分布式缓存系统,它是一个内存数据库,支持多种数据结构,如字符串、列表、哈希表、集合等,Redis 的设计目标是提供一个高性能、低延迟的分布式缓存系统,以支持大规模的数据存储和访问,同时提供高可用性和可扩展性。Redis 采用 RESP 协议,使用 C 语言编写,支持多种语言,如 C、C++、C#、Java、Python、Node.js 等,可以在 .NET 中使用 StackExchange.Redis 开源库对缓存进行操作。

Windows 下 Redis 不再维护,可以使用 WSL2 安装 Redis,或者使用 Docker 安装 Redis。也可使用 Memurai 替代 Redis,Memurai 是一个 Windows 下的 Redis 服务,支持 Redis 的大部分功能,可以在 Windows 下运行 Redis 服务。

容器化 Redis 服务

docker run --name my-redis -d -p 6379:6379 -e REDIS_PASSWORD=guest redis
shell

测试客户端工具

使用 redis-cli 连接到 Linux 缓存服务器,执行以下命令进行测试。

redis-cli -h localhost -p 6379 -a Password
auth Password
set key value
get key
shell

使用 StackExchange.Redis 客户端

donet add package StackExchange.Redis
shell
 using StackExchange.Redis;
 
 var redis = ConnectionMultiplexer.Connect("localhost:6379,password=guest");
 var db = redis.GetDatabase();
 
 db.StringSet("key", "value");
 
 var value = db.StringGet("key");
 
csharp

使用 IDistributedCache 接口

dotnet add package Microsoft.Extensions.Caching.StackExchangeRedis
shell
services.AddStackExchangeRedisCache(options =>
{
    options.Configuration = "localhost:6379,password=guest";
    options.InstanceName = "SampleInstance";
});
csharp
using Microsoft.Extensions.Caching.Distributed;
 
public class MyService(IDistributedCache cache) : IService
{
    public async Task Test()
    {
        await cache.SetStringAsync("key", "value");
        var value = await cache.GetStringAsync("key");
    }
}
csharp

推荐使用 IDistributedCache 接口,它是一个抽象接口,可以方便的切换不同的缓存实现,如 Redis、Memcached、Garnet 等,同时支持 ASP.NET Core 的缓存中间件,可以方便的在 ASP.NET Core 中使用缓存。

使用微软 Garnet 分布式缓存

Garnet 是微软开源的分布式缓存系统,它是一个高性能、低延迟的分布式缓存系统,支持多种数据结构,如字符串、列表、哈希表、集合等,Garnet 的设计目标是提供一个高性能、低延迟的分布式缓存系统,以支持大规模的数据存储和访问,同时提供高可用性和可扩展性。

Garnet 采用流行的 RESP 协议,使用 C# 编写,支持 Redis 的大部分命令,同时提供了更多的功能,如集群搭建、数据持久化、数据复制、数据分片等,在 C# 中的可以使用 StackExchange.Redis 开源库对缓存进行操作。

https://microsoft.github.io/garnet/docs

在 .NET 程序中自托管

dotnet add package Microsoft.Garnet
shell
using Garnet;
 
try
{
    using var server = new GarnetServer(args);
    server.Start();
    Thread.Sleep(Timeout.Infinite);
}
catch (Exception ex)
{
    Console.WriteLine($"Unable to initialize server due to exception: {ex.Message}");
}
csharp

在 Docker 容器中运行

docker run --name garnet -d -p 6379:6379 --ulimit memlock=-1 ghcr.io/microsoft/garnet --auth Password --password guest
shell

使用 Window Service 运行

dotnet add package Microsoft.Garnet
dotnet add package Microsoft.Extensions.Hosting.WindowsServices
shell
 
public class GarnetService(ILogger<GarnetService> logger) : BackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        if (!stoppingToken.IsCancellationRequested)
        {
            string[] commandLineArgs = Environment.GetCommandLineArgs();
 
            using var server = new GarnetServer(commandLineArgs);
 
            logger.LogInformation("Starting Garnet server...");
 
            try
            {
                server.Start();
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "An error occurred while starting the Garnet server.");
            }
 
 
            logger.LogInformation("Garnet server started.");
 
            await Task.Delay(Timeout.Infinite, stoppingToken);
        }
    }
}
 
csharp
var builder = Host.CreateApplicationBuilder(args);
 
builder.Services.AddWindowsService(options =>
{
    options.ServiceName = "Garnet Service";
});
 
builder.Services.AddHostedService<GarnetService>();
 
var host = builder.Build();
 
host.Run();
csharp
sc.exe create GarnetService binpath= "C:\Users\GarnetWindowsService.exe" start= auto
 
sc.exe start GarnetService
 
sc.exe stop GarnetService
 
sc.exe delete GarnetService
shell

购物车使用分布式缓存

dotnet add package Microsoft.Extensions.Caching.StackExchangeRedis
shell
builder.Services.AddStackExchangeRedisCache(options =>
{
     options.Configuration = builder.Configuration.GetConnectionString("MyRedisConStr");
});
 
csharp

高可用性分布式缓存集群

使用多个缓存服务器搭建高可用性集群,可以提高系统的可用性和可靠性。

https://microsoft.github.io/garnet/docs/cluster/overview

https://redis.io/topics/cluster-tutorial

 
services.AddStackExchangeRedisCache(options =>
{
    options.Configuration = "localhost:6379,localhost:6380,localhost:6381";
    options.InstanceName = "SampleInstance";
});
 
csharp
 
services.AddStackExchangeRedisCache(options =>
{
    options.InstanceName = "SampleInstance";
    options.ConfigurationOptions = new ConfigurationOptions
    {
        Password = "Password",
        AbortOnConnectFail = false,
        EndPoints =
        {
            { "localhost", 6379 },
            { "localhost", 6380 },
            { "localhost", 6381 }
        }
    };
});
 
csharp

缓存最佳实践

https://learn.microsoft.com/zh-cn/azure/architecture/best-practices/caching

https://learn.microsoft.com/zh-cn/azure/architecture/patterns/cache-aside

缓存技术点

缓存雪崩、缓存穿透、缓存击穿、缓存预热、缓存更新、缓存降级、缓存命中率、滑动过期、绝对过期、永不过期、缓存依赖。

https://developer.aliyun.com/article/841392

IDistributedCache 过期时间分为:绝对过期时间和滑动过期时间。

分布式缓存监控工具

可用 RedisInsight 工具监控 Redis 分布式缓存系统。

https://redis.io/docs/latest/operate/redisinsight/install

docker run -d --name redisinsight -p 5540:5540 redis/redisinsight:latest
shell

使用 Aspire 分布式组件库

Aspire Host 项目配置

dotnet add package Aspire.Hosting.Redis
shell
var redis = builder.AddRedis("cache");
builder.AddProject<Projects.ExampleProject>().WithReference(redis)
csharp

微服务项目配置

dotnet add package Aspire.StackExchange.Redis.DistributedCaching
shell
builder.AddRedisDistributedCache("cache");
csharp

接着看

视频零度新版框架搭建系列视频教程订阅

构建可靠的事务性发件箱模式

事务性发件箱模式确保将事件保存在数据存储中,然后再最终推送到消息代理。 如果业务对象和相应的事件保存在同一数据库事务中,则保证不会丢失任何数据。 所有内容都将被提交,或在出错时所有内容回滚。 为了最终发布事件,其他服务或工作进程会在发件箱表中查询未处理项,发布这些事件,并将其标记为已处理。 此模式可确保创建或修改业务对象后不会丢失事件。

1:01:15超过 1 年前
视频零度新版框架搭建系列视频教程订阅

自动迁移和数据库初始化

EF Core中数据库迁移与初始化的核心概念与实用技巧。内容包括生成与应用迁移脚本、EnsureCreatedAsync与MigrateAsync的使用及冲突解决、通过内部服务实现优雅的自动迁移、EF Core 9.0推荐的异步种子数据初始化方法,以及利用IHostedService或BackgroundService实现耗时操作的异步数据库初始化。通过本课程,开发者将掌握高效管理数据库生命周期的最佳实践,提升项目开发效率与质量。

57:00超过 1 年前
视频零度新版框架搭建系列视频教程订阅

重构代码并抽取基础构建库

本次重构优化了事件总线架构,提升了系统的扩展性和灵活性。新增支持包和多个项目,增强了事件处理能力。通过实现基于 Dapr 和 RabbitMQ 的事件总线,改进了消息传递效率。加入了分布式锁功能,确保并发场景下的数据一致性。

38:00超过 1 年前
视频零度新版框架搭建系列视频教程订阅

在 Aspire 中集成 PostgreSQL 数据库

在 Host 项目中添加 PostgreSQL 数据库,在最终的应用中引用 PostgreSQL 数据库,添加 PostgreSQL pgAdmin 资源,添加 PostgreSQL pgWeb 资源。

35:00超过 1 年前

搜索

搜索分类、视频或标签