.Net Core distributed cache Redis: Data Structure

I. Introduction

Data in this chapter use StackExchangeRedis use Redis in .Net Core, a foundation, see: here.

Second, the five base data structure

1. String type String

Redis string type is the most basic type of data, it can store any kind of string, including binary data. You can use it to store a user's mailbox, JSON objects or even of a picture. Allowing a maximum key string type data storage capacity is available to 512MB.

String type is the basis of four kinds of data types, string types and other data types differ in some ways different from just a string tissue. For example, a list of tissue types in the form of a list of strings, a set of two tissue types in the form of a set of strings.

The following is a common method and StackExchangeRedis command string:

(1) The value assigned to the

//方法
redisConnection.GetDatabase().StringSetAsync(key, value);
redisConnection.GetDatabase().StringGetAsync(key); 
//命令
127.0.0.1:6379> set stringKey stringValue
OK
127.0.0.1:6379> get stringKey
"stringValue"

(2) returns a character sub-string value in the key

//方法
redisConnection.GetDatabase().StringGetRangeAsync(key, start, end);
//命令
127.0.0.1:6379> getrange stringKey 6 10
"Value"

(3) a given set of values ​​key value, and returns the key of the old value (old value)

//方法
var oldvalue = await redisConnection.GetDatabase().StringGetSetAsync(key, oldkey);
//命令
127.0.0.1:6379> getset stringKey newValue
"stringValue"

(4) If a key string already exists, APPEND command specified key value appended to the original value (value) of

//方法
redisConnection.GetDatabase().StringAppendAsync(key, appendValue);
//命令
127.0.0.1:6379> append stringKey append
(integer) 14

2. The type of hash Hash

Redis stored data is in the form of key-value pairs in a dictionary structure, and the Hash key value is also a dictionary structure, which stores map field (field) and a field value, the field value can be a string, does not support other types of data, other data types can not be nested (the same way other data types).

Hash type adapted to store objects: object category using the keys and the ID configuration, using field indicates the attributes of the object, and the property value field value is stored. For example, to store the object ID for the automobile 2, you can use three fields named color, name and price to store color, name and price of the cars respectively.

The following types of hash StackExchangeRedis common methods and commands:

(1) Assignment

//方法
HashEntry[] hashEntry = new HashEntry[] {
  new HashEntry("id",2),
  new HashEntry("color","red"),
  new HashEntry("price",200),
};
redisConnection.GetDatabase().HashSetAsync("youCar", hashEntry);
//命令
127.0.0.1:6379> hset myCar price 200
(integer) 1
127.0.0.1:6379> hset mCar color blue
(integer) 1 
127.0 . 0.1 : 6379 > Seth Caro name tractor
(integer) 1

(2) Value

//方法
redisConnection.GetDatabase().HashGetAsync("youCar", "color");
redisConnection.GetDatabase().HashGetAllAsync("youCar");
//命令
127.0.0.1:6379> hget youCar color
"red"
127.0.0.1:6379> hgetall youCar
1) "id"
2) "2"
3) "color"
4) "red"
5) "price"
6) "200"

(3) an overview of all the fields in the type of hash

//方法
var keys = redisConnection.GetDatabase().HashKeys("youCar");
//命令
127.0.0.1:6379> hkeys youCar
1) "id"
2) "color"
3) "price"

3. List List

Type list (List) can store an ordered list of strings, common operation is to add elements to the ends of the list, or a list of fragments is obtained.

Internal list type using a doubly linked list (double linked list) is implemented, so the time to add complex elements are O (1) to the two lists, both ends of the elements closer to obtain faster. This means that even a list of tens of millions of elements, get a head or tail of 10 records is also very fast.

But the cost of using a linked list is relatively slow access the elements by index. Accordingly, it is a list of the type adapted to novelty as social networking sites, logging, message queue, the most similar to the first few data acquisition, and other scenes from the insertion end of the list.

The following is a list of the type commonly used methods StackExchangeRedis its command:

(1) add elements to the list ends

