ServiceStack.Redis 使用链接池方法

时间:2022-01-17 20:43:59

PooledRedisClientManager

1、RedisManage.cs

public static class RedisManager
{
  private static PooledRedisClientManager _prcm;

  static RedisManager()
  {
      CreateManager();
  }

    /// <summary>
    /// 创建链接池管理对象
    /// </summary>
  private static void CreateManager()
  {   
      _prcm = new PooledRedisClientManager(writeServerList, readServerList,
                  new RedisClientManagerConfig
                  {
                      /*Config*/
                  });
  }
  public static IRedisClient GetClient()
  {
      if (_prcm == null)
      {
          CreateManager();
      }
      return _prcm.GetClient();
  }

  public static IRedisClient GetReadOnlyClient()
  {
      if (_prcm == null)
      {
          CreateManager();
      }
      return _prcm.GetReadOnlyClient();
  }
}

2、调用

using(var client = RedisManage.GetClient())
{
  /*content*/
}

using(var client = RedisManage.GetReadOnlyClient())
{
  /*content*/
}