主要解决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 = ;
public static int RedisMaxWritePool = ; 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 > )
{
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 = ;
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 <= )
{
Remove(key); return;
} try
{
if (pool != null)
{
using (var r = pool.GetClient())
{
if (r != null)
{
r.SendTimeout = ;
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 = ;
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 = ;
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 = ;
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 = ; i < ; i++)
{
st += "a";
}
RedisHelper.Add("st",st,new TimeSpan(,,));
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;
}
}

Redis-Service.Stack的初级使用的更多相关文章

  1. 使用Service.Stack客户端编写redis pub sub的方法

    pub相对简单 client.PublishMessage("channel", "msg");   sub有2种方法 方法1 var subscription ...

  2. Redis Service

    https://raw.githubusercontent.com/MSOpenTech/redis/3.0/Windows%20Service%20Documentation.md

  3. AWS Data Lake Service Stack

  4. Redis介绍

    Redis的介绍 Remote Dictionary Server(Redis)是一个基于 key-value 键值对的持久化数据库存储系统.支持多种数据结构,包括 string (字符串).list ...

  5. Elastic Stack之Redis集群使用

    Elastic Stack之Redis集群使用 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客数据流走向:FileBeat ===>Redis  ===>lo ...

  6. ELK Stack (2) —— ELK + Redis收集Nginx日志

    ELK Stack (2) -- ELK + Redis收集Nginx日志 摘要 使用Elasticsearch.Logstash.Kibana与Redis(作为缓冲区)对Nginx日志进行收集 版本 ...

  7. CentOS 7安装/卸载Redis,配置service服务管理

    Redis简介 Redis功能简介 Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件. 相比于传统的关系型数据库,Redis的存储方式是key-va ...

  8. .Net使用Redis详解之ServiceStack.Redis(七)

    序言 本篇从.Net如何接入Reis开始,直至.Net对Redis的各种操作,为了方便学习与做为文档的查看,我做一遍注释展现,其中会对list的阻塞功能和事务的运用做二个案例,进行记录学习. Redi ...

  9. 记一次Redis和NetMQ的测试

    Redis是一个高速缓存K-V数据库,而NetMQ是ZeroMQ的C#实现版本,两者是完全不同的东西. 最近做游戏服务器的时候想到,如果选择一个组件来做服务器间通信的话,ZeroMQ绝对是一个不错的选 ...

随机推荐

  1. metasploit 学习笔记-VULNERABILITY SCANNING

    使用漏洞扫描器会在网络上产生大量流量,因此如果你不希望被发现踪迹时,不要使用漏洞扫描器。 The Basic Vulnerability Scan 漏洞扫描器的质量很大程度上取决于它自带的漏洞特征库。 ...

  2. Eavl整理

    一. 严格模式 eval方法只能在非严格模式中进行使用,在use strict中是不允许使用这个方法的. 二. 用法 eval函数会接收一个参数obj,如果obj不是一个字符串,那么eval会直接返回 ...

  3. linux 定制

    转载至http://luyafei.blog.51cto.com/1092421/1131532 测试环境: VMware Workstation 8.0 CentOS 6.7 x86_64 1.安装 ...

  4. NSNull空值

    1.前言 作为占据空间的一个空值,如用在数组或字典中占据一个没有任何值的空间. 1.1 NULL & nil 的区别: nil 是 OC 的,空对象,地址指向空的对象,指针地址指向的是 NUL ...

  5. async/await 处理异步

    async/ await来发送异步请求,从服务端获取数据,代码很简洁,同时async/await 已经被标准化. 先说一下async的用法,它作为一个关键字放到函数前面,用于表示函数是一个异步函数,因 ...

  6. Pyinstaller打包matplotlib.pyplot画图时提示无法找到Qt插件的解决办法

    This application failed to start because it could not find or load the Qt platform plugin "wind ...

  7. 计算机上配置 IP地址,子网掩码,默认网关

    The Internet Assigned Numbers Authority (IANA) has reserved the following three blocks of the IP add ...

  8. 常用JS事件对象

    事件源对象event.srcElement.tagNameevent.srcElement.type 捕获释放event.srcElement.setCapture(); event.srcEleme ...

  9. kuangbin专题十六 KMP&&扩展KMP HDU1358 Period

    For each prefix of a given string S with N characters (each character has an ASCII code between 97 a ...

  10. BaseNavigationController自定义导航栏

    #import <UIKit/UIKit.h> @interface RCDNavigationViewController : UINavigationController<UIG ...