.NET平台下Redis使用(四)【StackExchangeRedisHelper助手类】

敢为自己目标行动的人,整个世界都会为你让路


Program.cs主程序:

  class Program
    {
        static void Main(string[] args)
        {
            List userList =new List() {
                new User { UserName = "chengjun", PassWord = "qwerty" }, 
                new User { UserName = "chengjun1", PassWord = "qwerty" },
                new User { UserName = "chengjun2", PassWord = "qwerty" },
                new User { UserName = "chengjun3", PassWord = "qwerty" },
                new User { UserName = "chengjun5", PassWord = "qwerty" },
                new User { UserName = "chengjun6", PassWord = "qwerty" },
                new User { UserName = "chengjun7", PassWord = "qwerty" },
                new User { UserName = "chengjun8", PassWord = "qwerty" }
            };
            var stackExchangeHelper  = new StackExchangeHelper();
            stackExchangeHelper.Set("xxoo_cus", userList);
            var getUserListFromRedis = stackExchangeHelper.Get>("xxoo_cus");

            Console.WriteLine("List元素数量:"+getUserListFromRedis.Count);
            Console.ReadKey();
        }
    }


    [Serializable]
    public class User
    {
        public string UserName { get; set; }
        public string PassWord { get; set; }
    }

PubConstant.cs代码:

  public class PubConstant
    {
        public static string RedisIp
        {
            get
            {
                string _redisIp = ConfigurationManager.AppSettings["RedisIp"];
                return _redisIp;
            }
        }
        public static string RedisPort
        {
            get
            {
                string _redisPort = ConfigurationManager.AppSettings["RedisPort"];
                return _redisPort;
            }
        }
        public static string RedisPass
        {
            get
            {
                string _redisPass=ConfigurationManager.AppSettings["RedisPass"];
                return _redisPass;
            }
        }
    }

App.config代码:


<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    startup>
  <appSettings>
    
    <add key="RedisIp" value="127.0.0.1" />
    
    <add key="RedisPort" value="6379" />
    
    <add key="RedisPass" value="123456" />
  appSettings>
configuration>

StackExchangeConn.cs代码:

  public class StackExchangeConn
    {
        private static ConnectionMultiplexer _connection;
        private static readonly object SyncObject = new object();
        public static ConnectionMultiplexer GetFactionConn
        {
            get
            {
                if (_connection == null || !_connection.IsConnected)
                {
                    lock (SyncObject)
                    {
                        var configurationOptions = new ConfigurationOptions()
                        {
                            Password = PubConstant.RedisPass,
                            EndPoints = { { PubConstant.RedisIp, Convert.ToInt32(PubConstant.RedisPort) } }
                        };
                        //"192.168.100.37,password=123456";
                        _connection = ConnectionMultiplexer.Connect(configurationOptions);
                    }
                }
                return _connection;
            }
        }
    }

StackExchangeHelper.cs助手类

 public class StackExchangeHelper
    {
        private  static IDatabase cache;
        public StackExchangeHelper()
        {
            cache = StackExchangeConn.GetFactionConn.GetDatabase();
        }
        /// 
        /// 添加string类型数据到redis
        /// 
        /// 
        /// 
        /// 
        public bool StringSet(string key, string value)
        {
            return cache.StringSet(key, value);
        }
        /// 
        /// 从redis中获取string类型数据
        /// 
        /// 
        /// 
        public string StringGet(string key)
        {
            return cache.StringGet(key);
        }
        /// 
        /// 从redis中获取Int类型数据
        /// 
        /// 
        /// 
        public int IntGet(string key)
        {
            try
            {
                return Convert.ToInt32(StringGet(key));
            }
            catch (Exception)
            {
                return (-9999);
            }
        }
        /// 
        /// 根据key获取指定类型数据
        /// 
        /// 
        /// 
        /// 
        public T Get(string key)
        {
            return Deserialize(cache.StringGet(key));
        }
        /// 
        /// 根据key获取object类型数据
        /// 
        /// 
        /// 
        public object Get(string key)
        {
            return Deserialize<object>(cache.StringGet(key));
        }
        /// 
        /// 将数据添加到redis中
        /// 
        /// 
        /// 
        public void Set(string key, object value)
        {
            cache.StringSet(key, Serialize(value));
        }
        static byte[] Serialize(object o)
        {
            if (o == null)
            {
                return null;
            }

            BinaryFormatter binaryFormatter = new BinaryFormatter();
            using (MemoryStream memoryStream = new MemoryStream())
            {
                binaryFormatter.Serialize(memoryStream, o);
                byte[] objectDataAsStream = memoryStream.ToArray();
                return objectDataAsStream;
            }
        }

        static T Deserialize(byte[] stream)
        {
            if (stream == null)
            {
                return default(T);
            }

            BinaryFormatter binaryFormatter = new BinaryFormatter();
            using (MemoryStream memoryStream = new MemoryStream(stream))
            {
                T result = (T)binaryFormatter.Deserialize(memoryStream);
                return result;
            }
        }
    }

运行结果:

.NET平台下Redis使用(四)【StackExchangeRedisHelper助手类】_第1张图片


.NET平台下Redis使用(四)【StackExchangeRedisHelper助手类】_第2张图片

你可能感兴趣的:(Nosql之Redis数据库)