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;
            }
        }
  • 相关阅读:
    macbook 无声音解决方案
    webapck dev server代理请求 json截断问题
    百度卫星地图开启
    服务器 nginx配置 防止其他域名绑定自己的服务器
    记一次nginx php配置的心路历程
    遇到npm报错read ECONNRESET怎么办
    运行svn tortoiseSvn cleanup 命令失败的解决办法
    svn add 命令 递归目录下所有文件
    m4出现Please port gnulib freadahead.c to your platform! Look at the definition of fflush, fread, ungetc on your system, then report this to bug-gnulib."
    Ubuntu下安装GCC,mpc、mpfr、gmp
  • 原文地址:https://www.cnblogs.com/mengdongsky/p/7463515.html
Copyright © 2011-2022 走看看