ASP.NET?Core擴(kuò)展庫(kù)ServiceStack.Redis用法介紹
給大家安利一款 ServiceStack.Redis 的 ASP.NET Core 擴(kuò)展庫(kù),它是基于 ServiceStack.Redis.Core
開(kāi)發(fā)的。 簡(jiǎn)單易用,開(kāi)源免費(fèi),使用ASP.NET Core自身提供的DI容器來(lái)實(shí)現(xiàn)針對(duì)服務(wù)的注冊(cè)和消費(fèi)。直接在程序啟動(dòng)時(shí)注冊(cè)到服務(wù)中即可完成全部配置,對(duì)于小白用戶也可快速上手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> /// 單機(jī)的地址,例如:127.0.0.1:6379(默認(rèn)值)。如果你只用到一個(gè)Redis服務(wù)端,那么配置此項(xiàng)即可。 /// </summary> public string SingleServer { get; set; } = "127.0.0.1:6379"; /// <summary> /// 讀寫(xiě)的地址,例如:{ "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寫(xiě)的頻率比讀低。默認(rèn)值 8 /// </summary> public int MaxWritePoolSize { get; set; } = 8; /// <summary> /// MaxReadPoolSize讀的頻繁比較多。默認(rèn)值 12,Redis官方聲明最大連接數(shù)為1W,但是連接數(shù)要控制。 /// </summary> public int MaxReadPoolSize { get; set; } = 12; /// <summary> /// 連接最大的空閑時(shí)間。默認(rèn)值 60,Redis官方默認(rèn)是240 /// </summary> public int IdleTimeOutSecs { get; set; } = 60; /// <summary> /// 連接超時(shí)時(shí)間,毫秒。默認(rèn)值 6000 /// </summary> public int ConnectTimeout { get; set; } = 6000; /// <summary> /// 數(shù)據(jù)發(fā)送超時(shí)時(shí)間,毫秒。默認(rèn)值 6000 /// </summary> public int SendTimeout { get; set; } = 6000; /// <summary> /// 數(shù)據(jù)接收超時(shí)時(shí)間,毫秒。默認(rèn)值 6000 /// </summary> public int ReceiveTimeout { get; set; } = 6000; /// <summary> /// 連接池取鏈接的超時(shí)時(shí)間,毫秒。默認(rèn)值 6000 /// </summary> public int PoolTimeout { get; set; } = 6000; /// <summary> /// 默認(rèn)的數(shù)據(jù)庫(kù)。默認(rèn)值 0,Redis官方默認(rèn)也是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擴(kuò)展庫(kù)ServiceStack.Redis用法的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在ASP.NET中用MSDNURLRewriting實(shí)現(xiàn)Url Rewriting
在ASP.NET中用MSDNURLRewriting實(shí)現(xiàn)Url Rewriting...2007-03-03詳解.NET中string與StringBuilder在字符串拼接功能上的比較
string與StringBuilder的在字符串拼接時(shí)執(zhí)行效率上有差異,這篇文章主要介紹了詳解.NET中string與StringBuilder在字符串拼接功能上的比較,感興趣的小伙伴們可以參考一下2018-11-11ASP.NET站點(diǎn)導(dǎo)航應(yīng)用詳解
這篇文章主要內(nèi)容是ASP.NET站點(diǎn)導(dǎo)航,主要包括站點(diǎn)導(dǎo)航以及動(dòng)態(tài)修改內(nèi)存中的站點(diǎn)地圖,感興趣的小伙伴們可以參考一下2015-09-09.NET微信小程序用戶數(shù)據(jù)的簽名驗(yàn)證和解密代碼
這篇文章主要介紹了.NET微信小程序用戶數(shù)據(jù)的簽名驗(yàn)證和解密代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12ASP.NET2.0服務(wù)器控件之自定義狀態(tài)管理
ASP.NET2.0服務(wù)器控件之自定義狀態(tài)管理...2006-09-09asp.net下UTF-7轉(zhuǎn)GB2312編碼的代碼(中文)
UTF-7轉(zhuǎn)換GB2312編碼的方法2010-07-07近幾天對(duì)DataSet的新認(rèn)識(shí)
近幾天對(duì)DataSet的新認(rèn)識(shí)...2007-04-04ASP.NET Core使用JWT認(rèn)證授權(quán)的方法
這篇文章主要介紹了ASP.NET Core使用JWT認(rèn)證授權(quán)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11