聲明一個(gè)客戶端對(duì)象:
創(chuàng)新互聯(lián)建站專注于企業(yè)全網(wǎng)營(yíng)銷推廣、網(wǎng)站重做改版、古田網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5場(chǎng)景定制、商城建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為古田等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
一 .基本KEY/VALUE鍵值對(duì)操作:
1. 添加/獲取:
ListstoreMembers = new List (); 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("
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ù)
二.存儲(chǔ)對(duì)象:
[Serializable] public class UserInfo { public long Id { set; get; } public string UserName { get; set; } public int Age { get; set; } }
1.通常方式(底層使用json序列化):
Redis.Set("userinfo", new UserInfo() { UserName = "李四", Age = 45 }); UserInfo userinfo = Redis.Get ("userinfo");
注:當(dāng)然上面方式也適合于基本類型,如:
Redis.Set("my_age", 12);//或Redis.Set("my_age", 12); int age = Redis.Get ("my_age");
2.object序列化方式存儲(chǔ):
var ser = new ObjectSerializer(); //位于namespace ServiceStack.Redis.Support; bool result = Redis.Set("userinfo", ser.Serialize(new UserInfo() { UserName = "張三", Age = 12 })); UserInfo userinfo = ser.Deserialize(Redis.Get ("userinfo")) as UserInfo; //也支持列表 List userinfoList = new List (); userinfoList.Add(new UserInfo() { UserName = "pool_daizhj", Age = 1 }); userinfoList.Add(new UserInfo() { UserName = "pool_daizhj1", Age = 2 }); Redis.Set ("userinfolist_serialize", ser.Serialize(userinfoList)); List userList = ser.Deserialize(Redis.Get ("userinfolist_serialize")) as List ;
需要說(shuō)明的是在測(cè)試過(guò)程中發(fā)現(xiàn)JSON序列化的效率要比object序列化高一些。
三.存儲(chǔ)表格對(duì)象,比如:
using (var redisUsers = Redis.As()) { 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對(duì)象一樣,可以進(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, }); }
聲明鏈接池對(duì)象(這里只使用一個(gè)redis服務(wù)端):
PooledRedisClientManager prcm = CreateManager(new string[] { "127.0.0.1:6379" }, new string[] { "127.0.0.1:6379" }); ListuserinfoList = new List (); userinfoList.Add(new UserInfo() { UserName = "pool_daizhj", Age = 1 }); userinfoList.Add(new UserInfo() { UserName = "pool_daizhj1", Age = 2 });
從池中獲取一個(gè)鏈接:
using (IRedisClient Redis = prcm.GetClient()) { Redis.Set("userinfolist", userinfoList); ListuserList = Redis.Get >("userinfolist"); }
注:
如只想使用長(zhǎng)鏈接而不是鏈接池的話,可以直接將下面對(duì)象用static方式聲明即可:
protected static RedisClient Redis = new RedisClient("127.0.0.1", 6379);
這樣在redis服務(wù)端顯示只有一個(gè)客戶鏈接
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。