Redis-Service.Stack的初级使用

主要解决Redis服务器带有密码的情况下初始化。

 

创建RedisHelper类,直接贴代码:

 
using ServiceStack.Redis;
using System;
class RedisHelper { private static readonly PooledRedisClientManager pool = null; private static readonly string[] redisHosts = null; public static int RedisMaxReadPool = 1000; public static int RedisMaxWritePool = 1000; static RedisHelper() { //不带密码的ip地址:ip:port //带有密码的ip地址:password@ip:port var redisHostStr = "123456@192.168.1.10:6379"; if (!string.IsNullOrEmpty(redisHostStr)) { redisHosts = redisHostStr.Split(','); if (redisHosts.Length > 0) { pool = new PooledRedisClientManager(redisHosts, redisHosts, new RedisClientManagerConfig() { MaxWritePoolSize = RedisMaxWritePool, MaxReadPoolSize = RedisMaxReadPool, AutoStart = true }); } } } public static void Add<T>(string key, T value, DateTime expiry) { if (value == null) { return; } if (expiry <= DateTime.Now) { Remove(key); return; } try { if (pool != null) { using (var r = pool.GetClient()) { if (r != null) { r.SendTimeout = 1000; r.Set(key, value, expiry - DateTime.Now); } } } } catch (Exception ex) { string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "存储", key); Log(msg); } } public static void Add<T>(string key, T value, TimeSpan slidingExpiration) { if (value == null) { return; } if (slidingExpiration.TotalSeconds <= 0) { Remove(key); return; } try { if (pool != null) { using (var r = pool.GetClient()) { if (r != null) { r.SendTimeout = 1000; r.Set(key, value, slidingExpiration); r.Save(); } } } } catch (Exception ex) { string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "存储", key); Log(msg); } } public static T Get<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); Log(msg); } return obj; } public static void Remove(string key) { try { if (pool != null) { using (var r = pool.GetClient()) { if (r != null) { r.SendTimeout = 1000; r.Remove(key); } } } } catch (Exception ex) { string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "删除", key); Log(msg); } } public static bool Exists(string key) { try { if (pool != null) { using (var r = pool.GetClient()) { if (r != null) { r.SendTimeout = 1000; return r.ContainsKey(key); } } } } catch (Exception ex) { string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "是否存在", key); Log(msg); } return false; } static void Log(string s) { Console.WriteLine("RedisHelper:"+s); } }

使用方法:

    class Program
    {
        static void Main(string[] args)
        {
            string name = RedisHelper.Get<string>("name:1");
            Console.WriteLine(name);

            //add user object
            //User user = new User()
            //{
            //    UserToken = "庄周",
            //    Password = "123456",
            //    Age = 100
            //};
            //RedisHelper.Add("user:1", user, DateTime.Now + new TimeSpan(1, 0, 0));

            User user = RedisHelper.Get<User>("user:1");

            Console.WriteLine(user.ToString());
            string st="";
            for (int i = 0; i < 1024; i++)
            {
                st += "a";
            }
            RedisHelper.Add("st",st,new TimeSpan(1,0,0));
            while (true)
            {
                Console.ReadLine();
            }
        }


    }

    class User
    {
        public string UserToken { get; set; }
        public int Age { get; set; }
        public string Password { get; set; }

        public override string ToString()
        {
            string s = "UserToken:" + UserToken;
            s += " Password:" + Password;
            s += " Age:" + Age;
            return s;
        }
    }

 

posted @ 2017-09-01 15:37  孟栋sky  阅读(387)  评论(0编辑  收藏  举报