ServiceStack.Redis 使用
Redis官网提供了很多开源的C#客户端。例如,Nhiredis ,ServiceStack.Redis ,StackExchange.Redis等。其中ServiceStack.Redis应该算是比较流行的。它提供了一整套从Redis数据结构都强类型对象转换的机制并将对象json序列化。所以这里只介绍ServiceStack.Redis,它也是目前我们产品中所使用的客户端。
ServiceStack.Redis地址:https://github.com/ServiceStack/ServiceStack.Redis
1. 建立一个控制台应用程序,并引用以下ServiceStack.Redis相关的四个类库。或者通过Nuget进行安装Redis常用组件ServiceStack.Redis。 下载示例代码。
2. 创建一个Redis操作的公用类RedisCacheHelper,
using
System;
using
System.Collections.Generic;
using
System.Configuration;
using
System.Linq;
using
System.Text;
using
System.Web;
using
ServiceStack.Common.Extensions;
using
ServiceStack.Redis;
using
ServiceStack.Logging;
namespace
Weiz.Redis.RedisTest
{
public
class
RedisCacheHelper
{
private
static
readonly
PooledRedisClientManager pool =
null
;
private
static
readonly
string
[] redisHosts =
null
;
public
static
int
RedisMaxReadPool =
int
.Parse(ConfigurationManager.AppSettings[
"redis_max_read_pool"
]);
public
static
int
RedisMaxWritePool =
int
.Parse(ConfigurationManager.AppSettings[
"redis_max_write_pool"
]);
static
RedisCacheHelper()
{
var
redisHostStr = ConfigurationManager.AppSettings[
"redis_server_session"
];
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);
}
}
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);
}
}
}
}
catch
(Exception ex)
{
string
msg =
string
.Format(
"{0}:{1}发生异常!{2}"
,
"cache"
,
"存储"
, key);
}
}
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);
}
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);
}
}
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);
}
return
false
;
}
}
}
说明:RedisCacheHelper 使用的是客户端链接池模式,这样的存取效率应该是最高的。同时也更方便的支持读写分离,均衡负载。
3. 配置文件
<!-- redis Start -->
<add key=
"SessionExpireMinutes"
value=
"180"
/>
<add key=
"redis_server_session"
value=
"127.0.0.1:6379"
/>
<add key=
"redis_max_read_pool"
value=
"3"
/>
<add key=
"redis_max_write_pool"
value=
"1"
/>
<!--redis end-->
4. 测试程序调用
class
Program
{
static
void
Main(
string
[] args)
{
Console.WriteLine(
"Redis写入缓存:zhong"
);
RedisCacheHelper.Add(
"zhong"
,
"zhongzhongzhong"
, DateTime.Now.AddDays(1));
Console.WriteLine(
"Redis获取缓存:zhong"
);
string
str3 = RedisCacheHelper.Get<
string
>(
"zhong"
);
Console.WriteLine(str3);
Console.WriteLine(
"Redis获取缓存:nihao"
);
string
str = RedisCacheHelper.Get<
string
>(
"nihao"
);
Console.WriteLine(str);
Console.WriteLine(
"Redis获取缓存:wei"
);
string
str1 = RedisCacheHelper.Get<
string
>(
"wei"
);
Console.WriteLine(str1);
Console.ReadKey();
}
}
5. 输出结果
ServiceStack.Redis 使用的更多相关文章
- .Net使用Redis详解之ServiceStack.Redis(七)
序言 本篇从.Net如何接入Reis开始,直至.Net对Redis的各种操作,为了方便学习与做为文档的查看,我做一遍注释展现,其中会对list的阻塞功能和事务的运用做二个案例,进行记录学习. Redi ...
- ServiceStack.Redis订阅发布服务的调用(Z)
1.Redis订阅发布介绍Redis订阅发布是一种消息通信模式:发布者(publisher)发送消息,订阅者(Subscriber)接受消息.类似于设计模式中的观察者模式.发布者和订阅者之间使用频 ...
- serviceStack.Redis 在PooledRedisClientManager 中设置密码
ServiceStack.Redis 是一个C#访问Redis的客户端,可以说可以通过它实现所有需要Redis-Cli的功能.但是今天我在主Redis 实例设置了访问密码,而在slave 上没有设置, ...
- ServiceStack.Redis订阅发布服务的调用
1.Redis订阅发布介绍 Redis订阅发布是一种消息通信模式:发布者(publisher)发送消息,订阅者(Subscriber)接受消息.类似于设计模式中的观察者模式. 发布者和订阅者之间使用频 ...
- Redis简介、与memcached比较、存储方式、应用场景、生产经验教训、安全设置、key的建议、安装和常用数据类型介绍、ServiceStack.Redis使用(1)
1.NOSQL简介 nosql的产生并不是要彻底的代替关系型数据库,而是作为传统关系型数据库的一个补充. Facebook和360使用Cassandra来存储海量社交数据 Twitter在其url抓取 ...
- ServiceStack.Redis 使用教程
http://www.cnblogs.com/shanyou/archive/2011/11/10/2245082.html https://github.com/ServiceStack/Servi ...
- 在使用Redis的客户端连接工具ServiceStack.Redis要注意的问题
在使用Redis的客户端连接工具ServiceStack.Redis要注意的问题 Redis是一个非常NB的内存级的数据库,我们可以把很多”热数据“(即读写非常多的数据)放入其中来操作,这样就减少 ...
- 关于 ServiceStack.Redis 4.0 License
今天更新了框架中的Redis驱动ServiceStack.Redis,最新版本4.0.5.0. 在做简单压力测试时出现异常,提示每小时允许6000个请求. The free-quota limit o ...
- ServiceStack.Redis.RedisNativeClient的方法“get_Db”没有实现
Redis 4.0.0.0版本已经开始收费 Redis 4.0.5.0 已经完成收费 今日在更换Redis版本时 出现了ServiceStack.Redis.RedisNativeClient的方法“ ...
- ServiceStack.Redis
什么是Redis 首先,简述一下什么是Redis. Redis是一个开源.支持网络.基于内存.键值对存储数据库,使用ANSI C编写.从2013年5月开始,Redis的开发由Pivotal赞助.在这之 ...
随机推荐
- springMVC绑定json参数之二(2.2.4)
二.springmvc 接收不同格式的json字符串 4).格式四:json传递复杂对象(对象中有属性,还有List,这里验证数组接收,不用List,其他的和上一节不变) 测试一: Test对象属性如 ...
- shell入门-连接符(并且、和、或者)
特殊符号:&& 说明:并且,左右两边是两条命令,左面的执行成功才会去执行右面的命令.右. 特殊符号:|| 说明:或者,左右两边是两条命令,左边的命令执行不成功,才会执行右面的命令 &a ...
- Java探索之旅(10)——数组线性表ArrayList和字符串生成器StringBuffer/StringBuilder
1.数组线性表ArrayList 数组一旦定义则不可改变大小.ArrayList可以不限定个数的存储对象.添加,插入,删除,查找比较数组更加容易.可以直接使用引用类型变量名输出,相当于toString ...
- socket消息发送
expressClient.html <html><head><meta http-equiv="Content-Type" content=&quo ...
- call apply bind 的区别
1.call和apply都是对函数的直接调用,而bind方法返回的仍然是一个函数,因此后面还需要()来进行调用才可以 var xw={ name: "小王", gender: &q ...
- 数据库导入sql文件
数据库导入sql文件包括两种方式: 一.命令行导入 二.数据库工具导入 后续完善
- 具体问题:3、hibernate跟Mybatis/ ibatis 的区别,为什么选择?
第一章 Hibernate与MyBatis Hibernate 是当前最流行的O/R mapping框架,它出身于sf.net,现在已经成为Jboss的一部分. Mybatis 是另外一种优秀 ...
- 2、Tophat align_summary.txt and samtools flagstat accepted_hits.bam disagree
###https://www.biostars.org/p/195758/ Left reads: Input : 49801387 Mapped : 46258301 (92.9% of input ...
- jquery的命名空间
function A( event ){ alert( 'A' );}function B( event ){ alert( 'B' );}function C( event ){ ...
- 7.30实习培训日志-SQL优化
总结 今天早上考试,下午主要是老师引导我们学习SQL优化,晚上主要是同学的技术分享,杨松柏同学主要给我们分享了java的io的一些东西,c10k问题,bio(同步阻塞IO),NIO(同步非阻塞IO), ...