一、下载ServiceStack.Redis

  VS2015版本

二、将解压后的文件中下面四个dll引用到项目中

在C#中使用Redis

三、用C#对redis来进行简单的读取和写入操作

在C#中使用Redis

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.Common.Extensions;
using ServiceStack.Redis;
using ServiceStack.Logging;
using System.Threading.Tasks;

namespace RedisDemo.Redis
{
    public class RedisConfigInfo
    {
        //唯一实例
        private static RedisConfigInfo uniqueInstance;

        //public static int RedisMaxReadPool = int.Parse(ConfigurationManager.AppSettings["redis_max_read_pool"]);

        //public static int RedisMaxWritePool = int.Parse(ConfigurationManager.AppSettings["redis_max_write_pool"]);
        //定义一个标识确保线程同步

        private static readonly object locker = new object();

        private readonly string[] redisHosts = null;
        //链接池管理对象
        private PooledRedisClientManager _pool;
        //私有构造方法
        private RedisConfigInfo()
        {
            //创建连接池管理对象
            var redisHostStr = "127.0.0.1:6379";
            redisHosts = redisHostStr.Split(',');
            CreateRedisPoolManager(redisHostStr.Split(','), redisHostStr.Split(','));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="redisWriteHost"></param>
        /// <param name="redisReadHost"></param>
        private void CreateRedisPoolManager(string[] redisWriteHost, string[] redisReadHost)
        {
            _pool = new PooledRedisClientManager(redisWriteHost, redisReadHost, new RedisClientManagerConfig()
            {
                MaxWritePoolSize = redisHosts.Length * 5,
                MaxReadPoolSize = redisHosts.Length * 5,
                AutoStart = true
            });

        }

        //唯一全局访问点
        public static RedisConfigInfo GetRedisConfigInfo()
        {
            //双重锁定
            if (uniqueInstance == null)
            {
                lock (locker)
                {
                    if (uniqueInstance == null)
                    {
                        uniqueInstance = new RedisConfigInfo();
                    }
                }
            }
            return uniqueInstance;
        }

        public T _GetKey<T>(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                return default(T);
            }
            T obj = default(T);
            try
            {
                if (_pool != null)
                {
                    using (var r = _pool.GetClient())
                    {
                        if (r != null)
                        {
                            r.SendTimeout = 1000;
                            obj = r.Get<T>(key);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "获取", key);
            }
            return obj;
        }

        public bool _AddKey<T>(string key, T value)
        {
            if (value == null)
            {
                return false;
            }

            try
            {
                if (_pool != null)
                {
                    using (var r = _pool.GetClient())
                    {
                        if (r != null)
                        {
                            r.SendTimeout = 1000;
                            r.Set(key, value);
                            return true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "存储", key);
            }
            return false;
        }


    }
}

RedisConfigInfo.cs