https://www.cnblogs.com/ding2011/p/4745732.html
一::下载 windows 下的redis: https://github.com/ServiceStack/redis-windows
二:添加客户端的引用
下载连接( https://github.com/mythz/ServiceStack.Redis)
下面该链接中的源码包或dll文件,引入到项目中,并添加如下名空间引用(仅限本文):
using ServiceStack.Common.Extensions; using ServiceStack.Redis; using ServiceStack.Redis.Generic; using ServiceStack.Text; using ServiceStack.Redis.Support;
三:对redis的基本操作
https://www.cnblogs.com/daizhj/archive/2011/02/17/1956860.html
https://www.cnblogs.com/daxnet/archive/2012/03/16/2400418.html
目前网上有一些链接Redis的C#客户端工具,这里介绍其中也是目前我们企业版产品中所使用的ServiceStackRedis, 链接地址:
https://github.com/mythz/ServiceStack.Redis
下面该链接中的源码包或dll文件,引入到项目中,并添加如下名空间引用(仅限本文):
using ServiceStack.Redis;
using ServiceStack.Redis.Generic;
using ServiceStack.Text;
using ServiceStack.Redis.Support;
注:ServiceStackRedis封装了大量方法和对象,这里只摘有代表性的内容介绍,更多内容参见其官方文档。
声明一个客户端对象:
protected RedisClient Redis = new RedisClient("10.0.4.227", 6379);//redis服务IP和端口
一 .基本KEY/VALUE键值对操作:
1. 添加/获取:
storeMembers.ForEach(x => Redis.AddItemToList("additemtolist", x));
注:也可直接使用AddRangeToList方法将一组数据装入如:
Redis.AddRangeToList("addarrangetolist", storeMembers);
2. 获取数据members.ForEach(s => Response.Write("<br/>additemtolist :" + s));
3. 获取指定索引位置数据
4. 移除:
list.Clear();//清空
list.Remove("two");//移除指定键值
list.RemoveAt(2);//移除指定索引位置数据
{
public long Id { set; get; }
public string UserName { get; set; }
public int Age { get; set; }
}
1.通常方式(底层使用json序列化):
UserInfo userinfo = Redis.Get<UserInfo>("userinfo");
注:当然上面方式也适合于基本类型,如:
int age = Redis.Get<int>("my_age");
2.object序列化方式存储:

bool result = Redis.Set<byte[]>("userinfo", ser.Serialize(new UserInfo() { UserName = "张三", Age = 12 }));
UserInfo userinfo = ser.Deserialize(Redis.Get<byte[]>("userinfo")) as UserInfo;
//也支持列表
Redis.Set<byte[]>("userinfolist_serialize", ser.Serialize(userinfoList));
List<UserInfo> userList = ser.Deserialize(Redis.Get<byte[]>("userinfolist_serialize")) as List<UserInfo>;
需要说明的是在测试过程中发现JSON序列化的效率要比object序列化高一些。
三.存储表格对象,比如:

{
redisUsers.Store(new UserInfo { Id = redisUsers.GetNextSequence(), UserName = "daizhj", Age = 12 });
redisUsers.Store(new UserInfo { Id = redisUsers.GetNextSequence(), UserName = "daizhenjun", Age = 13 });
var allUsers = redisUsers.GetAll();//就像操作ado对象一样,可以进行CRUD等操作
allUsers.ForEach(s => Response.Write("<br/>user :" + s.UserName + " age:" + s.Age));
}
四.使用客户端链接池模式提升链接速度:

{
//支持读写分离,均衡负载
return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig
{
MaxWritePoolSize = 5,//“写”链接池链接数
MaxReadPoolSize = 5,//“写”链接池链接数
AutoStart = true,
});
}
声明链接池对象(这里只使用一个redis服务端):

List<UserInfo> userinfoList = new List<UserInfo>();
userinfoList.Add(new UserInfo() { UserName = "pool_daizhj", Age = 1 });
userinfoList.Add(new UserInfo() { UserName = "pool_daizhj1", Age = 2 });
从池中获取一个链接:
{
Redis.Set("userinfolist", userinfoList);
List<UserInfo> userList = Redis.Get<List<UserInfo>>("userinfolist");
}
1.前三种方式我在本地测试发现存取效率从高到底,具体原因还待分析。
2.如只想使用长链接而不是链接池的话,可以直接将下面对象用static方式声明即可:
protected static RedisClient Redis = new RedisClient("10.0.4.227", 6379);
这样在redis服务端显示只有一个客户链接
3.与memcached测试过程中发现,在存储时两者效率接近(使用本文第一种方式),在取数据时memcached速度比redis要快一些(毫秒级差异),这一点并不像网上一些文章所说的那样,看来在实际开发和生产环境下还要以使用背景及结果为准。
测试代码下载链接: