GetRequiredService>

發布 cache 22


還是直接上代碼吧
    

public class AppOptions
    {
    
public string RedisConnectionString { get; set; } = "localhost:6379";
    }

    

    

services.Configure<AppOptions>(Configuration);
    services.AddSingleton
<IConnectionMultiplexer,ConnectionMultiplexer>(sp =>
    {
    
var settings = sp.GetRequiredService<IOptions<AppOptions>>().Value;
    
var configuration = ConfigurationOptions.Parse(settings.RedisConnectionString, true);
    
return ConnectionMultiplexer.Connect(configuration);
    });

    

    

private readonly IDatabase _database;
    
public AccountController(IConnectionMultiplexer connectionMultiplexer)
    {
     _database
= connectionMultiplexer.GetDatabase();
    }
    
public IActionResult Index()
    {
     _database.StringSet(
"key", "value");
    
return View();
    }

    
 
美国扑克50手返水