Using Servicestackredis to operate Redis in C #

Source: Internet
Author: User
Tags object serialization redis server

: Https://github.com/mythz/ServiceStack.Redis

To add a DLL reference:

Using ServiceStack.Common.Extensions;
Using Servicestack.redis;
Using ServiceStack.Redis.Generic;
Using Servicestack.text;
Using ServiceStack.Redis.Support;


Declare a Client object:
protected Redisclient Redis = new Redisclient ("127.0.0.1", 6379);//redis Service IP and Ports


I. Basic Key/value key-value pair operation:
1. Add/Get:


list<string> storemembers = new list<string> ();
Storemembers.foreach (x = redis.additemtolist ("Test", X));

Note: You can also directly use the Addrangetolist method to load a set of data such as:

redis.addrangetolist ("Testt", storemembers);



2. Get Data


var members = redis.getallitemsfromlist ("test");
Members. ForEach (s = = Response.Write ("<br/>test:" + s));

3. Get the specified index location data


var item = redis.getitemfromlist ("Test", 2);

4. Removal:


var list = redis.lists["Test"];
List. Clear ();//Clear
List. Remove ("both");//Remove the specified key value
List. RemoveAt (2);//Remove the specified index location data

Two. Storage objects:


Public class UserInfo
{
Public long Id {set; get;}
public string UserName {get; set;}
public int Age {get; set;}
}

1. The usual way (the bottom layer uses JSON serialization):


redis.set<userinfo> ("UserInfo", new UserInfo () {UserName = "John Doe", age = 45});
UserInfo UserInfo = redis.get<userinfo> ("UserInfo");

Note: Of course, the above method is also suitable for basic types, such as:


redis.set<int> ("My_age", 12);//or Redis.set ("My_age", 12);
int age = redis.get<int> ("My_age");


2.object Serialization Mode storage:

var ser = new Objectserializer (); Located in namespace ServiceStack.Redis.Support;
BOOL result = redis.set<byte[]> ("UserInfo", Ser. Serialize (New UserInfo () {UserName = "Zhang san", age = 12});
UserInfo UserInfo = ser. Deserialize (redis.get<byte[]> ("userinfo")) as UserInfo;
Also support list
Redis.set<byte[]> ("Userinfolist_serialize", Ser. Serialize (userinfolist));
list<userinfo> userlist = ser. Deserialize (redis.get<byte[]> ("Userinfolist_serialize")) as list<userinfo>;

It is necessary to note that the efficiency of JSON serialization is found to be higher than the object serialization during the testing process.

Three. Store table objects, such as:


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 ();//As with ADO objects, you can do crud operations like
Allusers.foreach (s = Response.Write ("<br/>user:" + S.username + "Age:" + s.age));
}


Four. Increase link speed using Client link Pool mode:


Public static Pooledredisclientmanager Createmanager (string[] readwritehosts, string[] readonlyhosts)
{
Supports read-write separation, balanced load
return new Pooledredisclientmanager (Readwritehosts, readonlyhosts, new Redisclientmanagerconfig
{
Maxwritepoolsize = 5,//"write" link Pool link number
Maxreadpoolsize = 5,//"write" link Pool link number
AutoStart = True,
});
}



Declare the link pool object (only one Redis server is used here):

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});

Get a link from the pool:


using (iredisclient Redis = prcm. Getclient ())
{
Redis.set ("Userinfolist", userinfolist);
List<userinfo> userlist = redis.get<list<userinfo>> ("Userinfolist");
}

Note:
If you want to use a long link instead of a link pool, you can simply declare the following object in static mode:
protected static Redisclient Redis = new Redisclient ("127.0.0.1", 6379);
This shows only one customer link on the Redis server

Using Servicestackredis to operate Redis in C #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.