透過 Microsoft.Extensions.Caching.StackExchangeRedis 實現分散式快取機制。
Implement distributed cache mechanism with Microsoft.Extensions.Caching.StackExchangeRedis.
配置 appsettings.json
-
Configure appsettings.json
"Cache": { "Redis": { "InstanceName": "SampleInstance", "Configuration": "localhost:6379" } }
- InstanceName:The Redis instance name
- Configuration:The configuration used to connect to Redis
新增 Redis 快取實例至 DI 容器中。
-
Add redis cache instance to DI container
builder.Services.AddStackExchangeRedisCache(options => { options.InstanceName = builder.Configuration.GetValue<string>("Cache:Redis:InstanceName"); options.Configuration = builder.Configuration.GetValue<string>("Cache:Redis:Configuration"); }); builder.Services.AddSingleton<ICache, RedisCache>();
注入
ICache
來使用分散式快取。
- Inject
ICache
to use distributed cache.