Today we learn about redis Hash, What is it, Command of hash, Redis hashes is map or dictionaries data structure. Each hash in Redis is associated with a key, and the key can be used to access the value, add value, modify value or remove fields.

in short the Hashes in redis:

  • Key with named fields
  • Single level
  • Provide command on those fields e.g INCR
  • Dynamically add or remove fields
  • Store extremely efficiently
  • Are not recursive

Commands In Hash Redis

common command hash redis

Commands

  • HSET
    • Command to set a map or add value and modify value
    • HSET keyname field_name value field_name2 value
    • HSET employee:krisna full_name "krisna putra" city Denpasar
    • HSET employee:krisna full_name "Anak Agung Krisna Putra" //(Modify)
    • HSET employee:krisna age 17
    • HGETALL employee:krisna
    • HSET
  • HEXISTS
    • Check Fields Exists in a Hash
    • return 1 if field exists in the hash stored at key.
    • HEXISTS keyname field_name_check
    • HEXISTS employee:krisna full_name
    • HEXISTS REDIS
  • HGET and HGETALL
    • HGET Command returns the value assosiated with the fields in hash stored at key. Nil if the field is not exists.
    • HGETALL Command return all fieds and value in hash. if key does not exist will return empty list or set
    • HGET keyname field_name
    • HGETALL keyname
    • HGET and HGETALL
  • HDEL
    • Command to Remove fields the hash stored at key. if field name does not exist will ignore and return 0
    • HDEL keyname field [field ...]
    • HDEL employee:krisna age
    • HDEL REDIS
  • HINCRBY
    • command for increment value of field hash stored at key.
    • if key does not exists, automatically key will be created.
    • if field does not exists, automatically will created at increments by
    • if increment_value is negative value will be decrement instead of increment.
    • limit value is 64 bit signed integers.
    • HINCRBY keyname field_name value
    • HINCRBY REDIS
  • HINCRBYFLOAT
    • Similar with command HINCRBY.
    • HINCRBYFLOAT keyname field_name value_incrementor
    • HINCRBYFLOAT REDIS
  • HKEYS
    • This command will return all field on key.
    • If key does not exists will return (empty list or set)
    • HKEYS keyname
    • HKEYS REDIS
  • HLEN
    • This Command will return how many fields key
    • If Key does not Exists will return 0
    • HLEN keyname
    • HLEN Redis
  • HMGET
    • this command for get values for many fields in the hash stored in key.
    • if field name does not exist will return null
    • if keyname does not exists will return null of every field_name
    • with this command we can get many value with single command. “interesting (^^)!!”
    • HMGET keyname field_name [field_name ...]
    • HMGET Redis
  • HMSET
    • Similar of command HSET
    • Deprecated at Redis 4.0.0
    • Skipp learning
  • HRANFIELD
    • Return count random field from Hash stored at key .
    • If the provided count argument is positive, return an array of distinct fields. The array’s length is either count or the hash’s number of fields, whichever is lower.
    • If called with a negative count, the behavior changes and the command is allowed to return the same field multiple times. In this case, the number of returned fields is the absolute value of the specified count.
    • Honestly today, I’m don’t know use case for HRANDFIELD
    • HRANFIELD keyname [Count count]
    • example
    • HSET employee:krisna full_name "Anak Agung Krisna Putra" age 38 id 2121 marital true
    • HRANDFIELD employee:krisna 2
    • HRANDFIELD employee:krisna -2
    • HRANDFIELD REDIS
  • HSCAN
    • This Command HASH for looping fields of Hash type and their assosiated value.
    • HSCAN keyname cursor [MATCH pattern] [COUNT count]
      • keyname : Spesific the key of hash to scan
      • cursor : The Curson used for iteration. Initially, this should be set to 0 to start the scan
      • MATCH pattern : [Optional] Specifies a pattern/criteria than the fields name must match to be included in the scan. this uses glob-style pattern matching.
      • COUNT count : [Optional] Specifies the number of elements to return in a single iteration. This is a hint to Redis about how many elements you wan to retrieve at once.
    • HSCAN employee:krisna 0 MATCH full* COUNT 1
    • HSCAN REDIS
  • HSETNX
    • This Command to set Fields and value with conditions not exists. if exists this command will skipp and the value not changed.
    • HSETNX -> “Hash SET if Not Exists
    • HSETNX keyname field value
      • HSETNX employee:krisna city denpasar
      • HSETNX employee:krisna city bali
      • HGET employee:krisna city
      • Result : "denpasar"
    • HSETNX
  • HSTRLEN
    • This Command Return String length of the value associated with field in the hash stored at key. If The key or the field not exist, 0 is returned.
    • You can use HSTRLEN to determine the length of string values stored in a hash, which can be useful for various operations and validations in your Redis data structures.
    • HSTRLEN keyname field_name
      • HSTRLEN employee:krisna full_name
    • HSTRLEN Redis
  • HVALS
    • The HVALS command in Redis is used to retrieve all the values stored in a hash data structure. It returns an array containing all the values associated with the fields in the hash.
    • HVALS keyname
      • HVALS employee:krisna
    • HVALS Redis
    • You can use HVALS to fetch all the values from a hash, which can be useful for operations where you need to work with all the values stored in a hash collectively.

Redis hashes are versatile and widely used for various use cases, such as storing user profiles, configuration settings, and more. They provide efficient data organization and retrieval capabilities within Redis.

Reference

Happy Learning

Tagged with: