Home > Web Front-end > JS Tutorial > body text

Redis 2

WBOY
Release: 2024-08-14 17:28:31
Original
377 people have browsed it

Redis 2

Redis - Lists, Sets and HashMaps

This is a continuation to the previous blogpost

There are 3 other data types available apart from key-value pairs

Lists

This data structure resembles the list in Python or the array in Javascript or C#. They can be used to keep recently used items. The common operations are;

Operations Explanations Examples Further Explanation
LPUSH Adds an Item to the beginning of the list LPUSH friends "Sophia" similar to Array.unshift in javascript
LRANGE Get all the items in a list LRANGE friends 0 -1 similiarly a list in python 0 is the first item and -1 is the last item
RPUSH Adds an item to the end of the list RPUSH friends "Poe" similar to Array.push in javascript
LPOP Removes an item from the start of the list LPOP friends "Poe" Will return 1 if Poe exists in the list and 0 otherwise
RPOP Removes an item from the end of the list RPOP friends "Sophia" Will return 1 if Sophia exists in the list and 0 otherwise

Sets

A data structure that only has unique items. similar to sets in Python and Sets in Javascript and HashSets in C#. Common operations include;

Operations Explanations Examples Further Explanation
SADD Adds a value to the set SADD colors "pink"
SMEMBERS returns the members of the set SMEMBERS colors will return all the items in the set colors
SREM Removes members of the set SREM colors "pink" Will return 1 if pink exists in the list and 0 otherwise

Hashmaps

An hashmap is a group of key value pairs. An hashmap however cannot be nested. Lets take a case scenario of a person with a name, email and phone number

HSET person name "Joe" # Adds the key-value pair {name : joe} to the hashmap
HSET person email "Joe@joe.com" # Adds the key-value pair {email : Joe@joe.com} to the hashmap
HSET person phone_number "+2345656655413" # Adds the key-value pair {number : ....} to the hashmap
Copy after login

the HGET command can be used to get the value of a particular key in a hashmap

HGET person name # returns "Joe"
Copy after login

the HGETALL command all the key-value pair in the hashmap

HGETALL person 

1) "name"
2) "Joe"
3) "email"
4) "Joe@joe.com"
5) "phone_number"
6) "+2345656655413"
Copy after login

the HDEL command deletes a key-value pair by its key

HDEL person name # removes {name : joe}
Copy after login

the HEXISTS command checks if a key exists in a hashset

HEXISTS person name # returns 0 because we've deleted it before
Copy after login

Those are most of the basic commands a developer is required to know.


Common Interview Questions

  1. What is the full meaning of Redis?: Redis stands for REmote DIctionary Server

  2. How does redis differ from traditional databases like MySQL?: Redis operates in main memory and have basic operations for accessing values quickly unlike SQL which resides on disk and has a wide range of crud operations

  3. What are the limitations of Redis regarding data size and type?: Redis is designed to hold values that fits into the machines memory. It is not suitable for complex relational models with joins or for structuring large blobs

  4. How do you handle caching in a distributed environment using Redis?: Handle caching by setting up redis instances as a caching layer in front of the database. using consistent hashing to distribute keys accreoss the cache nodes ensueres even load distribution and reduces cache misses

  5. When should you use List data types in Redis?: Lists are great for durable atomic queues, for job queue, logs, buffers and many other use cases

The above is the detailed content of Redis 2. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!