The Redis of C # wants to

Source: Internet
Author: User

A redis is a database of key-value pairs that support multiple data structures

1.1redis:https://github.com/microsoftarchive/redis

It is recommended to download the. msi end of the application for installation, and the Redis service will be installed automatically

Redis is not available for extranet access by default

To deploy Redis locally, ignore the following

Set Firewall port exceptions

Change the redis.conf file

bind 127.0.0.1protected-mode yes

Change to

# bind 127.0.0.1protected-mode no
然后重启Redis服务,

1.2 Redis Supported data types: String, list, set, SortedSet, Geo (Redis 3.2 or later), note that the values written by different methods can not be mixed, such as the Write list method to write the value of the method to obtain a string to get the problem.

Advantages of 1.3 Redis:

Support for multiple complex types of data structures

High-hit data is run in memory, the data can eventually be saved to the hard disk, the data will not be lost after the server restarts

The server is single-threaded and all commands from all clients are serially executed without worrying about concurrent modifications

Support for message subscription/notification mechanisms that can be used as Message Queuing

Key/value Maximum length allows 512M

Disadvantages of 1.4 Redis:

Redis is single-threaded, so an instance of a single redis can use only one CPU core of the server and does not take full advantage of server performance

Two in. NET operation of Redis

2.1 Mainly uses two open source components to operate Redis in. Net

1. Stackexchange.redis: Fewer dependent components, operation close to native Redis operations

2. Servicestack.redis: The number of dependent components, the high degree of encapsulation

NuGet Command Installation component Install-package Stackexchange.redis

2.2 Manipulating string-type data

Create a connection, write a character data and then read public async task<viewresult> Index () {        //Create a Redis connection because the Async method is provided, so I'm going to play a dirty operation    using (Connectionmultiplexer conn = await Connectionmultiplexer.connectasync ("***.**.**.***:6379"))    {        Idatabase DB = conn. Getdatabase ();        Write a string        bool flag= await db with a key name to Redis. Stringsetasync ("name", "123");        Read Data        var a= db. Stringget ("name");    }    return View ();}    

  

The string type can be used as a counter public async  task<viewresult> Index () {    using (connectionmultiplexer conn = Connectionmultiplexer.connect ("120.25.161.171:6379,abortconnect = false"))    {        Idatabase db = conn. Getdatabase ();        Stringappend appends content to the string of this key, no is created, returns the string        var A = db. Stringappend ("Dare to be Chiyo?") "," Yes Yes yes ");        The Stringincrementasync counter, which starts from 0 and adds 1, does not start at 0 and returns the result after a count of        long a = await db. Stringincrementasync ("The King cover the Ground Tiger", 1);        Long B = convert.toint64 (db). Stringget ("King of the Land Tiger"));        Long C =  db. Stringdecrement ("The Breeze Blows The Willow", 1);    }    return View ();}

  

2.3 Manipulating the list type data

Public async  task<viewresult> Index () {    using (connectionmultiplexer conn = Connectionmultiplexer.connect ("***.**.***.***:6379,abortconnect = false"))    {        Idatabase db = conn. Getdatabase ();        for (int i = 0; i <; i++)        {           //toward set U oh then push data           var a= await db. Listleftpushasync ("List1", "+i+" ");        }        The data is removed from the collection (Message Queuing)        Redisvalue b = db after the pop data pops read the data to the right of the collection. Listrightpop ("List1");        Reads all the data in the collection and does not remove the data        redisvalue[] C =await db. Listrangeasync ("List1");    }    return View ();}

  

2.5 Hash

Value is also a "set of key-value pairs" or the value is another Dictionary.

2.6 SortedSet Type of data

If there is a requirement for the data traversal order, you can use SortedSet and he will traverse it according to the score.

 Public async task<viewresult> Index () {using (Connectionmultiplexer conn = Connectionmultiplexer.connect ("120.2 5.161.171:6379,abortconnect = false ")) {Idatabase db = conn.        Getdatabase (); The sortedsetincrement is used to sort the vaule of the set data for (int i = 0; i < 5; i++) {var a = db.        Sortedsetincrement ("Resou", "Little Bear vs", 1); } for (int i = 0; i < 3; i++) {var b = db.        Sortedsetincrement ("Resou", "Timberg", 1); } for (int i = 0; I <6; i++) {var c = db.        Sortedsetincrement ("Resou", "Duan Zheng Chun", 1); } sortedsetentry[] d= db.        Sortedsetrangebyrankwithscores ("Resou");        foreach (var item in D) {Console.WriteLine (item);  }//Depending on the sort return value, you can query part of it according to the ordinal number;//redisvalue[] Sortedsetrangebyrank (Rediskey key, long start = 0, long stop =-1, Order order = order.ascending)//Depending on the sort return value, you can return only start-stop this range;//redisvalue[] Sortedsetrangebyscore (rediSKey key, double start = double. NegativeInfinity, double stop = double. PositiveInfinity, Exclude Exclude = Exclude.none, order order = order.ascending, long skip = 0, long take =-1)} R Eturn View ();}

  

2.5 basic operations for GEO data types

Geo is a new data type after Redis 3.2, which holds the coordinate information for points of interest (poi,point of interest). You can calculate the distance between two POI and get a POI with a specified distance around the point.

Public async  task<viewresult> Index () {    using (connectionmultiplexer conn = Connectionmultiplexer.connect ("120.25.161.171:6379,abortconnect = false"))    {        Idatabase db = conn. Getdatabase ();        Add a point of interest        db. Geoadd ("hehe", New Geoentry (11.22,12.23, "1"));        Db. Geoadd ("hehe", New Geoentry (11.32, 12.23, "2"));        Db. Geoadd ("hehe", New Geoentry (11.42, 12.23, "3"));        Gets the coordinate geoposition based on the primary key of the point        ? pos = db. Geoposition ("Shopsgeo", "1");        Calculates the distance between two points of interest        var a= db. Geodistance ("hehe", "1", "3", geounit.meters);        Calculates the remaining points of interest within a point of interest        georadiusresult[] grr= db. Georadius ("hehe", 1,10000,geounit.meters);        Calculates the distance within a latitude and longitude range        georadiusresult[] grr2 = db. Georadius ("hehe", 11.42, 12.23,1000, geounit.meters);        foreach (var item in GRR)        {           Console.WriteLine (item. Member + ":" + Item. Distance + "M");        }    }    return View ();}

  

Three Redis batch Operations

If you perform more than one Redis operation at a time, a lot is slow and you can use bulk operations.

There are two main ways of doing this:

1) Almost all operations support the array type, so that multiple data can be manipulated at once: such as Geoadd (Rediskey key, geoentry[] values), Sortedsetadd (Rediskey key, Sortedsetentry [] values)

2) If the one-time operation is not a simple operation, then use batch mode:

Ibatch batch = db. Createbatch (); Db. Geoadd ("ShopsGeo1", New Geoentry (116.34039, 39.94218, "1")); Db. Stringset ("abc", "123"); Batch. Execute ();

The operation between the Createbatch (), Execute () of the current connection is submitted to the server once.

The Redis of C # wants to

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.