EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Redis Tutorial Redis LRANGE
Secondary Sidebar
Redis Tutorial
  • Redis
    • Redis Pubsub
    • Redis GUI
    • Redis Database
    • Redis Get All Keys
    • Redis TTL
    • Redis EXPIRE
    • Redis Version
    • Redis EXPIRE
    • Redis List
    • Redis CLI Commands
    • Redis HSET
    • Redis FLUSHDB
    • Redis Key
    • Redis Persistence
    • Redis FLUSHALL
    • Redis pipeline
    • Redis SADD
    • Redis ZADD
    • Redis ZSET
    • Redis Get
    • Redis Replication
    • Redis Delete All Keys
    • Redis Use Cases
    • Redis HMSET
    • Redis Transactions
    • Redis Monitor
    • Redis HGET
    • Redis SETEX
    • Redis Rails
    • RedisJSON
    • Redis Get Key
    • Redis Auth
    • Redis gem
    • Redis Key Count
    • RedisTimeSeries
    • Redis Timeout
    • Redis LPUSH
    • Redis Architecture
    • Redis API
    • Redis Message Queue
    • Redis RPUSH
    • Redis Protocol
    • Redis Data Types
    • Redis Namespace
    • Redis ZRANGE
    • Redis Master Slave
    • Redis Memory Usage
    • Redis WATCH
    • Redis URL
    • Redis Key Value
    • Redis INCR
    • Redis EXISTS
    • Redis INFO
    • Redis HMGET
    • Redis Operator
    • Redis NOAUTH Authentication Required
    • Redis LRANGE
    • Redis RDB
    • Redis TLS
    • Redis HDEL
    • Redis WebSocket
    • Redis HGETALL
    • Redis GETSET
    • Node-RED WebSocket

Redis LRANGE

Definition of Redis LRANGE

Redis LRANGE is a command that returns elements or values stored at the key in the specified range of the lists. It allows you to specify a start and stop position for the elements to return. The start and stop offsets are zero-based indexes; hence the first and second elements of the list are 0 and 1, respectively, and so on. LRANGE also allows negative indexes which retrieve elements from the tail of the lists, where the last and second last elements are -1 and -2, respectively, and so on.

Redis LRANGE

Key Highlights

  • LRANGE is a command used in Redis Lists.
  • LRANGE displays a specific list of elements stored at the key based on the range specified.
  • The offsets start and stop are zero-based indexes; therefore, the first element is always represented as 0.
  • LRANGE allows negative indexing where -1 denotes the last element and -2 denotes the second last element of the list, and so on.

How to Use Redis LRANGE?

The below syntax supports Redis LRANGE.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax

  • The KEY_NAME is the name of the specific key where the elements are stored.
  • The START denotes the first element you want to return.
  • The STOP denotes the last element you want to return.

Before we start moving to the core, please install Redis in your terminal for better understanding and practicing purposes.

Redis LRANGE Command Explained

We know that elements in the lists are stored in an ordered sequence.

Example:

LRANGE command helps to retrieve the elements of the list by specifying the start and stop range without affecting the sequence. The LRANGE command displays elements based on the specified range and also supports negative indexing of the elements in the lists.

a) Displaying a Specific Set of Elements
  • First, we created the lists by adding various elements to the key eduCBA.
  • The RPUSH command adds elements to the right of the list.
  • The LRANGE command with the specified range of 0 to 2 returns the elements.
  • Where redis is 0, the tutorial is 1, and the guide is 2.

Redis Tutorial

b) Displaying all the Elements
  • In this example, the command range of 0 to -1 denotes the first element (redis) and the last element (students), respectively.
  • Hence all the elements from 0 to -1 are returned.

All Elements

c) Using Negative Indexes
  • Here the range 0 denotes the first element (redis), and -2 denotes the second last element (guide) of the list.
  • Hence the command returns all the elements from 0 to -2 of the list.

Redis LRANGE - Command

d) Using out-of-range indexes

1. STOP > size of the list

In the below example, the stop index is 8, which is greater than the size of the list; hence it is considered the last element, and the command returns the elements of the list.

Redis LRANGE - Elements of the List

2. START > size of the list

In the below example, the start position is 5, which is greater than the size of the list; hence the command returns an empty list.

Redis LRANGE - Empty List

Conclusion

Since the Redis LRANGE command is used in List to retrieve data, It finds its application in retrieving desired data from a database consisting of ample data.

FAQs

Q1. What is Redis LRANGE?

Answer: LRANGE is a Redis list command that retrieves the list of elements based on the start and stop offsets. Both the start and stop offsets are zero-based indexes where 0 represents the first element, and -1 represents the last element.

Q2. How LRANGE handles out-of-range offsets?

Answer: The out-of-range offsets are handled in two ways. If the start offset is greater than the size of the list, then the command returns an empty list. In contrast, If the stop offset is greater than the size of the list, then the last element is considered the stop offset, and the command returns the elements.

Q3. Does Redis LRANGE allow Negative indexing?

Answer: Yes, LRANGE supports negative indexing and displays data from the tail of the list. Where -1 represents the last element and -2 represents the second last element, and so on.

Recommended Articles

This article explains everything about Redis LRANGE. To know more about related topics, visit the following links:

  1. Redis Operator
  2. Redis HMGET
  3. Redis INFO
  4. Redis Memory Usage
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more