Five types of data commonly used in Redis
String (Key-value)
String is the most commonly used type of data, and ordinary key/value storage can be classified as such.
A key corresponding to a value,string type is binary safe.
A Redis string can contain any data, such as a JPG image (generated binary) or a serialized object.
The basic operations are as follows:
Basic data type
var client = new Redisclient ("127.0.0.1", 6379);
Client. Set<int> ("pwd", 1111);
int pwd=client. Get<int> ("pwd");
Console.WriteLine (PWD);
Object
UserInfo UserInfo = new UserInfo () {UserName = "Zhangsan", userpwd = "1111"};//the bottom layer uses JSON to serialize the
client. Set<userinfo> ("UserInfo", UserInfo);
UserInfo user=client. Get<userinfo> ("UserInfo");
Console.WriteLine (user. UserName);
Object collection
list<userinfo> List = new list<userinfo> () {new UserInfo () {username= "Lisi", userpwd= "111"}, New UserInfo () {username= "Wangwu", userpwd= "123"}};
Client. Set<list<userinfo>> ("list", list);
List<userinfo>userinfolist=client. Get<list<userinfo>> ("List");
foreach (UserInfo UserInfo in userinfolist)
{
Console.WriteLine (userinfo.username);
}
Hash (Key-value)
The hash is a string-type field and value mapping table.
Hashing is especially good for storing objects. Relative to Gencun each word of an object to a single string type. An object that is stored in a hash type consumes less memory and makes it easier to access the entire object.
The value of the hash data type of Redis is a hashmap, and if the map has fewer members, a one-dimensional array is used to store the map in a compact manner, eliminating the memory overhead of a large number of pointers, which are listed in the redis.conf configuration file in the following 2 items.
Hash-max-zipmap-entries 64
Hash-max-zipmap-value 512.
The implication is that when the value of this map does not exceed the number of members will be used in a linear compact format storage, the default is 64, that is, the value of 64 of the following members is the use of linear compact storage, more than this value automatically turned into real hashmap.
The hash-max-zipmap-value implication is that when value is within the map, the length of each member value is no more than the number of bytes, the linear compact storage is used to save space. Above two conditions any one condition exceeds the setting value will turn into the real hashmap, also will no longer save the memory, this value set how many needs to weigh, the HashMap advantage is the search and the operation time is short.
Adopt a Key-field-value approach. A key can correspond to more than one field, and a field corresponds to a value. At the same time, note that Redis provides an interface (Hgetall) to directly fetch all of the attribute data, but if there are many members of the internal map, then it involves traversing the entire internal map, which may be time-consuming due to the Redis single-threaded model, To make requests from other clients completely unresponsive, this requires extra attention
It is recommended that you use object classes and IDs to make up key names, use fields to represent object properties, and field values to store property values.
Contrast
1. The use of a string type of storage object requires serialization of the object.
The cost of serialization/deserialization is increased, and the entire object needs to be retrieved when one of the information needs to be modified. 2. Using a hash data type is not required
Key is still the user ID, value is a map, the map key is the member's property name, value is the attribute value, so that the modification and access to the data can be directly through its internal map key (Redis called the internal Map key field), that is, through The key (User ID) + field (attribute tag) can manipulate the corresponding attribute data, neither storing data repeatedly nor bringing serialization and deserialization.
Setentryinhash Key-field-value this way
client. Setentryinhash ("UserId", "UserName", "Changliang");
List<string> list = client. Gethashkeys ("UserId");
List<string> list = client. Gethashvalues ("UserName");/Get value
list<string> List = client. Getallkeys ()//Get all key.
List
List is a list structure, the main function is push, pop, get a range of all the values and so on. The key in the operation is understood as the list name. The list type of Redis is actually a two-way list of string types for each child element. We can add deletion elements from the head or tail of the linked list by Push,pop operation, so that the list can be used as a stack and as a queue (the stack is Insertfirst+deletefirst, and the queue is Insertlast+deletefirst). Can support reverse lookup and traversal, easy to operate, but brings some extra memory overhead. Many implementations within the Redis, including sending buffer queues, are also used in this data structure.
Queue use-Join
client. Enqueueitemonlist ("name", "Zhangsan");
Client. Enqueueitemonlist ("name", "Lisi");
int count= client. GetListCount ("name");
for (int i = 0; i < count; i++)
{
Console.WriteLine (client. Dequeueitemfromlist ("name"));
}
Stack use-Press stack
client. Pushitemtolist ("Name2", "Wangwu");
Client. Pushitemtolist ("Name2", "Maliu");
int count = client. GetListCount ("name2");
for (int i = 0; i < count; i++)
{
Console.WriteLine (client. Popitemfromlist ("name2"));
}
Set
It is a string-type unordered collection. Set is implemented through hash table, and can be added, deleted, and found. We can take the set, the intersection, the difference set.
Collections can be intersected, and set like Sina Weibo to get two users of common concern
Sina Weibo uses Redis for two different scenarios:
1. The data is stored directly in the Redis, which is independent
2. Some data contains a logical relationship between tables and tables, which are often accessed more frequently, and the performance of queries directly from the database is relatively poor. Write the data to Redis, then save the data to the MySQL database
The set type operation set name can be followed by a key-value pair, or a base type client.
Additemtoset ("A3", "ddd"); Client.
Additemtoset ("A3", "CCC"); Client.
Additemtoset ("A3", "tttt"); Client.
Additemtoset ("A3", "Sssh"); Client.
Additemtoset ("A3", "HHHH"); System.collections.generic.hashset<string>hashset=client.
Getallitemsfromset ("A3");
foreach (String str in hashset) {Console.WriteLine (str);} Collection//Collection 1 A3 set client.
Additemtoset ("A3", "ddd"); Client.
Additemtoset ("A3", "CCC"); Client.
Additemtoset ("A3", "tttt"); Client.
Additemtoset ("A3", "Sssh"); Client.
Additemtoset ("A3", "HHHH"); Set 2 A4 Set client.
Additemtoset ("A4", "hhhh"); Client.
Additemtoset ("A4", "h777"); system.collections.generic.hashset<string>hashset= client.
Getunionfromsets (new string[] {"A3", "A4"});
foreach (String str in hashset) {Console.WriteLine (str);} Seek intersection system.collections.generic.hashset<string> HashSet = client.
Getintersectfromsets (new string[] {"A3", "A4"});
To find the difference set. Returns data that exists in the first collection but does not exist in other collections. Difference Set System.Collections.Generic.HaShset<string> hashset = client.
Getdifferencesfromset ("A3", new string[] {"A4"});
Zset
The use scenario for the Redis sorted set is similar to set, except that the set is not automatically ordered, and sorted set can be sorted by the user providing an additional priority (score) parameter, and is inserted in an orderly, automatic sort.
Can be understood as a list of stored value, a list of stored orders. The key in the operation is understood as Zset's name.
When you need an ordered and not duplicated list of collections, you can choose sorted Set data structure
Saves the order of the inserts and puts them in the collection A5
client. Additemtosortedset ("A5", "ffff");
Client. Additemtosortedset ("A5", "bbbb");
Client. Additemtosortedset ("A5", "Gggg");
Client. Additemtosortedset ("A5", "CCCC");
Client. Additemtosortedset ("A5", "waaa");
System.collections.generic.list<string>list =client. Getallitemsfromsortedset ("A5");
foreach (string str in list)
{
Console.WriteLine (str);
}
Summary
Data type |
Key |
Value |
String |
The normal key |
The normal value |
Hash |
Normal key such as user ID |
HashMap (Field,value) Represents the object |
List |
List name |
Each child element is a string type doubly linked list |
Set |
Set name |
Unordered collection of string types |
Zset |
Zset Name |
A list of stored value, a list of stored orders |