在c#中使用servicestackredis操作redis的實(shí)例代碼
聲明一個客戶端對象:
protected RedisClient Redis = new RedisClient("127.0.0.1", 6379);//redis服務(wù)IP和端口
一 .基本KEY/VALUE鍵值對操作:
1. 添加/獲取:
List<string> storeMembers = new List<string>();
storeMembers.ForEach(x => Redis.AddItemToList("test", x));
//注:也可直接使用AddRangeToList方法將一組數(shù)據(jù)裝入如:
Redis.AddRangeToList("testt", storeMembers);
2. 獲取數(shù)據(jù)
var members = Redis.GetAllItemsFromList("test");
members.ForEach(s => Response.Write("<br/>test :" + s));
3. 獲取指定索引位置數(shù)據(jù)
var item = Redis.GetItemFromList("test", 2);
4. 移除:
var list = Redis.Lists["test"];
list.Clear();//清空
list.Remove("two");//移除指定鍵值
list.RemoveAt(2);//移除指定索引位置數(shù)據(jù)
二.存儲對象:
[Serializable]
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");
注:當(dāng)然上面方式也適合于基本類型,如:
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;
//也支持列表
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<byte[]>("userinfolist_serialize", ser.Serialize(userinfoList));
List<UserInfo> userList = ser.Deserialize(Redis.Get<byte[]>("userinfolist_serialize")) as List<UserInfo>;
需要說明的是在測試過程中發(fā)現(xiàn)JSON序列化的效率要比object序列化高一些。
三.存儲表格對象,比如:
using (var redisUsers = Redis.As<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對象一樣,可以進(jìn)行CRUD等操作
}
四.使用客戶端鏈接池模式提升鏈接速度:
public static PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts)
{
//支持讀寫分離,均衡負(fù)載
return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig
{
MaxWritePoolSize = 5,//“寫”鏈接池鏈接數(shù)
MaxReadPoolSize = 5,//“讀”鏈接池鏈接數(shù)
AutoStart = true,
});
}
聲明鏈接池對象(這里只使用一個redis服務(wù)端):
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服務(wù)端顯示只有一個客戶鏈接
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用HttpHanlder處理404:File not found的問題
本篇文章小編為大家介紹。使用HttpHanlder處理404:File not found的問題。需要的朋友參考下2013-04-04
C#實(shí)現(xiàn)目錄跳轉(zhuǎn)(TreeView和SplitContainer)的示例代碼
本文主要介紹了C#實(shí)現(xiàn)目錄跳轉(zhuǎn)(TreeView和SplitContainer)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
C# 中的 IReadOnlyDictionary 和 IReadOnlyLis
C# 中的IReadOnlyDictionary和IReadOnlyList是接口,用于表示只讀的字典和只讀的列表,這些接口提供了對集合的只讀訪問權(quán)限,即不允許對集合進(jìn)行修改操作,這篇文章主要介紹了C# 中的 IReadOnlyDictionary 和 IReadOnlyList實(shí)例詳解,需要的朋友可以參考下2024-03-03
Winform ComboBox如何獨(dú)立繪制下拉選項的字體顏色
這篇文章主要介紹了Winform ComboBox如何獨(dú)立繪制下拉選項的字體顏色,幫助大家更好的理解和使用c# winform,感興趣的朋友可以了解下2020-11-11
C#使用Socket實(shí)現(xiàn)心跳的方法示例
這篇文章主要介紹了C#使用Socket實(shí)現(xiàn)心跳的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02

