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赞助.在这之 ...
随机推荐
- JasperReports项目中的应用
转自:http://www.blogjava.net/vjame/archive/2013/10/12/404908.html . 2.业务处理 //返回报表查询结果 List<ReportEl ...
- Mybaits整合Spring自动扫描 接口,Mybaits配置文件.xml文件和Dao实体类
1.转自:https://blog.csdn.net/u013802160/article/details/51815077 <?xml version="1.0" enco ...
- Java探索之旅(17)——多线程(1)
1.多线程 1.1线程 线程是程序运行的基本执行单元.指的是一段相对独立的代码,执行指定的计算或操作.多操作系统执行一个程序时会在系统中建立一个进程,而在这个进程中,必须至少建立一个线程(这个线程被 ...
- LoadRunner 12 模拟 RSA加密 登录的实现(JS)
LR 12 中 web_js_run API 非常坑,只能调用一个 JS 文件:更坑的是,不能通用 一个JS调用另外一个JS:(可能有,但在网上找了N个国家,都没有找到!如有,还请朋友告之,谢谢.) ...
- Flask05 cookie、类视图、方法视图、自己的404页面
1 什么是cookie 就是网站存放到你浏览器中的一部分固定内容:当你下次访问我这个网站的时候,你会把之前我存放到你浏览器中的数据带回来给我 你要先登录(用户名.密码) -> ...
- Luogu 3320 [SDOI2015]寻宝游戏
一开始还真没想到. 发现从所有有宝藏的点出发绕一圈只要不刻意绕路答案都是一样的,即我们呢要求的最后答案$ans = dis(x_1, x_2) + dis(x_2, x_3) +... + dis(x ...
- Umbraco Examine 实现Fuzzy search
在Umbraco examine search项目开发中,有一个需求, 就是intercom 和 intercoms需要返回同样的结果 也就是说 搜索intercom 时, 能返回包含intercom ...
- 2.XML实体注入漏洞攻与防
XML实体注入基础 当允许引用外部实体时,通过构造恶意内容,可导致读取任意文件.执行系统命令.探测内网端口.攻击内网网站等危害. 简单了解XML以后,我们知道要在XML中使用特殊字符,需要使用实体字符 ...
- eclipse中导入项目后提示错误:The project was not built due to"Could not delete'/文件夹路径名
eclipse中导入项目 1.新建一个项目: 2.把已存在的项目的所有文件复制到该新建的项目下: 3.把lib文件夹中的jar导入:(右键-)add into path……): 4.常见问题如下 ec ...
- 设置css属性