Redis Hash

Redis Hash

A Redis Hash is a data structure in Redis that stores a set of key-value pairs, similar to an object or map in programming languages. It is ideal for representing objects with multiple attributes, such as a user with a name, age, and email.

Each field in a hash can be accessed or modified individually, making it efficient in terms of memory and performance. With commands like HSET, HGET, and HGETALL, data can be managed quickly and easily.

Hashes are widely used to store structured data in applications.

HSET

With the HSET command, we can define a hash as follows:

hset myMedia title "Inception"        
(integer) 1        

After the HSET command, we must provide the parameters key, field, and value.

In the example above, we set the value Inception to the field title, which belongs to the hash myMedia.

In the example above, we defined just one key/value pair, but we could define more than one, for example:

hset myMedia title "Inception" is_popular true        
(integer) 1        

HGET

We use the hget command to retrieve the value of a field within a hash.

hget myMedia title        
"Inception"        

HGETALL

We can retrieve the value of all fields in a hash with the hgetall command.

hgetall myMedia        
1) "title"
2) "Inception"
3) "is_popular"
4) "true"        

HDEL

We can remove a field within a hash with the hdel command.

hdel myMedia is_popular        
(integer) 1        

We remove the field is_popular.

hgetall myMedia        
1) "title"
2) "Inception"        

HLEN

We can use the hlen command to retrieve the number of fields in a hash.

hlen myMedia        
(integer) 1        

Adding another field:

hset myMedia age_rating 13        
(integer) 1        

Retrieve the number of fields within the hash again.

hlen myMedia        
(integer) 2        


要查看或添加评论,请登录

Daniel M.的更多文章

  • Golang HTTP Requests - Complete Guide

    Golang HTTP Requests - Complete Guide

    HTTP Requests In Go, we can use the net/http package to make http requests. We will use this public endpoint to make a…

  • Using structs with Redis and Golang

    Using structs with Redis and Golang

    Many times, we will need to store a struct in Redis instead of storing only separate fields. We can store a struct like…

  • Go Nested Templates

    Go Nested Templates

    We can nest templates in Go, allowing us to create smaller parts of a template, similar to components. In this example,…

  • Go Templates 02 - Conditionals

    Go Templates 02 - Conditionals

    Conditional Templates Another very common feature we need when working with templates is the use of conditions. For all…

  • Go Templates 01 - Basics

    Go Templates 01 - Basics

    Templates Go provides a powerful template system through the text/template and html/template packages, enabling the…

  • Go - sorting slices

    Go - sorting slices

    In Go, sorting a slice is often necessary when working with data that needs to be organized or structured for better…

  • Go - Arrays

    Go - Arrays

    Arrays Array is a data structure used to store a sequence of elements of same type in a fixed-size. An array is stored…

  • Redis SET command.

    Redis SET command.

    Redis set command SET We can set a string value to a key using the set command. In order to check if the value was…

  • Golang - Recursion

    Golang - Recursion

    Recursion The programming language Golang is known for its simplicity, efficiency and for work really well with…

社区洞察

其他会员也浏览了