ASP.NET?Core擴展庫ServiceStack.Redis用法介紹
給大家安利一款 ServiceStack.Redis 的 ASP.NET Core 擴展庫,它是基于 ServiceStack.Redis.Core 開發(fā)的。 簡單易用,開源免費,使用ASP.NET Core自身提供的DI容器來實現(xiàn)針對服務(wù)的注冊和消費。直接在程序啟動時注冊到服務(wù)中即可完成全部配置,對于小白用戶也可快速上手Redis緩存和Redis分布式緩存。
Install Package
https://www.nuget.org/packages/ServiceStack.Redis.Extension.AspNetCore
Configure
Startup.cs
Single Server
public void ConfigureServices(IServiceCollection services)
{
services.AddDistributedServiceStackRedisCache(options =>
{
// default single server: 127.0.0.1:6379
// services.AddServiceStackRedisCache();
// customize single server
services.AddServiceStackRedisCache(options =>{
options.SingleServer = "123456@127.0.0.1:6379";
});
}
services.AddControllers();
}Read and write separation
public void ConfigureServices(IServiceCollection services)
{
services.AddServiceStackRedisCache(options =>
{
options.ReadWriteServers = new[]
{
"192.168.1.1:6379", "123456@192.168.1.2:6379", "123456@192.168.1.3:6379", "123456@192.168.1.4:6379"
};
options.ReadOnlyServers = new[]
{
"192.168.1.1:6379", "123456@192.168.1.3:6379"
};
});
services.AddControllers();
}Load from configuration
public void ConfigureServices(IServiceCollection services)
{
services.AddServiceStackRedisCache(Configuration.GetSection("ServiceStackRedisOptions"));
services.AddControllers();
}appsettings.Development.json
{
"ServiceStackRedisOptions": {
"SingleServer": "1234546@127.0.0.1:6379"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}appsetting.json
{
"ServiceStackRedisOptions": {
"ReadWriteServers": ["192.168.1.1:6379", "123456@192.168.1.2:6379", "123456@192.168.1.3:6379", "123456@192.168.1.4:6379"],
"ReadOnlyServers": ["192.168.1.1:6379", "123456@192.168.1.3:6379"]
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}ServiceStack.Redis Options
public class ServiceStackRedisOptions
{
/// <summary>
/// 單機的地址,例如:127.0.0.1:6379(默認值)。如果你只用到一個Redis服務(wù)端,那么配置此項即可。
/// </summary>
public string SingleServer { get; set; } = "127.0.0.1:6379";
/// <summary>
/// 讀寫的地址,例如:{ "192.168.1.1:6379","123456@192.168.1.2:6379","123456@192.168.1.3:6379","123456@192.168.1.4:6379" }
/// </summary>
public string[] ReadWriteServers { get; set; }
/// <summary>
/// 只讀地址,例如:{ "192.168.1.1:6379","123456@192.168.1.3:6379" }
/// </summary>
public string[] ReadOnlyServers { get; set; }
/// <summary>
/// MaxWritePoolSize寫的頻率比讀低。默認值 8
/// </summary>
public int MaxWritePoolSize { get; set; } = 8;
/// <summary>
/// MaxReadPoolSize讀的頻繁比較多。默認值 12,Redis官方聲明最大連接數(shù)為1W,但是連接數(shù)要控制。
/// </summary>
public int MaxReadPoolSize { get; set; } = 12;
/// <summary>
/// 連接最大的空閑時間。默認值 60,Redis官方默認是240
/// </summary>
public int IdleTimeOutSecs { get; set; } = 60;
/// <summary>
/// 連接超時時間,毫秒。默認值 6000
/// </summary>
public int ConnectTimeout { get; set; } = 6000;
/// <summary>
/// 數(shù)據(jù)發(fā)送超時時間,毫秒。默認值 6000
/// </summary>
public int SendTimeout { get; set; } = 6000;
/// <summary>
/// 數(shù)據(jù)接收超時時間,毫秒。默認值 6000
/// </summary>
public int ReceiveTimeout { get; set; } = 6000;
/// <summary>
/// 連接池取鏈接的超時時間,毫秒。默認值 6000
/// </summary>
public int PoolTimeout { get; set; } = 6000;
/// <summary>
/// 默認的數(shù)據(jù)庫。默認值 0,Redis官方默認也是0
/// </summary>
public long DefaultDb { get; set; } = 0;
}Usage
WeatherForecastController.cs
[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 ILogger<WeatherForecastController> _logger;
private readonly IServiceStackRedisCache _redisCache;
public WeatherForecastController(ILogger<WeatherForecastController> logger, IServiceStackRedisCache redisCache)
{
_logger = logger;
this._redisCache = redisCache;
}
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var array = _redisCache.Get<WeatherForecast[]>("WeatherForecast");
if (array == null)
{
var rng = new Random();
array = Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
}).ToArray();
// Cache for 30 minutes
_redisCache.Set("WeatherForecast", array, 60 * 1 * 30);
}
return array;
}
}到此這篇關(guān)于ASP.NET Core擴展庫ServiceStack.Redis用法的文章就介紹到這了。希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在ASP.NET中用MSDNURLRewriting實現(xiàn)Url Rewriting
在ASP.NET中用MSDNURLRewriting實現(xiàn)Url Rewriting...2007-03-03
詳解.NET中string與StringBuilder在字符串拼接功能上的比較
string與StringBuilder的在字符串拼接時執(zhí)行效率上有差異,這篇文章主要介紹了詳解.NET中string與StringBuilder在字符串拼接功能上的比較,感興趣的小伙伴們可以參考一下2018-11-11
.NET微信小程序用戶數(shù)據(jù)的簽名驗證和解密代碼
這篇文章主要介紹了.NET微信小程序用戶數(shù)據(jù)的簽名驗證和解密代碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12
ASP.NET2.0服務(wù)器控件之自定義狀態(tài)管理
ASP.NET2.0服務(wù)器控件之自定義狀態(tài)管理...2006-09-09
asp.net下UTF-7轉(zhuǎn)GB2312編碼的代碼(中文)
UTF-7轉(zhuǎn)換GB2312編碼的方法2010-07-07
ASP.NET Core使用JWT認證授權(quán)的方法
這篇文章主要介紹了ASP.NET Core使用JWT認證授權(quán)的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2020-11-11

