Basic Tutorials of Redis(3) -Hash

  When you first saw the name of Hash,what do you think?HashSet,HashTable or other data structs of C#?As for me,

the first time I saw the Hash,I considered is as the HashTable.Actually,Hash can identify with HashTable,the same as

DataRow.A row data of table can Regular as a Hash's data.The below picture may help you to card the point.

  

  
  There are 15 commands you can use in Redis,less than the Strings. 

  

  Before we use the Hash of Redis,we must hava some exists Hashes in the Redis's 

Hash to the database?And how can we get the Hash from the database.Command hset,hmset,hget,hmget can help us to

solve those two question.Now I use hset to add a key named user-1 with a filed named name,and the value of the filed is

catcher.And I use hget to get the value of name.
hset user-- name

  The hmset and hmget can handle multi k/v.
hmset user- age - name age gender

  When you want to learn how many fileds in this Hash,you can use the command hlen to get  

the Hash.And it will return a integer,meaning there are 3 fileds in the user-1.

hlen user-

  hget and hmget is a little complex when a hash has 100 fileds or much more.To solve this 

hgetall command,this command will return all of the fileds and the values of this key.

hgetall user-

  If there some fileds you don't need anymore,you can delete them by hdel command.For an 

gender filed from the user-1.

hdel user- gender

  Sometimes,we have to judge wheather a filed existses in the key.At this time we can use the hexists to finish the

job.As you can see,I judge wheather gender and name exists in the user-1.

hexists user-- name

  With the Requirement change,some places many only need the fileds of the hash,the other 

values of the hash.At this situation,some people may use hgetall to finish the requirements,but I don't suggest to do

more than the request.So I will use the hkeys to get all the fileds of the hash,and use the hvals to get all the values of

the hash.

hkeys user--

   How about increase a filed's value like the string do.As for me,both of the increased command and decreased command

are the same.Because of their regular usage.For example,I increase the age of the user-1 by 2,and you will get the result like

the below image.

hincr user- age

  After showing the native commands,we should turn to the usage of StackExchange.Redis.
db.HashSet(,, user_1 = HashEntry[] { HashEntry(,), HashEntry(, db.HashSet( Console.WriteLine(,db.HashGet(, user_1_fileds = RedisValue[] { ,, user_1_values = db.HashGet( ( item Console.WriteLine(.Format(,db.HashLength( all = db.HashGetAll( ( item Console.WriteLine(.Format( db.HashDelete(, all_after_del = db.HashGetAll( ( item Console.WriteLine(.Format( Console.WriteLine(.Format(,db.HashExists(,)?: Console.WriteLine(.Format(,) ? : keys = db.HashKeys( Console.WriteLine( ( item values = db.HashValues( Console.WriteLine( ( item Console.WriteLine(.Format(,db.HashIncrement(,)));
  When you debug the codes,the results are as follow.

  The next post of this series is the basic opreation of Set in Redis.

相关文章

在笔者近 3 年的 Java 一线开发经历中,尤其是一些移动端、用...
这一篇文章拖了有点久,虽然在项目中使用分布式锁的频率比较...
本文梳理总结了一些 Java 互联网项目中常见的 Redis 缓存应用...
书接上回,消息通知系统(notification-system)作为一个独立...
Redis 是目前互联网后端的热门中间件之一,在许多方面都有深...
在Java Spring 项目中,数据与远程数据库的频繁交互对服务器...