//方法
redisConnection.GetDatabase().ListLeftPushAsync("myList","head1");
redisConnection.GetDatabase().ListRightPush("myList","bottom1");
//命令
127.0.0.1:6379> lpush myList head1
(integer) 1127.0.0.1:6379> rpush myList bottom1
(integer) 2

(2) is removed from both ends of the element and obtain a listing

//方法
var value = redisConnection.GetDatabase().ListLeftPopAsync("myList");
var value = redisConnection.GetDatabase().ListRightPopAsync("myList");
//命令
127.0.0.1:6379> lpop myList
"head1"
127.0.0.1:6379> rpop myList
"bottom1"
127.0.0.1:6379> rpop myList
(nil)

(3) to obtain fragments element in the list (without deleting get only)

//方法
var value = redisConnection.GetDatabase().ListRangeAsync("myList",0,2);
//命令
127.0.0.1:6379> lrange myList 0 2
1) "1"
2) "2"
3) "3"

4. Set collection

Redis of type String Set is an unordered collection. It is the only member of the collection, which means that the collection of duplicate data can not appear. Common operation is added to the set or remove elements, determines whether there is an element, etc., since the collection of Redis hash table is implemented by, so to add, delete, search complexity is O (1).

Most conveniently a plurality of sets may also be set between the type of the keys and, intersection, and difference calculation.

Can be used in mutual friend, preferences and friend suggestions (intersection exceeds the threshold value), etc. can be set using the cross and operation scenarios.

The following is a collection of the type commonly used methods StackExchangeRedis and command:

(1) add / remove elements (can be more of the same elements automatically ignored)

//方法
redisConnection.GetDatabase().SetAddAsync("mySet", new RedisValue[] { "a", "b" });
redisConnection.GetDatabase().SetRemoveAsync("mySet", "a");
//命令
127.0.0.1:6379> sadd mySet a
(integer) 1
127.0.0.1:6379> sadd mySet a b c
(integer) 2
127.0.0.1:6379> srem mySet c
(integer) 1
127.0.0.1:6379> srem mySet b a
(integer) 2

(2) to obtain the intersection

//方法
var value = redisConnection.GetDatabase().SetCombine(SetOperation.Intersect, "mySet1", "mySet2");
//命令
127.0.0.1:6379> sadd mySet1 a b c
(integer) 3
127.0.0.1:6379> sadd mySet2 b c d
(integer) 3
127.0.0.1:6379> sinter mySet1 mySet2
1) "c"
2) "b"

5. ordered set Sort Set

From the name of the ordered set we can tell the difference is the "orderly" word and its collections.

In the collection type of foundation is still indexed collections for the collection of each element is associated with a score performed by scores of small to large size sorting, which allows us not only to complete the insertion, deletion and determine whether the element is waiting for collection types supported operating, it is possible to obtain the highest (or lowest) before fractional N elements, obtained scores and other elements related to operation within a specified range of scores. Although each element of the collection is different, but their scores but can be the same.

Indexed collections somewhat similar in some ways, and list type

  • Both are ordered.

  • Both can get a range of elements

But both of these very different, which makes them different application scenarios.

  • List is implemented by a linked list, obtaining data speeds close to both ends of the fast, and when the element increases, the speed of accesses to the intermediate element may be slow, so it is more suitable for achieving such as "novelty" or "log" so rarely accessed intermediate application elements.

  • Using a hash table is an ordered set and the jump table (skip list) implemented, even if the data read speed is also fast entertainment intermediate portion (time complexity of O (log (N))).

  • The list can not simply adjust the position of the element, but can be ordered set (by changing elements of the score), it can be used for leaderboards, weight applications (the right to re-message queue).

  • Ordered set of categories than consume more memory.

The following is a method of the type commonly used in the ordered set and StackExchangeRedis command:

(1) add elements

