ServiceStack.Redis 使用
2024-09-01 15:18:45
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. 输出结果
最新文章
- javascript 基础
- PHP根据array_map、array_reduce和array_column获取二维数组中某个key的集合
- SVN 远程无法联通
- gulp-uglify《JS压缩》----gulp系列(四)
- linux下安装7z命令及7z命令的使用
- IOSView显示特性设置
- echart饼状图的学习
- C 语言文件操作
- 添加jar
- 利用iptables将本地的80端口请求转发到8080,当前主机ip为192.168.1.1,命令怎么写?
- virtualenv 安装不同版本的虚拟环境的办法
- 机器学习:Python实现单层Rosenblatt感知器
- centos7 部署dns服务器
- TCP协议学习总结(上)
- 06_ for 练习 _ 年利率
- Git仓库完全迁移,包括所有的分支和标签,当然也包括日志
- input()和print()函数同时输入输出多个数据--python3
- Python3 与 NetCore 基础语法对比(就当Python和C#基础的普及吧)
- 网络通信协议五之IP协议详解
- webpack学习总结(一)