Introduction to using Servicestackredis link Redis

Source: Internet
Author: User
Tags object serialization redis server

Note: For more information on how to configure Redis under Windows,linux, see this article:)

Currently there are some C # client tools linked to Redis, which is also currently used in our Enterprise Edition products. Servicestackredis, Link address:
Https://github.com/mythz/ServiceStack.Redis

The source package or DLL file in the link below is introduced into the project and adds the following name space reference (only for this article):
Using ServiceStack.Common.Extensions;
Using Servicestack.redis;
Using ServiceStack.Redis.Generic;
Using Servicestack.text;
Using ServiceStack.Redis.Support;
Note: The Servicestackredis encapsulates a number of methods and objects, which are only representative of the content, and more on its official documentation.

Declare a Client object:
protected Redisclient Redis = new Redisclient ("10.0.4.227", 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 ("Additemtolist", x));

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

Redis.addrangetolist ("Addarrangetolist", storemembers);

2. Get Data
var members = redis.getallitemsfromlist ("Additemtolist");
Members. ForEach (s = = Response.Write ("<br/>additemtolist:" + s));
3. Get the specified index location data
var item = redis.getitemfromlist ("Addarrangetolist", 2);
4. Removal:
var list = redis.lists["Addarrangetolist"];
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 = "Daizhj", age = 12});
Redisusers.store (new UserInfo {Id = Redisusers.getnextsequence (), UserName = "Daizhenjun", age = 13});

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[] {"10.0.4.210:6379"}, new string[] {"10.0.4.210: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:
1. The first three ways I tested in the local test found that access efficiency from high to the end, the specific reason is still to be analyzed.

2. 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 ("10.0.4.227", 6379);

This shows only one customer link on the Redis server

3. With the memcached test, it was found that the efficiency of both is close when stored (using the first method in this article), the memcached speed is faster than Redis at the time of data fetching (millisecond difference), which is not, as some online articles say, It appears that in the actual development and production environment, the use of background and results will prevail.


Test code Download Link:

/files/daizhj/redis.sample.rar

Introduction to using Servicestackredis link Redis

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.