//方法
SortedSetEntry[] sortedSetEntry = new SortedSetEntry[] {
  new SortedSetEntry("tom",10),
  new SortedSetEntry("peter",20),
  new SortedSetEntry("david",30),
};
redisConnection.GetDatabase().SortedSetAddAsync("mySs", sortedSetEntry);
//命令
127.0.0.1:6379> zadd mySs 10 tom 20 peter 30 david
(integer) 3

(2) obtain a ranking score range of list elements

//方法
var value = redisConnection.GetDatabase().SortedSetRemoveRangeByScoreAsync("mySs", 10, 20);
//命令
127.0.0.1:6379> zrangebyscore mySs 10 20
1) "tom"
2) "peter"

(3) increase the score of an element

//方法
var sorce = redisConnection.GetDatabase().SortedSetIncrementAsync("mySs", "top", 3);
//命令
127.0.0.1:6379> zincrby mySs 3 tom
"13"

6. Summary

The above cited various types have limited example, substantially all Redis commands StackExchangeRedis has a corresponding asynchronous and synchronous method, we can refer https://redis.io/commands .

Third, other data structures

1.HyperLogLog

In Redis 2.8.9 release adds HyperLogLog structure (which itself is an algorithm).

Redis HyperLogLog base is used to make statistical algorithms HyperLogLog advantage is that, when the number of input elements or very, very large volume of space required for the calculation base is always fixed, and is very small.

In Redis inside each key HyperLogLog takes only 12 KB of memory, you can calculate the closest base 2 ^ 64 different elements. When this calculation and the base, the more the cost of memory elements of the set, the more contrast.

However, because HyperLogLog will be calculated based on the input element base, but does not store the input element itself, so HyperLogLog not like a collection that returns each element inputs.

It is mainly used in:

  • Statistics IP access number;

  • Search statistics the number of keywords;

  • Count the number of users online;

  • And so large number of statistics.

The following is a commonly used method and StackExchangeRedis command:

(1) add the specified element to HyperLogLog

//方法
RedisValue[] redisValue = new RedisValue[] {
    110,120,130,130
};
redisConnection.GetDatabase().HyperLogLogAddAsync("key", redisValue);
//命令
127.0.0.1:6379> pfadd hyperlogKy 110 120 130 130
(integer) 1

(2) returns HyperLogLog given cardinality estimates.

//方法
var count = redisConnection.GetDatabase().HyperLogLogLength("key");
//命令
127.0.0.1:6379> pfcount hyperlogKy
(integer) 3

2.geo

After Redis 3.2 version adds a geo (geographic location), its data structure is an ordered set of sort set.

geo location information (latitude and longitude) can be stored, and calculating a position between the two geographic coordinates, geographical location information and the like in order to return all the specified position within a given radius of the center.

We usually like a taxi, rent maps and other functions can be used.

Since it is an ordered collection of geo mentioned earlier that it's how to score? Enter our own thing? In fact, through our store latitude and longitude of base32 encoded string after geohash calculated. geohash principle here.

The following is a commonly used method and StackExchangeRedis command:

(1) add location

//方法
GeoEntry[] geoEntry = new GeoEntry[] {
  new GeoEntry(120.20000, 30.26667, "hangzhou"),
  new GeoEntry(116.41667, 39.91667, "beijing"),
  new GeoEntry(121.47, 31.23, "shanghai"),
};
redisConnection.GetDatabase().GeoAdd("city", geoEntry);
//命令
127.0.0.1:6379> geoadd city 120.20000 30.26667 hangzhou  116.41667 39.91667 beijing 121.47 31.23 shanghai
(integer) 3

(2) obtaining geohash

//方法
var geohash = redisConnection.GetDatabase().GeoHash("city", "hanghzou");
//命令
127.0.0.1:6379> geohash city hangzhou
1) "wtmkpjyuph0"

(3) obtaining geographic location information of the specified range of elements set

//方法
var geo = redisConnection.GetDatabase().GeoRadius("city", "hanghzou", 300, GeoUnit.Kilometers, 10, Order.Ascending, GeoRadiusOptions.WithGeoHash);
//命令
127.0.0.1:6379> georadiusbymember city hangzhou 300 km withcoord withdist withhash asc count 10
1) 1) "hangzhou"
   2) "0.0000"
   3) (integer) 4054134205316048
   4) 1) "120.20000249147415"
      2) "30.266670658987586"
