using CSRedis;
using System.Collections.Concurrent;
using System.Linq;

namespace Fuel.Infrastructure
{
    /// <summary>
    /// redis 多个database 客户端
    /// </summary>
    public class CoreRedisHelper
    {
        public static ConcurrentDictionary<string, CSRedisClient> redisHelpers
        {
            get;
            private set;
        }


        public static bool AddClient(string clientName, CSRedisClient redisHelper)
        {
            if (redisHelpers == null)
            {
                redisHelpers = new ConcurrentDictionary<string, CSRedisClient>();
            }
            if (string.IsNullOrWhiteSpace(clientName))
            {
                return false;
            }
            return redisHelpers.TryAdd(clientName, redisHelper);
        }

        /// <summary>
        /// 获取redis客户端名称
        /// </summary>
        /// <param name="clientName"></param>
        /// <returns></returns>
        public static CSRedisClient GetRedisHelper(string clientName)
        {
            if (redisHelpers.Any())
            {
                if (redisHelpers.Keys.Any(p => p == clientName))
                {
                    return redisHelpers[clientName];
                }
            }
            return null;
        }
    }
}
