欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ASP.NET?Core擴(kuò)展庫(kù)ServiceStack.Redis用法介紹

 更新時(shí)間:2022年02月17日 09:34:38   作者:Run2948  
這篇文章介紹了ASP.NET?Core擴(kuò)展庫(kù)ServiceStack.Redis的用法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

給大家安利一款 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)文章

最新評(píng)論