2) 1) "shanghai"
   2) "161.9183"
   3) (integer) 4054803462927619
   4) 1) "121.47000163793564"
      2) "31.229999039757836"

When given the option, the command returns additional information:

  • WITHDIST: while the return position of the element, the distance between the center position of the elements is also returned together. User distance units and given range consistent units.

  • WITHCOORD: The latitude and longitude position of the element together also return.

  • WITHHASH: 52 in the form of a signed integer, the position of the element through the return value of the original ordered set of encoded geohash.

  • ASC: The position of the center, according to the return from a position near to far way element. DESC: The position of the center, far away from the embodiment according to the return position near the element.

It also has a similar georadius command, the distinction is given the latitude and longitude as the center.

(4) calculates the distance between the two positions
//方法
var geo = redisConnection.GetDatabase().GeoDistance("city", "hanghzou","beijing");
//命令
127.0.0.1:6379> geodist city hangzhou beijing km
"1126.8937"

3. Publish Subscribe pub / sub 

"Publish / subscribe" model contains two roles, namely publishers and subscribers. Subscribers can subscribe to one or several channels (channel), and the publisher can send messages to a specified channel, all subscribe to this channel subscribers will receive this message.

Posted by sending a message through the publish command:

// command 
127.0 . 0.1 : 6379 > publish CHANNEL_1 hi
(integer) 0
//方法
var count = redisConnection.GetSubscriber().Publish("channel_1", "hi");

Message is sent out, publish command return value indicates the number of subscribers receiving this message. Because it is not currently subscribe to subscribe to this channel, it returns 0.

Note that the message sent out is not persistent, subscribers can only receive messages sent after the subscription publisher.

Subscribers subscribe to one or more channels through the subscribe command:

//命令
127.0.0.1:6379> subscribe channel_1 channel_2
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "channel_1"
3) (integer) 1
1) "subscribe"
2) "channel_2"
3) (integer) 2
1) "message"
2) "channel_1"
3) "hi"
//方法
while (true)
{
  redisConnection.GetSubscriber().Subscribe("channel_1", (channel, message) =>
  {
    var MSG = Message; // received message 
    var Chan = Channel; // channel name 
  });
}

After executing subscribe command to enter the subscription status, you may receive three types of response. Each type of reply contains three values, the first value is a type of message, depending on the message type, the second, worth three different meanings. Message type may have the following three values:

  • subscribe: subscribe to indicate success was the feedback information. The second value is the name of the subscription channel was successful, and the third is the current clients to subscribe to get the number of channels.

  • message: This was the type of response is our greatest concern was that represents the message was received. The second value represents a channel name Jackson message, the third value is the content of the message.

  • unsubscribe: unsubscribe from a channel for success. The second value is the name of the channel corresponding to the third value is the number of channels the current client subscriptions.

StackExchangeRedis used in both message and unsubscribe.

 In addition to the channels by name, you can also use the command psubscribe rule subscribe. Such as the rules of engagement for the channel _? * Can match channel_ beginning with channels such as channel_1, channel_2 and so on.

//命令
127.0.0.1:6379> psubscribe cahnnel_?*
Reading messages... (press Ctrl-C to quit)
(integer) 1
1) "psubscribe"
2) "cahnnel_?*"
3) (integer) 1
//方法
while (true)
{
  redisConnection.GetSubscriber().Subscribe("channel_?*", (channel, message) =>
  {
    var MSG = Message; // received message 
    var Chan = Channel; // channel name 
  });
}

Unsubscribe command unsubscribe, if you do not specify the channel to cancel all subscriptions. Method respectively when Articles Unsubscribe () and UnsubscribeAll () in the StackExchangeRedis.

Guess you like

Origin www.cnblogs.com/xwc1996/p/12013329.html