Redis Learning Notes Hash type and example of C # invocation

Source: Internet
Author: User
Tags foreach hash redis

1,


Hset key field value

Sets the value of the field field in the hash table key to values.

Key does not exist, create.

field does not exist, create. already exists, covering it.

Example:

Hset hashkey1 field1 "value1"//Create
Hset hashkey1 field1 "value1-new"/cover
Hset hashkey1 field2 "value2"//Create

C#:

Method:

Public long Hset (string Hashid, byte[] key, byte[] value);

Implementation: Code in the Hset_hget_hgetall_hdel_hexists_hlen () of the demo

using (redisclient redisclient = new Redisclient ("127.0.0.1", 6379, NULL, 2)//db=2 namely: Select 2


{


string key = "keyhashhset_c#";


if (Redisclient.containskey (key)) Redisclient.del (key); Remove this key first.


Redisclient.hset (Key, Encoding.UTF8.GetBytes ("field1_c#"), Encoding.UTF8.GetBytes ("value1_c#"));


Redisclient.hset (Key, Encoding.UTF8.GetBytes ("field2_c#"), Encoding.UTF8.GetBytes ("value2_c#"));


/*


Hgetall keyhashhset_c#


* field1_c# Domain


* value1_c# Value


* field2_c#


* value2_c#


*/


}


Hget key Field

Returns the value of the given field field in the hash table key.

Example:

Hset hashkey2 field1 "Test Hget"//Create a key
Hget Hashkey2 field1//back to "Test Hget"

C#:

Method:

Public byte[] Hget (String Hashid, byte[] key);


Implementation: Code in the Hset_hget_hgetall_hdel_hexists_hlen () of the demo




11


using (redisclient redisclient = new Redisclient ("127.0.0.1", 6379, NULL, 2)//db=2 namely: Select 2


{


Key = "keyhashhget_c#";


if (Redisclient.containskey (key)) Redisclient.del (key); Remove this key first.


Redisclient.hset (Key, Encoding.UTF8.GetBytes ("field1_c#"), Encoding.UTF8.GetBytes ("Test hget China"); Create a Key-field


byte[] Hgetstr = Redisclient.hget (Key, Encoding.UTF8.GetBytes ("field1_c#")); Get to Value


String returnstr = Encoding.UTF8.GetString (HGETSTR); Into strings


/*


Test Hget Zhonghua


Hget End *


}


3,

Hgetall Key

Returns the value of all fields in the hash table key.

Example:


Hset Hashkey3 field1 values1
Hset Hashkey3 Field2 Values2
Hset Hashkey3 field3 Values3
Hset Hashkey3 field4 values4//Create key
1
Hgetall Hashkey3//Returns the value of the field below Key=hashkey3.

C#:

Method:

Public byte[][] Hgetall (string Hashid);
Realize:

The code is in the Hset_hget_hgetall_hdel_hexists_hlen () of the demo

using (redisclient redisclient = new Redisclient ("127.0.0.1", 6379, NULL, 2)//db=2 namely: Select 2


{


Key = "keyhashhgetall_c#";


if (Redisclient.containskey (key)) Redisclient.del (key); Remove this key first.


Redisclient.hset (Key, Encoding.UTF8.GetBytes ("field1_c#"), Encoding.UTF8.GetBytes ("Test hgetall Zhonghua 1"); Create a Key-field


Redisclient.hset (Key, Encoding.UTF8.GetBytes ("field2_c#"), Encoding.UTF8.GetBytes ("Test Hgetall Zhonghua 2"); Create a Key-field


Redisclient.hset (Key, Encoding.UTF8.GetBytes ("field3_c#"), Encoding.UTF8.GetBytes ("Test Hgetall Zhonghua 3"); Create a Key-field


Redisclient.hset (Key, Encoding.UTF8.GetBytes ("field4_c#"), Encoding.UTF8.GetBytes ("Test Hgetall Zhonghua 4"); Create a Key-field


Byte[][] arrreturn= Redisclient.hgetall (key); The results obtained


list<string> arr = new list<string> ();


foreach (Var a in Arrreturn)


{


Arr. ADD (Encoding.UTF8.GetString (a));


}


/*


* field1_c#


* Test Hgetall Zhonghua 1


* field2_c#


* Test Hgetall Zhonghua 2


* field3_c#


* Test Hgetall Zhonghua 3


* field4_c#


* Test Hgetall Zhonghua 4


Hgetall End *


}


4,

Hdel key field [field ...]

Deletes one or more of the specified fields in the hash table key, and the nonexistent domain is ignored.


Example:

Hset Hashkey4 field1 values1
Hset Hashkey4 Field2 Values2
Hset Hashkey4 field3 Values3
Hset Hashkey4 Field4 Values4
Hset Hashkey4 field5 values5
Hset hashkey4 field6 values6//Create key
Hgetall Hashkey4//Returns the value of the field below Key=hashkey4.
Hdel Hashkey4 field2//delete single domain
Hdel hashkey4 field4 field5//Delete multiple domains
Hgetall Hashkey4//field1=values1 Field3=values3 FIELD6=VALUES6


As shown in figure: 4-1-4

It share 4-1-4.png

C#:

Method:

Public long Hdel (string Hashid, byte[] key); Delete a single domain
Public long Hdel (string Hashid, byte[][] keys);//Delete multiple domains
Realize:

The code is in the Hset_hget_hgetall_hdel_hexists_hlen () of the demo




using (redisclient redisclient = new Redisclient ("127.0.0.1", 6379, NULL, 2)//db=2 namely: Select 2


{


Key = "keyhashhdel_c#";


if (Redisclient.containskey (key)) Redisclient.del (key); Remove this key first.


Redisclient.hset (Key, Encoding.UTF8.GetBytes ("field1_c#"), Encoding.UTF8.GetBytes ("Test hgetall Zhonghua 1"); Create a Key-field


Redisclient.hset (Key, Encoding.UTF8.GetBytes ("field2_c#"), Encoding.UTF8.GetBytes ("Test Hgetall Zhonghua 2"); Create a Key-field


Redisclient.hset (Key, Encoding.UTF8.GetBytes ("field3_c#"), Encoding.UTF8.GetBytes ("Test Hgetall Zhonghua 3"); Create a Key-field


Redisclient.hset (Key, Encoding.UTF8.GetBytes ("field4_c#"), Encoding.UTF8.GetBytes ("Test Hgetall Zhonghua 4"); Create a Key-field


Redisclient.hset (Key, Encoding.UTF8.GetBytes ("field5_c#"), Encoding.UTF8.GetBytes ("Test Hgetall Zhonghua 5"); Create a Key-field


Redisclient.hset (Key, Encoding.UTF8.GetBytes ("field6_c#"), Encoding.UTF8.GetBytes ("Test Hgetall Zhonghua 6"); Create a Key-field


Redisclient.hdel (Key, Encoding.UTF8.GetBytes ("field2_c#")); Delete a domain


Redisclient.hdel (Key, new byte[][] {Encoding.UTF8.GetBytes ("field4_c#"), Encoding.UTF8.GetBytes ("field5_c#")}); Delete multiple domains


/*


* field1_c#


* Test Hgetall Zhonghua 1


* field3_c#


* Test Hgetall Zhonghua 3


* field6_c#


* Test Hgetall Zhonghua 6


Hdel End *


}


5,

Hexists key Field
Whether the field field exists in the key. The existence returns 1, does not exist returns 0.

Example:

Hexists hashkey5 field1//Return 0
Hset hashkey5 field1 "values1"//Create Key-field
Hexists hashkey5 field1//return 1

C#:

Method:

Public long hexists (string Hashid, byte[] key);
Realize:

The code is in the Hset_hget_hgetall_hdel_hexists_hlen () of the demo


using (redisclient redisclient = new Redisclient ("127.0.0.1", 6379, NULL, 2)//db=2 namely: Select 2
{
Key = "keyhashhexists_c#";
if (Redisclient.containskey (key)) Redisclient.del (key); Remove this key first.
var v = redisclient.hexists (key, Encoding.UTF8.GetBytes ("field1_c#")); 0-No query to results
var v2 = Redisclient.hset (key, Encoding.UTF8.GetBytes ("field1_c#"), Encoding.UTF8.GetBytes ("values1_c#")); Create a Key-field
var v3 = redisclient.hexists (Key, Encoding.UTF8.GetBytes ("field1_c#")); 1-Query to a result
}

6,


Hlen Key

The number of fields in the key.

Example:

Hset Keyhashhlen field1 values1
Hset Keyhashhlen Field2 Values2
Hset Keyhashhlen field3 Values3
Hlen Keyhashhlen//3

C#:

Method:

Public long Hlen (string Hashid);
Realize:

The code is in the Hset_hget_hgetall_hdel_hexists_hlen () of the demo




using (redisclient redisclient = new Redisclient ("127.0.0.1", 6379, NULL, 2)//db=2 namely: Select 2


{


Key = "keyhashhlen_c#";


if (Redisclient.containskey (key)) Redisclient.del (key); Remove this key first.


Redisclient.hset (Key, Encoding.UTF8.GetBytes ("field1_c#"), Encoding.UTF8.GetBytes ("values1")); Create a Key-field


Redisclient.hset (Key, Encoding.UTF8.GetBytes ("field2_c#"), Encoding.UTF8.GetBytes ("Values2")); Create a Key-field


Redisclient.hset (Key, Encoding.UTF8.GetBytes ("field3_c#"), Encoding.UTF8.GetBytes ("Values3")); Create a Key-field


var length = Redisclient.hlen (key); 3 A


}


7,

Hincrby key field Increment
Adds an incremental increment (or negative number) to the value of the field field in the hash table key
If the key does not exist, create it and then increment it.
If the field does not exist, assign a value of 0 and increment.

Example:

Hincrby hashkey7 field1 100//100


C#:

Method:

Public long Hincrby (string Hashid, byte[] key, int incrementby);

Public long Hincrby (string Hashid, byte[] key, long IncrementBy);
Realize:

The code is in the Hincrby_hincrbyfloat_hkeys_hmget_hmset_hsetnx_hvals () method of the demo
   
using (redisclient Redisclient = new Redisclient ("127.0.0.1", 6379, NULL, 2))
        {
 & nbsp;              string key = "Keyhashhincrby_C #";
                if ( Redisclient.containskey (Key)) Redisclient.del (key); First delete this key
                Redisclient.hincrby (Key, Encoding.UTF8.GetBytes ("field1_c#"), 100);
                var ss = Encoding.UTF8.GetString (Redisclient.hget) (Key, Encoding.UTF8.GetBytes ("field1_c#"));
       }
   
8,

Hincrbyfloat key field Increment

Adds a floating-point increment increment to the field field in the hash table key, along with the Hincrby command.

9,

Hkeys Key

Returns all the domain names in the hash table key.
Example:

Hset hashkey9 field1 "Values1"
Hset hashkey9 field2 "Values2"
Hkeys Hashkey9//field1 Field2
C#:
Method:

Public byte[][] Hkeys (string Hashid);

Realize:

Code in the Hincrby_hincrbyfloat_hkeys_hmget_hmset_hsetnx_hvals () method of the demo

using (redisclient redisclient = new Redisclient ("127.0.0.1", 6379, NULL, 2))


{


Key = "keyhashhkeys_c#";


if (Redisclient.containskey (key)) Redisclient.del (key); Remove this key first.


Redisclient.hset (Key, Encoding.UTF8.GetBytes ("field1_c#"), Encoding.UTF8.GetBytes ("values1"));


Redisclient.hset (Key, Encoding.UTF8.GetBytes ("field2_c#"), Encoding.UTF8.GetBytes ("Values2"));


Redisclient.hset (Key, Encoding.UTF8.GetBytes ("field3_c#"), Encoding.UTF8.GetBytes ("Values3"));


Redisclient.hset (Key, Encoding.UTF8.GetBytes ("field4_c#"), Encoding.UTF8.GetBytes ("Values4"));


byte[][] Arrreturn = Redisclient.hkeys (key); Get


list<string> arr = new list<string> ();


foreach (Var a in Arrreturn)


{


Arr. ADD (Encoding.UTF8.GetString (a));


}


/* Results:


* field1_c#


* field2_c#


* field3_c#


* field4_c#


Hkeys End *


}


10,

Hmget key field [field ...]

Returns the value of one or more given fields in a hash table key

Hmset key field value [field value ...]
Set multiple Field-value (field-value) pairs to the hash table key at the same time.
This command overwrites a domain that already exists in the hash table.
If key does not exist, an empty hash table is created and performs a hmset operation.

Example:

Hmset hashkey10 Dog "Doudou" cat "Nounou"//Set multiple field values at a time
Hmget hashkey10 dog cat Nofield//doudou Nounou (nil)

C#:
Method:

public void Hmset (string Hashid, byte[][] keys, byte[][] values);
Public byte[][] Hmget (string Hashid, params byte[][] keys);

Realize:
Code in the Hincrby_hincrbyfloat_hkeys_hmget_hmset_hsetnx_hvals () method of the demo

using (redisclient redisclient = new Redisclient ("127.0.0.1", 6379, NULL, 2))


{


Key = "keyhashhmget_c#";


if (Redisclient.containskey (key)) Redisclient.del (key); Remove this key first.


var arr_fields = new byte[][] {


Encoding.UTF8.GetBytes ("field1_c#"),


Encoding.UTF8.GetBytes ("field2_c#"),


Encoding.UTF8.GetBytes ("field3_c#"),


Encoding.UTF8.GetBytes ("field4_c#"),


Encoding.UTF8.GetBytes ("field5_c#")


};


var arr_values = new byte[][] {


Encoding.UTF8.GetBytes ("Values1"),


Encoding.UTF8.GetBytes ("Values2"),


Encoding.UTF8.GetBytes ("Values3"),


Encoding.UTF8.GetBytes ("Values4"),


Encoding.UTF8.GetBytes ("Values5")


};


Redisclient.hmset (Key, Arr_fields, arr_values); Hmset operation, setting multiple fields


var arr_getfields = new byte[][] {


Encoding.UTF8.GetBytes ("field2_c#"),


Encoding.UTF8.GetBytes ("field3_c#")


};


byte[][] Arrreturnfromhmget = Redisclient.hmget (key,arr_getfields); Hmget Get multiple fields


list<string> arrfromhmget = new list<string> ();


foreach (Var a in arrreturnfromhmget)


{


Arrfromhmget.add (Encoding.UTF8.GetString (a));


}


/* Results:


* Values2


* Values2


Hmset Hmget End of the * *


}





11,

Hsetnx key field value
Sets the value of the field field in the hash table key to value if and only if the field field does not exist.
If the field field already exists, the operation is invalid.
Example:

Hsetnx hashkey11 field1 values1//Create Key-field, and assign values.
Hsetnx hashkey11 field1 valuesedit//already exists Key-field, the operation is invalid.

C#:
Method:

Public long Hsetnx (string Hashid, byte[] key, byte[] value);
Realize:

Code in the Hincrby_hincrbyfloat_hkeys_hmget_hmset_hsetnx_hvals () method of the demo

using (redisclient redisclient = new Redisclient ("127.0.0.1", 6379, NULL, 2))


{


Key = "keyhashhsetnx_c#";


if (Redisclient.containskey (key)) Redisclient.del (key); Remove this key first.


Redisclient.hsetnx (Key, Encoding.UTF8.GetBytes ("Notexistfields"), Encoding.UTF8.GetBytes ("msetnx values")); No domain notexistfields, create and assign values


Redisclient.hsetnx (Key, Encoding.UTF8.GetBytes ("Notexistfields"), Encoding.UTF8.GetBytes ("Msetnx values2")); Not executed. Because the domain already exists and does not operate.


var returnstr = Encoding.UTF8.GetString (Redisclient.hget (Key, Encoding.UTF8.GetBytes ("Notexistfields")); MSETNX values


/* Results:


* MSETNX Values


Hsetnx End *


}


12,

Hvals keys
Returns the value of all fields in the hash table key.

Example:


Hmset hashkey12 field1 values1 field2 values2
Hvals HASHKEY12//Result: Field1 field2


C#:
Method:

Public long Hsetnx (string Hashid, byte[] key, byte[] value);
Realize:

Code in the Hincrby_hincrbyfloat_hkeys_hmget_hmset_hsetnx_hvals () method of the demo

using (redisclient redisclient = new Redisclient ("127.0.0.1", 6379, NULL, 2))


{


Key = "keyhashhvals_c#";


if (Redisclient.containskey (key)) Redisclient.del (key); Remove this key first.


var arr_fields_hvals = new byte[][] {


Encoding.UTF8.GetBytes ("field1_c#"),


Encoding.UTF8.GetBytes ("field2_c#")


};


var arr_values_hvals = new byte[][] {


Encoding.UTF8.GetBytes ("Values1"),


Encoding.UTF8.GetBytes ("Values2")


};


Redisclient.hmset (Key, Arr_fields_hvals, arr_values_hvals); Hmset operation, setting multiple fields


byte[][] arrreturnfromhvals = redisclient.hvals (key); Hmget Get multiple fields


list<string> arrfromhvals = new list<string> ();


foreach (Var a in arrreturnfromhvals)


{


Arrfromhvals.add (Encoding.UTF8.GetString (a));


}


/* Results:


* Values1


* Values2


Hvals End *


}

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.