0
0
Redisquery~3 mins

LLEN for list length in Redis

Choose your learning style9 modes available
Introduction
LLEN helps you find out how many items are in a list stored in Redis. It tells you the list's length quickly.
You want to check how many messages are waiting in a chat queue.
You need to know how many tasks are in a to-do list stored in Redis.
You want to monitor the size of a list to decide if you should add more items.
You want to confirm if a list is empty before processing it.
You want to limit the number of items processed by checking the list length first.
Syntax
Redis
LLEN key
Replace 'key' with the name of your Redis list.
If the key does not exist, LLEN returns 0.
Examples
Returns the length of the list stored at 'mylist'.
Redis
LLEN mylist
Finds out how many tasks are waiting in the 'tasks_queue' list.
Redis
LLEN tasks_queue
Returns 0 if 'emptylist' does not exist or has no items.
Redis
LLEN emptylist
Sample Program
First, we add three fruits to 'mylist'. Then, we check the length of 'mylist', which is 3. Finally, we check the length of a list that does not exist, 'unknownlist', which returns 0.
Redis
127.0.0.1:6379> RPUSH mylist "apple"
(integer) 1
127.0.0.1:6379> RPUSH mylist "banana"
(integer) 2
127.0.0.1:6379> RPUSH mylist "cherry"
(integer) 3
127.0.0.1:6379> LLEN mylist
(integer) 3
127.0.0.1:6379> LLEN unknownlist
(integer) 0
OutputSuccess
Important Notes
LLEN runs in constant time O(1), so it is very fast even for long lists.
It uses very little memory since it only returns a number.
A common mistake is to expect LLEN to return the list items; it only returns the count.
Use LLEN when you only need the size of the list, not the items themselves.
Summary
LLEN returns the number of items in a Redis list.
If the list does not exist, LLEN returns 0.
It is a fast and simple way to check list size.