zoukankan      html  css  js  c++  java
  • 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;
            }
        }
  • 相关阅读:
    链表查找问题总结
    部分有序中查找给定值-【百度面试题】循环有序数组的查找问题
    为何要将整型变量强制转化为指针
    洗牌算法汇总
    如果有三个Bool型变量,请写出一程序得知其中有2个以上变量的值是true
    利用宏来求结构体成员偏移值
    水塘抽样问题
    Javascript 装载和执行
    git ssh认证
    git 配置文件
  • 原文地址:https://www.cnblogs.com/mengdongsky/p/7463515.html
Copyright © 2011-2022 走看看