標籤:
: 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;
聲明一個用戶端對象:
protected RedisClient Redis = new RedisClient("127.0.0.1", 6379);//redis服務IP和連接埠
一 .基本KEY/VALUE索引值對操作:
1. 添加/擷取:
List<string> storeMembers = new List<string>();
storeMembers.ForEach(x => Redis.AddItemToList("test", x));
註:也可直接使用AddRangeToList方法將一組資料裝入如:
Redis.AddRangeToList("testt", storeMembers);
2. 擷取資料
var members = Redis.GetAllItemsFromList("test");
members.ForEach(s => Response.Write("<br/>test :" + s));
3. 擷取指定索引位置資料
var item = Redis.GetItemFromList("test", 2);
4. 移除:
var list = Redis.Lists["test"];
list.Clear();//清空
list.Remove("two");//移除指定索引值
list.RemoveAt(2);//移除指定索引位置資料
二.儲存物件:
public class UserInfo
{
public long Id { set; get; }
public string UserName { get; set; }
public int Age { get; set; }
}
1.通常方式(底層使用json序列化):
Redis.Set<UserInfo>("userinfo", new UserInfo() { UserName = "李四", Age = 45 });
UserInfo userinfo = Redis.Get<UserInfo>("userinfo");
註:當然上面方式也適合於基本類型,如:
Redis.Set<int>("my_age", 12);//或Redis.Set("my_age", 12);
int age = Redis.Get<int>("my_age");
2.object序列化方式儲存:
var ser = new ObjectSerializer(); //位於namespace ServiceStack.Redis.Support;
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序列化高一些。
三.儲存表格對象,比如:
using (var redisUsers = Redis.GetTypedClient<UserInfo>())
{
redisUsers.Store(new UserInfo { Id = redisUsers.GetNextSequence(), UserName = "test1", Age = 22 });
redisUsers.Store(new UserInfo { Id = redisUsers.GetNextSequence(), UserName = "test2", Age = 23 });
var allUsers = redisUsers.GetAll();//就像操作ado對象一樣,可以進行CRUD等操作
allUsers.ForEach(s => Response.Write("<br/>user :" + s.UserName + " age:" + s.Age));
}
四.使用用戶端連結池模式提升連結速度:
public static PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts)
{
//支援讀寫分離,均衡負載
return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig
{
MaxWritePoolSize = 5,//“寫”連結池連結數
MaxReadPoolSize = 5,//“寫”連結池連結數
AutoStart = true,
});
}
聲明連結池對象(這裡只使用一個redis服務端):
PooledRedisClientManager prcm = CreateManager(new string[] { "127.0.0.1:6379" }, new string[] { "127.0.0.1:6379" });
List<UserInfo> userinfoList = new List<UserInfo>();
userinfoList.Add(new UserInfo() { UserName = "pool_daizhj", Age = 1 });
userinfoList.Add(new UserInfo() { UserName = "pool_daizhj1", Age = 2 });
從池中擷取一個連結:
using (IRedisClient Redis = prcm.GetClient())
{
Redis.Set("userinfolist", userinfoList);
List<UserInfo> userList = Redis.Get<List<UserInfo>>("userinfolist");
}
註:
如只想使用長連結而不是連結池的話,可以直接將下面對象用static方式聲明即可:
protected static RedisClient Redis = new RedisClient("127.0.0.1", 6379);
這樣在redis服務端顯示只有一個客戶連結
c#中使用servicestackredis操作redis