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 = ;
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的初级使用的更多相关文章
- 使用Service.Stack客户端编写redis pub sub的方法
pub相对简单 client.PublishMessage("channel", "msg"); sub有2种方法 方法1 var subscription ...
- Redis Service
https://raw.githubusercontent.com/MSOpenTech/redis/3.0/Windows%20Service%20Documentation.md
- AWS Data Lake Service Stack
- Redis介绍
Redis的介绍 Remote Dictionary Server(Redis)是一个基于 key-value 键值对的持久化数据库存储系统.支持多种数据结构,包括 string (字符串).list ...
- Elastic Stack之Redis集群使用
Elastic Stack之Redis集群使用 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客数据流走向:FileBeat ===>Redis ===>lo ...
- ELK Stack (2) —— ELK + Redis收集Nginx日志
ELK Stack (2) -- ELK + Redis收集Nginx日志 摘要 使用Elasticsearch.Logstash.Kibana与Redis(作为缓冲区)对Nginx日志进行收集 版本 ...
- CentOS 7安装/卸载Redis,配置service服务管理
Redis简介 Redis功能简介 Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件. 相比于传统的关系型数据库,Redis的存储方式是key-va ...
- .Net使用Redis详解之ServiceStack.Redis(七)
序言 本篇从.Net如何接入Reis开始,直至.Net对Redis的各种操作,为了方便学习与做为文档的查看,我做一遍注释展现,其中会对list的阻塞功能和事务的运用做二个案例,进行记录学习. Redi ...
- 记一次Redis和NetMQ的测试
Redis是一个高速缓存K-V数据库,而NetMQ是ZeroMQ的C#实现版本,两者是完全不同的东西. 最近做游戏服务器的时候想到,如果选择一个组件来做服务器间通信的话,ZeroMQ绝对是一个不错的选 ...
随机推荐
- 10.9 guz模拟题题解
感谢@guz 顾z的题题解 考试共三道题,其中 第一题help共10个测试点,时间限制为 1000ms,空间限制为 256MB. 第二题escape共20个测试点,时间限制为1000ms2000ms, ...
- 字串变换 bfs + 字符串
题目描述 已知有两个字串A,BA,BA,B及一组字串变换的规则(至多666个规则): A1A_1A1 ->B1 B_1B1 A2A_2A2 -> B2B_2B2 规则的含义为:在 ...
- kuangbin专题十二 POJ3186 Treats for the Cows (区间dp)
Treats for the Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7949 Accepted: 42 ...
- Flashcache的 KEEP属性自动失效
如果希望一个数据对象长期地缓存在flashcache中,则可以手动地将该数据对象的CELL_FLASH_CACHE属性设置为"keep". 其实需要说明的是,但不是数据对象的CEL ...
- 《C程序设计II》简易计算器,杨辉,数字杯子图形
<C程序设计II>简易计算器,杨辉,数字杯子图形.<C程序设计II>简易计算器,杨辉,数字杯子图形.<C程序设计II>简易计算器,杨辉,数字杯子图形. <C程 ...
- P3348 [ZJOI2016]大森林(LCT)
Luogu3348 BZOJ4573 LOJ2092 题解 对于每个\(1\)操作建一个虚点,以后的\(0\)操作都连在最近建好的虚点上.这样每次整体嫁接的时候,直接把这个虚点断掉它原来的父亲,再\( ...
- 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_类型的各种成员
[Class中,可能包含的成员] 常量, 字段, 实例构造器, 类型构造器, 方法, 操作符重载, 转换操作符, 属性, 事件, 类型(Class)
- 神奇的操作--O(1)快速乘
从同机房大佬那里听来的... 用O(1)时间求出两个相乘超过long long的数的取摸的结果 神奇的操作... inline long long multi(long long x,long lon ...
- POJ3368
http://poj.org/problem?id=3368 给出一个升序数组和 q 个查询.对每个查询,返回 a b 之间出现次数最多的那个元素的出现次数. 这一类区间查询的问题很容易想到用线段树来 ...
- NETSpider 网络蜘蛛采集工具
NETSpider网站数据采集软件是一款基于.Net平台的开源软件.软件部分功能是基本Soukey软件进行开发的.这个版本采用VS2010+.NET3.5进行开发的.NETSpider采摘当前提供的主 ...