Redis lists
Introduction to Redis lists
Redis lists are linked lists of string values. Redis lists are frequently used to:
- Implement stacks and queues.
- Build queue management for background worker systems.
Basic commands
LPUSH
adds a new element to the head of a list;RPUSH
adds to the tail.LPOP
removes and returns an element from the head of a list;RPOP
does the same but from the tails of a list.LLEN
returns the length of a list.LMOVE
atomically moves elements from one list to another.LRANGE
extracts a range of elements from a list.LTRIM
reduces a list to the specified range of elements.
Blocking commands
Lists support several blocking commands. For example:
BLPOP
removes and returns an element from the head of a list. If the list is empty, the command blocks until an element becomes available or until the specified timeout is reached.BLMOVE
atomically moves elements from a source list to a target list. If the source list is empty, the command will block until a new element becomes available.
See the complete series of list commands.
Examples
-
Treat a list like a queue (first in, first out):
> LPUSH bikes:repairs bike:1 (integer) 1 > LPUSH bikes:repairs bike:2 (integer) 2 > RPOP bikes:repairs "bike:1" > RPOP bikes:repairs "bike:2"
Are you tired of using redis-cli? Try Redis Insight - the developer GUI for Redis.""" Code samples for List doc pages: https://redis.io/docs/latest/develop/data-types/lists/ """ import redis r = redis.Redis(decode_responses=True) res1 = r.lpush("bikes:repairs", "bike:1") print(res1) # >>> 1 res2 = r.lpush("bikes:repairs", "bike:2") print(res2) # >>> 2 res3 = r.rpop("bikes:repairs") print(res3) # >>> bike:1 res4 = r.rpop("bikes:repairs") print(res4) # >>> bike:2 res5 = r.lpush("bikes:repairs", "bike:1") print(res5) # >>> 1 res6 = r.lpush("bikes:repairs", "bike:2") print(res6) # >>> 2 res7 = r.lpop("bikes:repairs") print(res7) # >>> bike:2 res8 = r.lpop("bikes:repairs") print(res8) # >>> bike:1 res9 = r.llen("bikes:repairs") print(res9) # >>> 0 res10 = r.lpush("bikes:repairs", "bike:1") print(res10) # >>> 1 res11 = r.lpush("bikes:repairs", "bike:2") print(res11) # >>> 2 res12 = r.lmove("bikes:repairs", "bikes:finished", "LEFT", "LEFT") print(res12) # >>> 'bike:2' res13 = r.lrange("bikes:repairs", 0, -1) print(res13) # >>> ['bike:1'] res14 = r.lrange("bikes:finished", 0, -1) print(res14) # >>> ['bike:2'] res15 = r.rpush("bikes:repairs", "bike:1") print(res15) # >>> 1 res16 = r.rpush("bikes:repairs", "bike:2") print(res16) # >>> 2 res17 = r.lpush("bikes:repairs", "bike:important_bike") print(res17) # >>> 3 res18 = r.lrange("bikes:repairs", 0, -1) print(res18) # >>> ['bike:important_bike', 'bike:1', 'bike:2'] res19 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3") print(res19) # >>> 3 res20 = r.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike") print(res20) # >>> 5 res21 = r.lrange("bikes:repairs", 0, -1) print( res21 ) # >>> ['bike:very_important_bike', 'bike:important_bike', 'bike:1', ... res22 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3") print(res22) # >>> 3 res23 = r.rpop("bikes:repairs") print(res23) # >>> 'bike:3' res24 = r.lpop("bikes:repairs") print(res24) # >>> 'bike:1' res25 = r.rpop("bikes:repairs") print(res25) # >>> 'bike:2' res26 = r.rpop("bikes:repairs") print(res26) # >>> None res27 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5") print(res27) # >>> 5 res28 = r.ltrim("bikes:repairs", 0, 2) print(res28) # >>> True res29 = r.lrange("bikes:repairs", 0, -1) print(res29) # >>> ['bike:5', 'bike:4', 'bike:3'] res27 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5") print(res27) # >>> 5 res28 = r.ltrim("bikes:repairs", -3, -1) print(res28) # >>> True res29 = r.lrange("bikes:repairs", 0, -1) print(res29) # >>> ['bike:3', 'bike:4', 'bike:5'] res31 = r.rpush("bikes:repairs", "bike:1", "bike:2") print(res31) # >>> 2 res32 = r.brpop("bikes:repairs", timeout=1) print(res32) # >>> ('bikes:repairs', 'bike:2') res33 = r.brpop("bikes:repairs", timeout=1) print(res33) # >>> ('bikes:repairs', 'bike:1') res34 = r.brpop("bikes:repairs", timeout=1) print(res34) # >>> None res35 = r.delete("new_bikes") print(res35) # >>> 0 res36 = r.lpush("new_bikes", "bike:1", "bike:2", "bike:3") print(res36) # >>> 3 res37 = r.set("new_bikes", "bike:1") print(res37) # >>> True res38 = r.type("new_bikes") print(res38) # >>> 'string' try: res39 = r.lpush("new_bikes", "bike:2", "bike:3") # >>> redis.exceptions.ResponseError: # >>> WRONGTYPE Operation against a key holding the wrong kind of value except redis.exceptions.ResponseError as e: print(e) r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3") print(res36) # >>> 3 res40 = r.exists("bikes:repairs") print(res40) # >>> 1 res41 = r.lpop("bikes:repairs") print(res41) # >>> 'bike:3' res42 = r.lpop("bikes:repairs") print(res42) # >>> 'bike:2' res43 = r.lpop("bikes:repairs") print(res43) # >>> 'bike:1' res44 = r.exists("bikes:repairs") print(res44) # >>> False res45 = r.delete("bikes:repairs") print(res45) # >>> 0 res46 = r.llen("bikes:repairs") print(res46) # >>> 0 res47 = r.lpop("bikes:repairs") print(res47) # >>> None res48 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5") print(res48) # >>> 5 res49 = r.ltrim("bikes:repairs", 0, 2) print(res49) # >>> True res50 = r.lrange("bikes:repairs", 0, -1) print(res50) # >>> ['bike:5', 'bike:4', 'bike:3']
import assert from 'assert'; import { createClient } from 'redis'; const client = await createClient(); await client.connect(); const res1 = await client.lPush('bikes:repairs', 'bike:1'); console.log(res1); // 1 const res2 = await client.lPush('bikes:repairs', 'bike:2'); console.log(res2); // 2 const res3 = await client.rPop('bikes:repairs'); console.log(res3); // bike:1 const res4 = await client.rPop('bikes:repairs'); console.log(res4); // bike:2 const res5 = await client.lPush('bikes:repairs', 'bike:1'); console.log(res5); // 1 const res6 = await client.lPush('bikes:repairs', 'bike:2'); console.log(res6); // 2 const res7 = await client.lPop('bikes:repairs'); console.log(res7); // bike:2 const res8 = await client.lPop('bikes:repairs'); console.log(res8); // bike:1 const res9 = await client.lLen('bikes:repairs'); console.log(res9); // 0 const res10 = await client.lPush('bikes:repairs', 'bike:1'); console.log(res10); // 1 const res11 = await client.lPush('bikes:repairs', 'bike:2'); console.log(res11); // 2 const res12 = await client.lMove('bikes:repairs', 'bikes:finished', 'LEFT', 'LEFT'); console.log(res12); // 'bike:2' const res13 = await client.lRange('bikes:repairs', 0, -1); console.log(res13); // ['bike:1'] const res14 = await client.lRange('bikes:finished', 0, -1); console.log(res14); // ['bike:2'] const res15 = await client.rPush('bikes:repairs', 'bike:1'); console.log(res15); // 1 const res16 = await client.rPush('bikes:repairs', 'bike:2'); console.log(res16); // 2 const res17 = await client.lPush('bikes:repairs', 'bike:important_bike'); console.log(res17); // 3 const res18 = await client.lRange('bikes:repairs', 0, -1); console.log(res18); // ['bike:important_bike', 'bike:1', 'bike:2'] const res19 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']); console.log(res19); // 3 const res20 = await client.lPush( 'bikes:repairs', ['bike:important_bike', 'bike:very_important_bike'] ); console.log(res20); // 5 const res21 = await client.lRange('bikes:repairs', 0, -1); console.log(res21); // ['bike:very_important_bike', 'bike:important_bike', 'bike:1', 'bike:2', 'bike:3'] const res22 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']); console.log(res22); // 3 const res23 = await client.rPop('bikes:repairs'); console.log(res23); // 'bike:3' const res24 = await client.lPop('bikes:repairs'); console.log(res24); // 'bike:1' const res25 = await client.rPop('bikes:repairs'); console.log(res25); // 'bike:2' const res26 = await client.rPop('bikes:repairs'); console.log(res26); // None const res27 = await client.lPush( 'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5'] ); console.log(res27); // 5 const res28 = await client.lTrim('bikes:repairs', 0, 2); console.log(res28); // true const res29 = await client.lRange('bikes:repairs', 0, -1); console.log(res29); // ['bike:5', 'bike:4', 'bike:3'] const res27eol = await client.rPush( 'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5'] ); console.log(res27eol); // 5 const res28eol = await client.lTrim('bikes:repairs', -3, -1); console.log(res28eol); // 'OK' const res29eol = await client.lRange('bikes:repairs', 0, -1); console.log(res29eol); // ['bike:3', 'bike:4', 'bike:5'] const res31 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2']); console.log(res31); // 2 const res32 = await client.brPop('bikes:repairs', 1); console.log(res32); // { key: 'bikes:repairs', element: 'bike:2' } const res33 = await client.brPop('bikes:repairs', 1); console.log(res33); // { key: 'bikes:repairs', element: 'bike:1' } const res34 = await client.brPop('bikes:repairs', 1); console.log(res34); // null const res35 = await client.del('new_bikes'); console.log(res35); // 0 const res36 = await client.lPush('new_bikes', ['bike:1', 'bike:2', 'bike:3']); console.log(res36); // 3 const res37 = await client.set('new_bikes', 'bike:1'); console.log(res37); // 'OK' const res38 = await client.type('new_bikes'); console.log(res38); // 'string' try { const res39 = await client.lPush('new_bikes', 'bike:2', 'bike:3'); // redis.exceptions.ResponseError: // [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value] } catch(e){ console.log(e); } await client.lPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']); console.log(res36); // 3 const res40 = await client.exists('bikes:repairs') console.log(res40); // true const res41 = await client.lPop('bikes:repairs'); console.log(res41); // 'bike:3' const res42 = await client.lPop('bikes:repairs'); console.log(res42); // 'bike:2' const res43 = await client.lPop('bikes:repairs'); console.log(res43); // 'bike:1' const res44 = await client.exists('bikes:repairs'); console.log(res44); // 0 const res45 = await client.del('bikes:repairs'); console.log(res45); // 0 const res46 = await client.lLen('bikes:repairs'); console.log(res46); // 0 const res47 = await client.lPop('bikes:repairs'); console.log(res47); // null const res48 = await client.lPush( 'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5'] ); console.log(res48); // 5 const res49 = await client.lTrim('bikes:repairs', 0, 2); console.log(res49); // 'OK' const res50 = await client.lRange('bikes:repairs', 0, -1); console.log(res50); // ['bike:5', 'bike:4', 'bike:3']
package io.redis.examples; import org.junit.Test; import redis.clients.jedis.UnifiedJedis; import redis.clients.jedis.args.ListDirection; import java.util.List; import static org.junit.Assert.*; public class ListExample { public void run() { UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); long res1 = jedis.lpush("bikes:repairs", "bike:1"); System.out.println(res1); // >>> 1 long res2 = jedis.lpush("bikes:repairs", "bike:2"); System.out.println(res2); // >>> 2 String res3 = jedis.rpop("bikes:repairs"); System.out.println(res3); // >>> bike:1 String res4 = jedis.rpop("bikes:repairs"); System.out.println(res4); // >>> bike:2 long res5 = jedis.lpush("bikes:repairs", "bike:1"); System.out.println(res5); // >>> 1 long res6 = jedis.lpush("bikes:repairs", "bike:2"); System.out.println(res6); // >>> 2 String res7 = jedis.lpop("bikes:repairs"); System.out.println(res7); // >>> bike:2 String res8 = jedis.lpop("bikes:repairs"); System.out.println(res8); // >>> bike:1 long res9 = jedis.llen("bikes:repairs"); System.out.println(res9); // >>> 0 long res10 = jedis.lpush("bikes:repairs", "bike:1"); System.out.println(res10); // >>> 1 long res11 = jedis.lpush("bikes:repairs", "bike:2"); System.out.println(res11); // >>> 2 String res12 = jedis.lmove("bikes:repairs", "bikes:finished", ListDirection.LEFT, ListDirection.LEFT); System.out.println(res12); // >>> bike:2 List<String> res13 = jedis.lrange("bikes:repairs", 0, -1); System.out.println(res13); // >>> [bike:1] List<String> res14 = jedis.lrange("bikes:finished", 0, -1); System.out.println(res14); // >>> [bike:2] long res15 = jedis.rpush("bikes:repairs", "bike:1"); System.out.println(res15); // >>> 1 long res16 = jedis.rpush("bikes:repairs", "bike:2"); System.out.println(res16); // >>> 2 long res17 = jedis.lpush("bikes:repairs", "bike:important_bike"); System.out.println(res17); // >>> 3 List<String> res18 = jedis.lrange("bikes:repairs", 0, -1); System.out.println(res18); // >>> [bike:important_bike, bike:1, bike:2] long res19 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3"); System.out.println(res19); // >>> 3 long res20 = jedis.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike"); System.out.println(res20); // >>> 5 List<String> res21 = jedis.lrange("bikes:repairs", 0, -1); System.out.println(res21); // >>> [bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3] long res22 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3"); System.out.println(res22); // >>> 3 String res23 = jedis.rpop("bikes:repairs"); System.out.println(res23); // >>> bike:3 String res24 = jedis.lpop("bikes:repairs"); System.out.println(res24); // >>> bike:1 String res25 = jedis.rpop("bikes:repairs"); System.out.println(res25); // >>> bike:2 String res26 = jedis.rpop("bikes:repairs"); System.out.println(res26); // >>> null long res27 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5"); System.out.println(res27); // >>> 5 String res28 = jedis.ltrim("bikes:repairs", 0, 2); System.out.println(res28); // >>> OK List<String> res29 = jedis.lrange("bikes:repairs", 0, -1); System.out.println(res29); // >>> [bike:5, bike:4, bike:3] res27 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5"); System.out.println(res27); // >>> 5 res28 = jedis.ltrim("bikes:repairs", -3, -1); System.out.println(res2); // >>> OK res29 = jedis.lrange("bikes:repairs", 0, -1); System.out.println(res29); // >>> [bike:3, bike:4, bike:5] long res31 = jedis.rpush("bikes:repairs", "bike:1", "bike:2"); System.out.println(res31); // >>> 2 List<String> res32 = jedis.brpop(1, "bikes:repairs"); System.out.println(res32); // >>> (bikes:repairs, bike:2) List<String> res33 = jedis.brpop(1,"bikes:repairs"); System.out.println(res33); // >>> (bikes:repairs, bike:1) List<String> res34 = jedis.brpop(1,"bikes:repairs"); System.out.println(res34); // >>> null long res35 = jedis.del("new_bikes"); System.out.println(res35); // >>> 0 long res36 = jedis.lpush("new_bikes", "bike:1", "bike:2", "bike:3"); System.out.println(res36); // >>> 3 String res37 = jedis.set("new_bikes", "bike:1"); System.out.println(res37); // >>> OK String res38 = jedis.type("new_bikes"); System.out.println(res38); // >>> string try { long res39 = jedis.lpush("new_bikes", "bike:2", "bike:3"); } catch (Exception e) { e.printStackTrace(); // >>> redis.clients.jedis.exceptions.JedisDataException: // >>> WRONGTYPE Operation against a key holding the wrong kind of value } jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3"); System.out.println(res36); // >>> 3 boolean res40 = jedis.exists("bikes:repairs"); System.out.println(res40); // >>> true String res41 = jedis.lpop("bikes:repairs"); System.out.println(res41); // >>> bike:3 String res42 = jedis.lpop("bikes:repairs"); System.out.println(res42); // >>> bike:2 String res43 = jedis.lpop("bikes:repairs"); System.out.println(res43); // >>> bike:1 boolean res44 = jedis.exists("bikes:repairs"); System.out.println(res44); // >>> false long res45 = jedis.del("bikes:repairs"); System.out.println(res45); // >>> 0 long res46 = jedis.llen("bikes:repairs"); System.out.println(res46); // >>> 0 String res47 = jedis.lpop("bikes:repairs"); System.out.println(res47); // >>> null long res48 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5"); System.out.println(res48); // >>> 5 String res49 = jedis.ltrim("bikes:repairs", 0, 2); System.out.println(res49); // >>> OK List<String> res50 = jedis.lrange("bikes:repairs", 0, -1); System.out.println(res50); // >>> [bike:5, bike:4, bike:3] } }
package example_commands_test import ( "context" "fmt" "github.com/redis/go-redis/v9" ) func ExampleClient_queue() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res1, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result() if err != nil { panic(err) } fmt.Println(res1) // >>> 1 res2, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result() if err != nil { panic(err) } fmt.Println(res2) // >>> 2 res3, err := rdb.RPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res3) // >>> bike:1 res4, err := rdb.RPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res4) // >>> bike:2 } func ExampleClient_stack() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res5, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result() if err != nil { panic(err) } fmt.Println(res5) // >>> 1 res6, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result() if err != nil { panic(err) } fmt.Println(res6) // >>> 2 res7, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res7) // >>> bike:2 res8, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res8) // >>> bike:1 } func ExampleClient_llen() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res9, err := rdb.LLen(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res9) // >>> 0 } func ExampleClient_lmove_lrange() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res10, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result() if err != nil { panic(err) } fmt.Println(res10) // >>> 1 res11, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result() if err != nil { panic(err) } fmt.Println(res11) // >>> 2 res12, err := rdb.LMove(ctx, "bikes:repairs", "bikes:finished", "LEFT", "LEFT").Result() if err != nil { panic(err) } fmt.Println(res12) // >>> bike:2 res13, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res13) // >>> [bike:1] res14, err := rdb.LRange(ctx, "bikes:finished", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res14) // >>> [bike:2] } func ExampleClient_lpush_rpush() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res15, err := rdb.RPush(ctx, "bikes:repairs", "bike:1").Result() if err != nil { panic(err) } fmt.Println(res15) // >>> 1 res16, err := rdb.RPush(ctx, "bikes:repairs", "bike:2").Result() if err != nil { panic(err) } fmt.Println(res16) // >>> 2 res17, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike").Result() if err != nil { panic(err) } fmt.Println(res17) // >>> 3 res18, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res18) // >>> [bike:important_bike bike:1 bike:2] } func ExampleClient_variadic() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res19, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result() if err != nil { panic(err) } fmt.Println(res19) // >>> 3 res20, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike", "bike:very_important_bike").Result() if err != nil { panic(err) } fmt.Println(res20) // >>> 5 res21, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res21) // >>> [bike:very_important_bike bike:important_bike bike:1 bike:2 bike:3] } func ExampleClient_lpop_rpop() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res22, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result() if err != nil { panic(err) } fmt.Println(res22) // >>> 3 res23, err := rdb.RPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res23) // >>> bike:3 res24, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res24) // >>> bike:1 res25, err := rdb.RPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res25) // >>> bike:2 res26, err := rdb.RPop(ctx, "bikes:repairs").Result() if err != nil { fmt.Println(err) // >>> redis: nil } fmt.Println(res26) // >>> <empty string> } func ExampleClient_ltrim() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res27, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result() if err != nil { panic(err) } fmt.Println(res27) // >>> 5 res28, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result() if err != nil { panic(err) } fmt.Println(res28) // >>> OK res29, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res29) // >>> [bike:5 bike:4 bike:3] } func ExampleClient_ltrim_end_of_list() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res30, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result() if err != nil { panic(err) } fmt.Println(res30) // >>> 5 res31, err := rdb.LTrim(ctx, "bikes:repairs", -3, -1).Result() if err != nil { panic(err) } fmt.Println(res31) // >>> OK res32, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res32) // >>> [bike:3 bike:4 bike:5] } func ExampleClient_brpop() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res33, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2").Result() if err != nil { panic(err) } fmt.Println(res33) // >>> 2 res34, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res34) // >>> [bikes:repairs bike:2] res35, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res35) // >>> [bikes:repairs bike:1] res36, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result() if err != nil { fmt.Println(err) // >>> redis: nil } fmt.Println(res36) // >>> [] } func ExampleClient_rule1() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res37, err := rdb.Del(ctx, "new_bikes").Result() if err != nil { panic(err) } fmt.Println(res37) // >>> 0 res38, err := rdb.LPush(ctx, "new_bikes", "bike:1", "bike:2", "bike:3").Result() if err != nil { panic(err) } fmt.Println(res38) // >>> 3 } func ExampleClient_rule11() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res39, err := rdb.Set(ctx, "new_bikes", "bike:1", 0).Result() if err != nil { panic(err) } fmt.Println(res39) // >>> OK res40, err := rdb.Type(ctx, "new_bikes").Result() if err != nil { panic(err) } fmt.Println(res40) // >>> string res41, err := rdb.LPush(ctx, "new_bikes", "bike:2", "bike:3").Result() if err != nil { fmt.Println(err) // >>> WRONGTYPE Operation against a key holding the wrong kind of value } fmt.Println(res41) } func ExampleClient_rule2() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res42, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result() if err != nil { panic(err) } fmt.Println(res42) // >>> 3 res43, err := rdb.Exists(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res43) // >>> 1 res44, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res44) // >>> bike:3 res45, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res45) // >>> bike:2 res46, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res46) // >>> bike:1 res47, err := rdb.Exists(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res47) // >>> 0 } func ExampleClient_rule3() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res48, err := rdb.Del(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res48) // >>> 0 res49, err := rdb.LLen(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res49) // >>> 0 res50, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { fmt.Println(err) // >>> redis: nil } fmt.Println(res50) // >>> <empty string> } func ExampleClient_ltrim1() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res51, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result() if err != nil { panic(err) } fmt.Println(res51) // >>> 5 res52, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result() if err != nil { panic(err) } fmt.Println(res52) // >>> OK res53, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res53) // >>> [bike:5 bike:4 bike:3] }
public class ListExample { public void run() { var muxer = ConnectionMultiplexer.Connect("localhost:6379"); var db = muxer.GetDatabase(); long res1 = db.ListLeftPush("bikes:repairs", "bike:1"); Console.WriteLine(res1); // >>> 1 long res2 = db.ListLeftPush("bikes:repairs", "bike:2"); Console.WriteLine(res2); // >>> 2 RedisValue res3 = db.ListRightPop("bikes:repairs"); Console.WriteLine(res3); // >>> "bike:1" RedisValue res4 = db.ListRightPop("bikes:repairs"); Console.WriteLine(res4); // >>> "bike:2" long res5 = db.ListLeftPush("bikes:repairs", "bike:1"); Console.WriteLine(res5); // >>> 1 long res6 = db.ListLeftPush("bikes:repairs", "bike:2"); Console.WriteLine(res6); // >>> 2 RedisValue res7 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res7); // >>> "bike:2" RedisValue res8 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res8); // >>> "bike:1" long res9 = db.ListLength("bikes:repairs"); Console.WriteLine(res9); // >>> 0 long res10 = db.ListLeftPush("{bikes}:repairs", "bike:1"); Console.WriteLine(res10); // >>> 1 long res11 = db.ListLeftPush("{bikes}:repairs", "bike:2"); Console.WriteLine(res11); // >>> 2 RedisValue res12 = db.ListMove("{bikes}:repairs", "{bikes}:finished", ListSide.Left, ListSide.Left); Console.Write(res12); // >>> "bike:2" RedisValue[] res13 = db.ListRange("{bikes}:repairs", 0, -1); Console.WriteLine(string.Join(", ", res13)); // >>> "bike:1" RedisValue[] res14 = db.ListRange("{bikes}:finished", 0, -1); Console.WriteLine(string.Join(", ", res14)); // >>> "bike:2" long res15 = db.ListRightPush("bikes:repairs", "bike:1"); Console.WriteLine(res15); // >>> 1 long res16 = db.ListRightPush("bikes:repairs", "bike:2"); Console.WriteLine(res16); // >>> 2 long res17 = db.ListLeftPush("bikes:repairs", "bike:important_bike"); Console.WriteLine(res17); // >>> 3 RedisValue[] res18 = db.ListRange("bikes:repairs", 0, -1); Console.WriteLine(string.Join(", ", res18)); // >>> "bike:important_bike, bike:1, bike:2" long res19 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); Console.WriteLine(res19); // >>> 3 long res20 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:important_bike", "bike:very_important_bike" }); Console.WriteLine(res20); // >>> 5 RedisValue[] res21 = db.ListRange("bikes:repairs", 0, -1); Console.WriteLine(string.Join(", ", res21)); // >>> "bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3" long res22 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); Console.WriteLine(res22); // >>> 3 RedisValue res23 = db.ListRightPop("bikes:repairs"); Console.WriteLine(res23); // >>> "bike:3" RedisValue res24 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res24); // >>> "bike:1" RedisValue res25 = db.ListRightPop("bikes:repairs"); Console.WriteLine(res25); // >>> "bike:2" RedisValue res26 = db.ListRightPop("bikes:repairs"); Console.WriteLine(res26); // >>> <Empty string> long res27 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" }); Console.WriteLine(res27); // >>> 5 db.ListTrim("bikes:repairs", 0, 2); RedisValue[] res28 = db.ListRange("bikes:repairs", 0, -1); Console.WriteLine(string.Join(", ", res28)); // "bike:5, bike:4, bike:3" long res29 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" }); Console.WriteLine(res29); // >>> 5 db.ListTrim("bikes:repairs", -3, -1); RedisValue[] res30 = db.ListRange("bikes:repairs", 0, -1); Console.WriteLine(string.Join(", ", res30)); // >>> "bike:3, bike:4, bike:5" long res31 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2" }); Console.WriteLine(res31); // >>> 2 Tuple<RedisKey, RedisValue>? res32 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1); if (res32 != null) Console.WriteLine($"{res32.Item1} -> {res32.Item2}"); // >>> "bikes:repairs -> bike:2" Tuple<RedisKey, RedisValue>? res33 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1); if (res33 != null) Console.WriteLine($"{res33.Item1} -> {res33.Item2}"); // >>> "bikes:repairs -> bike:1" Tuple<RedisKey, RedisValue>? res34 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1); Console.WriteLine(res34); // >>> "Null" bool res35 = db.KeyDelete("new_bikes"); Console.WriteLine(res35); // >>> False long res36 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); Console.WriteLine(res36); // >>> 3 bool res37 = db.StringSet("new_bikes", "bike:1"); Console.WriteLine(res37); // >>> True RedisType res38 = db.KeyType("new_bikes"); Console.WriteLine(res38); // >>> RedisType.String try { long res39 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:2", "bike:3" }); } catch (Exception e) { Console.WriteLine(e); } long res40 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); Console.WriteLine(res40); // >>> 3 bool res41 = db.KeyExists("bikes:repairs"); Console.WriteLine(res41); // >>> True RedisValue res42 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res42); // >>> "bike:3" RedisValue res43 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res43); // >>> "bike:2" RedisValue res44 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res44); // >>> "bike:1" bool res45 = db.KeyExists("bikes:repairs"); Console.WriteLine(res45); // >>> False bool res46 = db.KeyDelete("bikes:repairs"); Console.WriteLine(res46); // >>> False long res47 = db.ListLength("bikes:repairs"); Console.WriteLine(res47); // >>> 0 RedisValue res48 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res48); // >>> Null long res49 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" }); Console.WriteLine(res49); // >>> 5 db.ListTrim("bikes:repairs", 0, 2); RedisValue[] res50 = db.ListRange("bikes:repairs", 0, -1); Console.WriteLine(string.Join(", ", res50)); // >>> "bike:5, bike:4, bike:3" } }
-
Treat a list like a stack (first in, last out):
> LPUSH bikes:repairs bike:1 (integer) 1 > LPUSH bikes:repairs bike:2 (integer) 2 > LPOP bikes:repairs "bike:2" > LPOP bikes:repairs "bike:1"
Are you tired of using redis-cli? Try Redis Insight - the developer GUI for Redis.""" Code samples for List doc pages: https://redis.io/docs/latest/develop/data-types/lists/ """ import redis r = redis.Redis(decode_responses=True) res1 = r.lpush("bikes:repairs", "bike:1") print(res1) # >>> 1 res2 = r.lpush("bikes:repairs", "bike:2") print(res2) # >>> 2 res3 = r.rpop("bikes:repairs") print(res3) # >>> bike:1 res4 = r.rpop("bikes:repairs") print(res4) # >>> bike:2 res5 = r.lpush("bikes:repairs", "bike:1") print(res5) # >>> 1 res6 = r.lpush("bikes:repairs", "bike:2") print(res6) # >>> 2 res7 = r.lpop("bikes:repairs") print(res7) # >>> bike:2 res8 = r.lpop("bikes:repairs") print(res8) # >>> bike:1 res9 = r.llen("bikes:repairs") print(res9) # >>> 0 res10 = r.lpush("bikes:repairs", "bike:1") print(res10) # >>> 1 res11 = r.lpush("bikes:repairs", "bike:2") print(res11) # >>> 2 res12 = r.lmove("bikes:repairs", "bikes:finished", "LEFT", "LEFT") print(res12) # >>> 'bike:2' res13 = r.lrange("bikes:repairs", 0, -1) print(res13) # >>> ['bike:1'] res14 = r.lrange("bikes:finished", 0, -1) print(res14) # >>> ['bike:2'] res15 = r.rpush("bikes:repairs", "bike:1") print(res15) # >>> 1 res16 = r.rpush("bikes:repairs", "bike:2") print(res16) # >>> 2 res17 = r.lpush("bikes:repairs", "bike:important_bike") print(res17) # >>> 3 res18 = r.lrange("bikes:repairs", 0, -1) print(res18) # >>> ['bike:important_bike', 'bike:1', 'bike:2'] res19 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3") print(res19) # >>> 3 res20 = r.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike") print(res20) # >>> 5 res21 = r.lrange("bikes:repairs", 0, -1) print( res21 ) # >>> ['bike:very_important_bike', 'bike:important_bike', 'bike:1', ... res22 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3") print(res22) # >>> 3 res23 = r.rpop("bikes:repairs") print(res23) # >>> 'bike:3' res24 = r.lpop("bikes:repairs") print(res24) # >>> 'bike:1' res25 = r.rpop("bikes:repairs") print(res25) # >>> 'bike:2' res26 = r.rpop("bikes:repairs") print(res26) # >>> None res27 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5") print(res27) # >>> 5 res28 = r.ltrim("bikes:repairs", 0, 2) print(res28) # >>> True res29 = r.lrange("bikes:repairs", 0, -1) print(res29) # >>> ['bike:5', 'bike:4', 'bike:3'] res27 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5") print(res27) # >>> 5 res28 = r.ltrim("bikes:repairs", -3, -1) print(res28) # >>> True res29 = r.lrange("bikes:repairs", 0, -1) print(res29) # >>> ['bike:3', 'bike:4', 'bike:5'] res31 = r.rpush("bikes:repairs", "bike:1", "bike:2") print(res31) # >>> 2 res32 = r.brpop("bikes:repairs", timeout=1) print(res32) # >>> ('bikes:repairs', 'bike:2') res33 = r.brpop("bikes:repairs", timeout=1) print(res33) # >>> ('bikes:repairs', 'bike:1') res34 = r.brpop("bikes:repairs", timeout=1) print(res34) # >>> None res35 = r.delete("new_bikes") print(res35) # >>> 0 res36 = r.lpush("new_bikes", "bike:1", "bike:2", "bike:3") print(res36) # >>> 3 res37 = r.set("new_bikes", "bike:1") print(res37) # >>> True res38 = r.type("new_bikes") print(res38) # >>> 'string' try: res39 = r.lpush("new_bikes", "bike:2", "bike:3") # >>> redis.exceptions.ResponseError: # >>> WRONGTYPE Operation against a key holding the wrong kind of value except redis.exceptions.ResponseError as e: print(e) r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3") print(res36) # >>> 3 res40 = r.exists("bikes:repairs") print(res40) # >>> 1 res41 = r.lpop("bikes:repairs") print(res41) # >>> 'bike:3' res42 = r.lpop("bikes:repairs") print(res42) # >>> 'bike:2' res43 = r.lpop("bikes:repairs") print(res43) # >>> 'bike:1' res44 = r.exists("bikes:repairs") print(res44) # >>> False res45 = r.delete("bikes:repairs") print(res45) # >>> 0 res46 = r.llen("bikes:repairs") print(res46) # >>> 0 res47 = r.lpop("bikes:repairs") print(res47) # >>> None res48 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5") print(res48) # >>> 5 res49 = r.ltrim("bikes:repairs", 0, 2) print(res49) # >>> True res50 = r.lrange("bikes:repairs", 0, -1) print(res50) # >>> ['bike:5', 'bike:4', 'bike:3']
import assert from 'assert'; import { createClient } from 'redis'; const client = await createClient(); await client.connect(); const res1 = await client.lPush('bikes:repairs', 'bike:1'); console.log(res1); // 1 const res2 = await client.lPush('bikes:repairs', 'bike:2'); console.log(res2); // 2 const res3 = await client.rPop('bikes:repairs'); console.log(res3); // bike:1 const res4 = await client.rPop('bikes:repairs'); console.log(res4); // bike:2 const res5 = await client.lPush('bikes:repairs', 'bike:1'); console.log(res5); // 1 const res6 = await client.lPush('bikes:repairs', 'bike:2'); console.log(res6); // 2 const res7 = await client.lPop('bikes:repairs'); console.log(res7); // bike:2 const res8 = await client.lPop('bikes:repairs'); console.log(res8); // bike:1 const res9 = await client.lLen('bikes:repairs'); console.log(res9); // 0 const res10 = await client.lPush('bikes:repairs', 'bike:1'); console.log(res10); // 1 const res11 = await client.lPush('bikes:repairs', 'bike:2'); console.log(res11); // 2 const res12 = await client.lMove('bikes:repairs', 'bikes:finished', 'LEFT', 'LEFT'); console.log(res12); // 'bike:2' const res13 = await client.lRange('bikes:repairs', 0, -1); console.log(res13); // ['bike:1'] const res14 = await client.lRange('bikes:finished', 0, -1); console.log(res14); // ['bike:2'] const res15 = await client.rPush('bikes:repairs', 'bike:1'); console.log(res15); // 1 const res16 = await client.rPush('bikes:repairs', 'bike:2'); console.log(res16); // 2 const res17 = await client.lPush('bikes:repairs', 'bike:important_bike'); console.log(res17); // 3 const res18 = await client.lRange('bikes:repairs', 0, -1); console.log(res18); // ['bike:important_bike', 'bike:1', 'bike:2'] const res19 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']); console.log(res19); // 3 const res20 = await client.lPush( 'bikes:repairs', ['bike:important_bike', 'bike:very_important_bike'] ); console.log(res20); // 5 const res21 = await client.lRange('bikes:repairs', 0, -1); console.log(res21); // ['bike:very_important_bike', 'bike:important_bike', 'bike:1', 'bike:2', 'bike:3'] const res22 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']); console.log(res22); // 3 const res23 = await client.rPop('bikes:repairs'); console.log(res23); // 'bike:3' const res24 = await client.lPop('bikes:repairs'); console.log(res24); // 'bike:1' const res25 = await client.rPop('bikes:repairs'); console.log(res25); // 'bike:2' const res26 = await client.rPop('bikes:repairs'); console.log(res26); // None const res27 = await client.lPush( 'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5'] ); console.log(res27); // 5 const res28 = await client.lTrim('bikes:repairs', 0, 2); console.log(res28); // true const res29 = await client.lRange('bikes:repairs', 0, -1); console.log(res29); // ['bike:5', 'bike:4', 'bike:3'] const res27eol = await client.rPush( 'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5'] ); console.log(res27eol); // 5 const res28eol = await client.lTrim('bikes:repairs', -3, -1); console.log(res28eol); // 'OK' const res29eol = await client.lRange('bikes:repairs', 0, -1); console.log(res29eol); // ['bike:3', 'bike:4', 'bike:5'] const res31 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2']); console.log(res31); // 2 const res32 = await client.brPop('bikes:repairs', 1); console.log(res32); // { key: 'bikes:repairs', element: 'bike:2' } const res33 = await client.brPop('bikes:repairs', 1); console.log(res33); // { key: 'bikes:repairs', element: 'bike:1' } const res34 = await client.brPop('bikes:repairs', 1); console.log(res34); // null const res35 = await client.del('new_bikes'); console.log(res35); // 0 const res36 = await client.lPush('new_bikes', ['bike:1', 'bike:2', 'bike:3']); console.log(res36); // 3 const res37 = await client.set('new_bikes', 'bike:1'); console.log(res37); // 'OK' const res38 = await client.type('new_bikes'); console.log(res38); // 'string' try { const res39 = await client.lPush('new_bikes', 'bike:2', 'bike:3'); // redis.exceptions.ResponseError: // [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value] } catch(e){ console.log(e); } await client.lPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']); console.log(res36); // 3 const res40 = await client.exists('bikes:repairs') console.log(res40); // true const res41 = await client.lPop('bikes:repairs'); console.log(res41); // 'bike:3' const res42 = await client.lPop('bikes:repairs'); console.log(res42); // 'bike:2' const res43 = await client.lPop('bikes:repairs'); console.log(res43); // 'bike:1' const res44 = await client.exists('bikes:repairs'); console.log(res44); // 0 const res45 = await client.del('bikes:repairs'); console.log(res45); // 0 const res46 = await client.lLen('bikes:repairs'); console.log(res46); // 0 const res47 = await client.lPop('bikes:repairs'); console.log(res47); // null const res48 = await client.lPush( 'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5'] ); console.log(res48); // 5 const res49 = await client.lTrim('bikes:repairs', 0, 2); console.log(res49); // 'OK' const res50 = await client.lRange('bikes:repairs', 0, -1); console.log(res50); // ['bike:5', 'bike:4', 'bike:3']
package io.redis.examples; import org.junit.Test; import redis.clients.jedis.UnifiedJedis; import redis.clients.jedis.args.ListDirection; import java.util.List; import static org.junit.Assert.*; public class ListExample { public void run() { UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); long res1 = jedis.lpush("bikes:repairs", "bike:1"); System.out.println(res1); // >>> 1 long res2 = jedis.lpush("bikes:repairs", "bike:2"); System.out.println(res2); // >>> 2 String res3 = jedis.rpop("bikes:repairs"); System.out.println(res3); // >>> bike:1 String res4 = jedis.rpop("bikes:repairs"); System.out.println(res4); // >>> bike:2 long res5 = jedis.lpush("bikes:repairs", "bike:1"); System.out.println(res5); // >>> 1 long res6 = jedis.lpush("bikes:repairs", "bike:2"); System.out.println(res6); // >>> 2 String res7 = jedis.lpop("bikes:repairs"); System.out.println(res7); // >>> bike:2 String res8 = jedis.lpop("bikes:repairs"); System.out.println(res8); // >>> bike:1 long res9 = jedis.llen("bikes:repairs"); System.out.println(res9); // >>> 0 long res10 = jedis.lpush("bikes:repairs", "bike:1"); System.out.println(res10); // >>> 1 long res11 = jedis.lpush("bikes:repairs", "bike:2"); System.out.println(res11); // >>> 2 String res12 = jedis.lmove("bikes:repairs", "bikes:finished", ListDirection.LEFT, ListDirection.LEFT); System.out.println(res12); // >>> bike:2 List<String> res13 = jedis.lrange("bikes:repairs", 0, -1); System.out.println(res13); // >>> [bike:1] List<String> res14 = jedis.lrange("bikes:finished", 0, -1); System.out.println(res14); // >>> [bike:2] long res15 = jedis.rpush("bikes:repairs", "bike:1"); System.out.println(res15); // >>> 1 long res16 = jedis.rpush("bikes:repairs", "bike:2"); System.out.println(res16); // >>> 2 long res17 = jedis.lpush("bikes:repairs", "bike:important_bike"); System.out.println(res17); // >>> 3 List<String> res18 = jedis.lrange("bikes:repairs", 0, -1); System.out.println(res18); // >>> [bike:important_bike, bike:1, bike:2] long res19 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3"); System.out.println(res19); // >>> 3 long res20 = jedis.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike"); System.out.println(res20); // >>> 5 List<String> res21 = jedis.lrange("bikes:repairs", 0, -1); System.out.println(res21); // >>> [bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3] long res22 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3"); System.out.println(res22); // >>> 3 String res23 = jedis.rpop("bikes:repairs"); System.out.println(res23); // >>> bike:3 String res24 = jedis.lpop("bikes:repairs"); System.out.println(res24); // >>> bike:1 String res25 = jedis.rpop("bikes:repairs"); System.out.println(res25); // >>> bike:2 String res26 = jedis.rpop("bikes:repairs"); System.out.println(res26); // >>> null long res27 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5"); System.out.println(res27); // >>> 5 String res28 = jedis.ltrim("bikes:repairs", 0, 2); System.out.println(res28); // >>> OK List<String> res29 = jedis.lrange("bikes:repairs", 0, -1); System.out.println(res29); // >>> [bike:5, bike:4, bike:3] res27 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5"); System.out.println(res27); // >>> 5 res28 = jedis.ltrim("bikes:repairs", -3, -1); System.out.println(res2); // >>> OK res29 = jedis.lrange("bikes:repairs", 0, -1); System.out.println(res29); // >>> [bike:3, bike:4, bike:5] long res31 = jedis.rpush("bikes:repairs", "bike:1", "bike:2"); System.out.println(res31); // >>> 2 List<String> res32 = jedis.brpop(1, "bikes:repairs"); System.out.println(res32); // >>> (bikes:repairs, bike:2) List<String> res33 = jedis.brpop(1,"bikes:repairs"); System.out.println(res33); // >>> (bikes:repairs, bike:1) List<String> res34 = jedis.brpop(1,"bikes:repairs"); System.out.println(res34); // >>> null long res35 = jedis.del("new_bikes"); System.out.println(res35); // >>> 0 long res36 = jedis.lpush("new_bikes", "bike:1", "bike:2", "bike:3"); System.out.println(res36); // >>> 3 String res37 = jedis.set("new_bikes", "bike:1"); System.out.println(res37); // >>> OK String res38 = jedis.type("new_bikes"); System.out.println(res38); // >>> string try { long res39 = jedis.lpush("new_bikes", "bike:2", "bike:3"); } catch (Exception e) { e.printStackTrace(); // >>> redis.clients.jedis.exceptions.JedisDataException: // >>> WRONGTYPE Operation against a key holding the wrong kind of value } jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3"); System.out.println(res36); // >>> 3 boolean res40 = jedis.exists("bikes:repairs"); System.out.println(res40); // >>> true String res41 = jedis.lpop("bikes:repairs"); System.out.println(res41); // >>> bike:3 String res42 = jedis.lpop("bikes:repairs"); System.out.println(res42); // >>> bike:2 String res43 = jedis.lpop("bikes:repairs"); System.out.println(res43); // >>> bike:1 boolean res44 = jedis.exists("bikes:repairs"); System.out.println(res44); // >>> false long res45 = jedis.del("bikes:repairs"); System.out.println(res45); // >>> 0 long res46 = jedis.llen("bikes:repairs"); System.out.println(res46); // >>> 0 String res47 = jedis.lpop("bikes:repairs"); System.out.println(res47); // >>> null long res48 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5"); System.out.println(res48); // >>> 5 String res49 = jedis.ltrim("bikes:repairs", 0, 2); System.out.println(res49); // >>> OK List<String> res50 = jedis.lrange("bikes:repairs", 0, -1); System.out.println(res50); // >>> [bike:5, bike:4, bike:3] } }
package example_commands_test import ( "context" "fmt" "github.com/redis/go-redis/v9" ) func ExampleClient_queue() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res1, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result() if err != nil { panic(err) } fmt.Println(res1) // >>> 1 res2, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result() if err != nil { panic(err) } fmt.Println(res2) // >>> 2 res3, err := rdb.RPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res3) // >>> bike:1 res4, err := rdb.RPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res4) // >>> bike:2 } func ExampleClient_stack() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res5, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result() if err != nil { panic(err) } fmt.Println(res5) // >>> 1 res6, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result() if err != nil { panic(err) } fmt.Println(res6) // >>> 2 res7, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res7) // >>> bike:2 res8, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res8) // >>> bike:1 } func ExampleClient_llen() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res9, err := rdb.LLen(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res9) // >>> 0 } func ExampleClient_lmove_lrange() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res10, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result() if err != nil { panic(err) } fmt.Println(res10) // >>> 1 res11, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result() if err != nil { panic(err) } fmt.Println(res11) // >>> 2 res12, err := rdb.LMove(ctx, "bikes:repairs", "bikes:finished", "LEFT", "LEFT").Result() if err != nil { panic(err) } fmt.Println(res12) // >>> bike:2 res13, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res13) // >>> [bike:1] res14, err := rdb.LRange(ctx, "bikes:finished", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res14) // >>> [bike:2] } func ExampleClient_lpush_rpush() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res15, err := rdb.RPush(ctx, "bikes:repairs", "bike:1").Result() if err != nil { panic(err) } fmt.Println(res15) // >>> 1 res16, err := rdb.RPush(ctx, "bikes:repairs", "bike:2").Result() if err != nil { panic(err) } fmt.Println(res16) // >>> 2 res17, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike").Result() if err != nil { panic(err) } fmt.Println(res17) // >>> 3 res18, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res18) // >>> [bike:important_bike bike:1 bike:2] } func ExampleClient_variadic() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res19, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result() if err != nil { panic(err) } fmt.Println(res19) // >>> 3 res20, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike", "bike:very_important_bike").Result() if err != nil { panic(err) } fmt.Println(res20) // >>> 5 res21, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res21) // >>> [bike:very_important_bike bike:important_bike bike:1 bike:2 bike:3] } func ExampleClient_lpop_rpop() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res22, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result() if err != nil { panic(err) } fmt.Println(res22) // >>> 3 res23, err := rdb.RPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res23) // >>> bike:3 res24, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res24) // >>> bike:1 res25, err := rdb.RPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res25) // >>> bike:2 res26, err := rdb.RPop(ctx, "bikes:repairs").Result() if err != nil { fmt.Println(err) // >>> redis: nil } fmt.Println(res26) // >>> <empty string> } func ExampleClient_ltrim() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res27, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result() if err != nil { panic(err) } fmt.Println(res27) // >>> 5 res28, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result() if err != nil { panic(err) } fmt.Println(res28) // >>> OK res29, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res29) // >>> [bike:5 bike:4 bike:3] } func ExampleClient_ltrim_end_of_list() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res30, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result() if err != nil { panic(err) } fmt.Println(res30) // >>> 5 res31, err := rdb.LTrim(ctx, "bikes:repairs", -3, -1).Result() if err != nil { panic(err) } fmt.Println(res31) // >>> OK res32, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res32) // >>> [bike:3 bike:4 bike:5] } func ExampleClient_brpop() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res33, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2").Result() if err != nil { panic(err) } fmt.Println(res33) // >>> 2 res34, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res34) // >>> [bikes:repairs bike:2] res35, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res35) // >>> [bikes:repairs bike:1] res36, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result() if err != nil { fmt.Println(err) // >>> redis: nil } fmt.Println(res36) // >>> [] } func ExampleClient_rule1() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res37, err := rdb.Del(ctx, "new_bikes").Result() if err != nil { panic(err) } fmt.Println(res37) // >>> 0 res38, err := rdb.LPush(ctx, "new_bikes", "bike:1", "bike:2", "bike:3").Result() if err != nil { panic(err) } fmt.Println(res38) // >>> 3 } func ExampleClient_rule11() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res39, err := rdb.Set(ctx, "new_bikes", "bike:1", 0).Result() if err != nil { panic(err) } fmt.Println(res39) // >>> OK res40, err := rdb.Type(ctx, "new_bikes").Result() if err != nil { panic(err) } fmt.Println(res40) // >>> string res41, err := rdb.LPush(ctx, "new_bikes", "bike:2", "bike:3").Result() if err != nil { fmt.Println(err) // >>> WRONGTYPE Operation against a key holding the wrong kind of value } fmt.Println(res41) } func ExampleClient_rule2() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res42, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result() if err != nil { panic(err) } fmt.Println(res42) // >>> 3 res43, err := rdb.Exists(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res43) // >>> 1 res44, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res44) // >>> bike:3 res45, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res45) // >>> bike:2 res46, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res46) // >>> bike:1 res47, err := rdb.Exists(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res47) // >>> 0 } func ExampleClient_rule3() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res48, err := rdb.Del(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res48) // >>> 0 res49, err := rdb.LLen(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res49) // >>> 0 res50, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { fmt.Println(err) // >>> redis: nil } fmt.Println(res50) // >>> <empty string> } func ExampleClient_ltrim1() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res51, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result() if err != nil { panic(err) } fmt.Println(res51) // >>> 5 res52, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result() if err != nil { panic(err) } fmt.Println(res52) // >>> OK res53, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res53) // >>> [bike:5 bike:4 bike:3] }
public class ListExample { public void run() { var muxer = ConnectionMultiplexer.Connect("localhost:6379"); var db = muxer.GetDatabase(); long res1 = db.ListLeftPush("bikes:repairs", "bike:1"); Console.WriteLine(res1); // >>> 1 long res2 = db.ListLeftPush("bikes:repairs", "bike:2"); Console.WriteLine(res2); // >>> 2 RedisValue res3 = db.ListRightPop("bikes:repairs"); Console.WriteLine(res3); // >>> "bike:1" RedisValue res4 = db.ListRightPop("bikes:repairs"); Console.WriteLine(res4); // >>> "bike:2" long res5 = db.ListLeftPush("bikes:repairs", "bike:1"); Console.WriteLine(res5); // >>> 1 long res6 = db.ListLeftPush("bikes:repairs", "bike:2"); Console.WriteLine(res6); // >>> 2 RedisValue res7 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res7); // >>> "bike:2" RedisValue res8 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res8); // >>> "bike:1" long res9 = db.ListLength("bikes:repairs"); Console.WriteLine(res9); // >>> 0 long res10 = db.ListLeftPush("{bikes}:repairs", "bike:1"); Console.WriteLine(res10); // >>> 1 long res11 = db.ListLeftPush("{bikes}:repairs", "bike:2"); Console.WriteLine(res11); // >>> 2 RedisValue res12 = db.ListMove("{bikes}:repairs", "{bikes}:finished", ListSide.Left, ListSide.Left); Console.Write(res12); // >>> "bike:2" RedisValue[] res13 = db.ListRange("{bikes}:repairs", 0, -1); Console.WriteLine(string.Join(", ", res13)); // >>> "bike:1" RedisValue[] res14 = db.ListRange("{bikes}:finished", 0, -1); Console.WriteLine(string.Join(", ", res14)); // >>> "bike:2" long res15 = db.ListRightPush("bikes:repairs", "bike:1"); Console.WriteLine(res15); // >>> 1 long res16 = db.ListRightPush("bikes:repairs", "bike:2"); Console.WriteLine(res16); // >>> 2 long res17 = db.ListLeftPush("bikes:repairs", "bike:important_bike"); Console.WriteLine(res17); // >>> 3 RedisValue[] res18 = db.ListRange("bikes:repairs", 0, -1); Console.WriteLine(string.Join(", ", res18)); // >>> "bike:important_bike, bike:1, bike:2" long res19 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); Console.WriteLine(res19); // >>> 3 long res20 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:important_bike", "bike:very_important_bike" }); Console.WriteLine(res20); // >>> 5 RedisValue[] res21 = db.ListRange("bikes:repairs", 0, -1); Console.WriteLine(string.Join(", ", res21)); // >>> "bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3" long res22 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); Console.WriteLine(res22); // >>> 3 RedisValue res23 = db.ListRightPop("bikes:repairs"); Console.WriteLine(res23); // >>> "bike:3" RedisValue res24 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res24); // >>> "bike:1" RedisValue res25 = db.ListRightPop("bikes:repairs"); Console.WriteLine(res25); // >>> "bike:2" RedisValue res26 = db.ListRightPop("bikes:repairs"); Console.WriteLine(res26); // >>> <Empty string> long res27 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" }); Console.WriteLine(res27); // >>> 5 db.ListTrim("bikes:repairs", 0, 2); RedisValue[] res28 = db.ListRange("bikes:repairs", 0, -1); Console.WriteLine(string.Join(", ", res28)); // "bike:5, bike:4, bike:3" long res29 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" }); Console.WriteLine(res29); // >>> 5 db.ListTrim("bikes:repairs", -3, -1); RedisValue[] res30 = db.ListRange("bikes:repairs", 0, -1); Console.WriteLine(string.Join(", ", res30)); // >>> "bike:3, bike:4, bike:5" long res31 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2" }); Console.WriteLine(res31); // >>> 2 Tuple<RedisKey, RedisValue>? res32 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1); if (res32 != null) Console.WriteLine($"{res32.Item1} -> {res32.Item2}"); // >>> "bikes:repairs -> bike:2" Tuple<RedisKey, RedisValue>? res33 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1); if (res33 != null) Console.WriteLine($"{res33.Item1} -> {res33.Item2}"); // >>> "bikes:repairs -> bike:1" Tuple<RedisKey, RedisValue>? res34 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1); Console.WriteLine(res34); // >>> "Null" bool res35 = db.KeyDelete("new_bikes"); Console.WriteLine(res35); // >>> False long res36 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); Console.WriteLine(res36); // >>> 3 bool res37 = db.StringSet("new_bikes", "bike:1"); Console.WriteLine(res37); // >>> True RedisType res38 = db.KeyType("new_bikes"); Console.WriteLine(res38); // >>> RedisType.String try { long res39 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:2", "bike:3" }); } catch (Exception e) { Console.WriteLine(e); } long res40 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); Console.WriteLine(res40); // >>> 3 bool res41 = db.KeyExists("bikes:repairs"); Console.WriteLine(res41); // >>> True RedisValue res42 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res42); // >>> "bike:3" RedisValue res43 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res43); // >>> "bike:2" RedisValue res44 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res44); // >>> "bike:1" bool res45 = db.KeyExists("bikes:repairs"); Console.WriteLine(res45); // >>> False bool res46 = db.KeyDelete("bikes:repairs"); Console.WriteLine(res46); // >>> False long res47 = db.ListLength("bikes:repairs"); Console.WriteLine(res47); // >>> 0 RedisValue res48 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res48); // >>> Null long res49 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" }); Console.WriteLine(res49); // >>> 5 db.ListTrim("bikes:repairs", 0, 2); RedisValue[] res50 = db.ListRange("bikes:repairs", 0, -1); Console.WriteLine(string.Join(", ", res50)); // >>> "bike:5, bike:4, bike:3" } }
-
Check the length of a list:
> LLEN bikes:repairs (integer) 0
Are you tired of using redis-cli? Try Redis Insight - the developer GUI for Redis.""" Code samples for List doc pages: https://redis.io/docs/latest/develop/data-types/lists/ """ import redis r = redis.Redis(decode_responses=True) res1 = r.lpush("bikes:repairs", "bike:1") print(res1) # >>> 1 res2 = r.lpush("bikes:repairs", "bike:2") print(res2) # >>> 2 res3 = r.rpop("bikes:repairs") print(res3) # >>> bike:1 res4 = r.rpop("bikes:repairs") print(res4) # >>> bike:2 res5 = r.lpush("bikes:repairs", "bike:1") print(res5) # >>> 1 res6 = r.lpush("bikes:repairs", "bike:2") print(res6) # >>> 2 res7 = r.lpop("bikes:repairs") print(res7) # >>> bike:2 res8 = r.lpop("bikes:repairs") print(res8) # >>> bike:1 res9 = r.llen("bikes:repairs") print(res9) # >>> 0 res10 = r.lpush("bikes:repairs", "bike:1") print(res10) # >>> 1 res11 = r.lpush("bikes:repairs", "bike:2") print(res11) # >>> 2 res12 = r.lmove("bikes:repairs", "bikes:finished", "LEFT", "LEFT") print(res12) # >>> 'bike:2' res13 = r.lrange("bikes:repairs", 0, -1) print(res13) # >>> ['bike:1'] res14 = r.lrange("bikes:finished", 0, -1) print(res14) # >>> ['bike:2'] res15 = r.rpush("bikes:repairs", "bike:1") print(res15) # >>> 1 res16 = r.rpush("bikes:repairs", "bike:2") print(res16) # >>> 2 res17 = r.lpush("bikes:repairs", "bike:important_bike") print(res17) # >>> 3 res18 = r.lrange("bikes:repairs", 0, -1) print(res18) # >>> ['bike:important_bike', 'bike:1', 'bike:2'] res19 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3") print(res19) # >>> 3 res20 = r.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike") print(res20) # >>> 5 res21 = r.lrange("bikes:repairs", 0, -1) print( res21 ) # >>> ['bike:very_important_bike', 'bike:important_bike', 'bike:1', ... res22 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3") print(res22) # >>> 3 res23 = r.rpop("bikes:repairs") print(res23) # >>> 'bike:3' res24 = r.lpop("bikes:repairs") print(res24) # >>> 'bike:1' res25 = r.rpop("bikes:repairs") print(res25) # >>> 'bike:2' res26 = r.rpop("bikes:repairs") print(res26) # >>> None res27 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5") print(res27) # >>> 5 res28 = r.ltrim("bikes:repairs", 0, 2) print(res28) # >>> True res29 = r.lrange("bikes:repairs", 0, -1) print(res29) # >>> ['bike:5', 'bike:4', 'bike:3'] res27 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5") print(res27) # >>> 5 res28 = r.ltrim("bikes:repairs", -3, -1) print(res28) # >>> True res29 = r.lrange("bikes:repairs", 0, -1) print(res29) # >>> ['bike:3', 'bike:4', 'bike:5'] res31 = r.rpush("bikes:repairs", "bike:1", "bike:2") print(res31) # >>> 2 res32 = r.brpop("bikes:repairs", timeout=1) print(res32) # >>> ('bikes:repairs', 'bike:2') res33 = r.brpop("bikes:repairs", timeout=1) print(res33) # >>> ('bikes:repairs', 'bike:1') res34 = r.brpop("bikes:repairs", timeout=1) print(res34) # >>> None res35 = r.delete("new_bikes") print(res35) # >>> 0 res36 = r.lpush("new_bikes", "bike:1", "bike:2", "bike:3") print(res36) # >>> 3 res37 = r.set("new_bikes", "bike:1") print(res37) # >>> True res38 = r.type("new_bikes") print(res38) # >>> 'string' try: res39 = r.lpush("new_bikes", "bike:2", "bike:3") # >>> redis.exceptions.ResponseError: # >>> WRONGTYPE Operation against a key holding the wrong kind of value except redis.exceptions.ResponseError as e: print(e) r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3") print(res36) # >>> 3 res40 = r.exists("bikes:repairs") print(res40) # >>> 1 res41 = r.lpop("bikes:repairs") print(res41) # >>> 'bike:3' res42 = r.lpop("bikes:repairs") print(res42) # >>> 'bike:2' res43 = r.lpop("bikes:repairs") print(res43) # >>> 'bike:1' res44 = r.exists("bikes:repairs") print(res44) # >>> False res45 = r.delete("bikes:repairs") print(res45) # >>> 0 res46 = r.llen("bikes:repairs") print(res46) # >>> 0 res47 = r.lpop("bikes:repairs") print(res47) # >>> None res48 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5") print(res48) # >>> 5 res49 = r.ltrim("bikes:repairs", 0, 2) print(res49) # >>> True res50 = r.lrange("bikes:repairs", 0, -1) print(res50) # >>> ['bike:5', 'bike:4', 'bike:3']
import assert from 'assert'; import { createClient } from 'redis'; const client = await createClient(); await client.connect(); const res1 = await client.lPush('bikes:repairs', 'bike:1'); console.log(res1); // 1 const res2 = await client.lPush('bikes:repairs', 'bike:2'); console.log(res2); // 2 const res3 = await client.rPop('bikes:repairs'); console.log(res3); // bike:1 const res4 = await client.rPop('bikes:repairs'); console.log(res4); // bike:2 const res5 = await client.lPush('bikes:repairs', 'bike:1'); console.log(res5); // 1 const res6 = await client.lPush('bikes:repairs', 'bike:2'); console.log(res6); // 2 const res7 = await client.lPop('bikes:repairs'); console.log(res7); // bike:2 const res8 = await client.lPop('bikes:repairs'); console.log(res8); // bike:1 const res9 = await client.lLen('bikes:repairs'); console.log(res9); // 0 const res10 = await client.lPush('bikes:repairs', 'bike:1'); console.log(res10); // 1 const res11 = await client.lPush('bikes:repairs', 'bike:2'); console.log(res11); // 2 const res12 = await client.lMove('bikes:repairs', 'bikes:finished', 'LEFT', 'LEFT'); console.log(res12); // 'bike:2' const res13 = await client.lRange('bikes:repairs', 0, -1); console.log(res13); // ['bike:1'] const res14 = await client.lRange('bikes:finished', 0, -1); console.log(res14); // ['bike:2'] const res15 = await client.rPush('bikes:repairs', 'bike:1'); console.log(res15); // 1 const res16 = await client.rPush('bikes:repairs', 'bike:2'); console.log(res16); // 2 const res17 = await client.lPush('bikes:repairs', 'bike:important_bike'); console.log(res17); // 3 const res18 = await client.lRange('bikes:repairs', 0, -1); console.log(res18); // ['bike:important_bike', 'bike:1', 'bike:2'] const res19 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']); console.log(res19); // 3 const res20 = await client.lPush( 'bikes:repairs', ['bike:important_bike', 'bike:very_important_bike'] ); console.log(res20); // 5 const res21 = await client.lRange('bikes:repairs', 0, -1); console.log(res21); // ['bike:very_important_bike', 'bike:important_bike', 'bike:1', 'bike:2', 'bike:3'] const res22 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']); console.log(res22); // 3 const res23 = await client.rPop('bikes:repairs'); console.log(res23); // 'bike:3' const res24 = await client.lPop('bikes:repairs'); console.log(res24); // 'bike:1' const res25 = await client.rPop('bikes:repairs'); console.log(res25); // 'bike:2' const res26 = await client.rPop('bikes:repairs'); console.log(res26); // None const res27 = await client.lPush( 'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5'] ); console.log(res27); // 5 const res28 = await client.lTrim('bikes:repairs', 0, 2); console.log(res28); // true const res29 = await client.lRange('bikes:repairs', 0, -1); console.log(res29); // ['bike:5', 'bike:4', 'bike:3'] const res27eol = await client.rPush( 'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5'] ); console.log(res27eol); // 5 const res28eol = await client.lTrim('bikes:repairs', -3, -1); console.log(res28eol); // 'OK' const res29eol = await client.lRange('bikes:repairs', 0, -1); console.log(res29eol); // ['bike:3', 'bike:4', 'bike:5'] const res31 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2']); console.log(res31); // 2 const res32 = await client.brPop('bikes:repairs', 1); console.log(res32); // { key: 'bikes:repairs', element: 'bike:2' } const res33 = await client.brPop('bikes:repairs', 1); console.log(res33); // { key: 'bikes:repairs', element: 'bike:1' } const res34 = await client.brPop('bikes:repairs', 1); console.log(res34); // null const res35 = await client.del('new_bikes'); console.log(res35); // 0 const res36 = await client.lPush('new_bikes', ['bike:1', 'bike:2', 'bike:3']); console.log(res36); // 3 const res37 = await client.set('new_bikes', 'bike:1'); console.log(res37); // 'OK' const res38 = await client.type('new_bikes'); console.log(res38); // 'string' try { const res39 = await client.lPush('new_bikes', 'bike:2', 'bike:3'); // redis.exceptions.ResponseError: // [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value] } catch(e){ console.log(e); } await client.lPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']); console.log(res36); // 3 const res40 = await client.exists('bikes:repairs') console.log(res40); // true const res41 = await client.lPop('bikes:repairs'); console.log(res41); // 'bike:3' const res42 = await client.lPop('bikes:repairs'); console.log(res42); // 'bike:2' const res43 = await client.lPop('bikes:repairs'); console.log(res43); // 'bike:1' const res44 = await client.exists('bikes:repairs'); console.log(res44); // 0 const res45 = await client.del('bikes:repairs'); console.log(res45); // 0 const res46 = await client.lLen('bikes:repairs'); console.log(res46); // 0 const res47 = await client.lPop('bikes:repairs'); console.log(res47); // null const res48 = await client.lPush( 'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5'] ); console.log(res48); // 5 const res49 = await client.lTrim('bikes:repairs', 0, 2); console.log(res49); // 'OK' const res50 = await client.lRange('bikes:repairs', 0, -1); console.log(res50); // ['bike:5', 'bike:4', 'bike:3']
package io.redis.examples; import org.junit.Test; import redis.clients.jedis.UnifiedJedis; import redis.clients.jedis.args.ListDirection; import java.util.List; import static org.junit.Assert.*; public class ListExample { public void run() { UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); long res1 = jedis.lpush("bikes:repairs", "bike:1"); System.out.println(res1); // >>> 1 long res2 = jedis.lpush("bikes:repairs", "bike:2"); System.out.println(res2); // >>> 2 String res3 = jedis.rpop("bikes:repairs"); System.out.println(res3); // >>> bike:1 String res4 = jedis.rpop("bikes:repairs"); System.out.println(res4); // >>> bike:2 long res5 = jedis.lpush("bikes:repairs", "bike:1"); System.out.println(res5); // >>> 1 long res6 = jedis.lpush("bikes:repairs", "bike:2"); System.out.println(res6); // >>> 2 String res7 = jedis.lpop("bikes:repairs"); System.out.println(res7); // >>> bike:2 String res8 = jedis.lpop("bikes:repairs"); System.out.println(res8); // >>> bike:1 long res9 = jedis.llen("bikes:repairs"); System.out.println(res9); // >>> 0 long res10 = jedis.lpush("bikes:repairs", "bike:1"); System.out.println(res10); // >>> 1 long res11 = jedis.lpush("bikes:repairs", "bike:2"); System.out.println(res11); // >>> 2 String res12 = jedis.lmove("bikes:repairs", "bikes:finished", ListDirection.LEFT, ListDirection.LEFT); System.out.println(res12); // >>> bike:2 List<String> res13 = jedis.lrange("bikes:repairs", 0, -1); System.out.println(res13); // >>> [bike:1] List<String> res14 = jedis.lrange("bikes:finished", 0, -1); System.out.println(res14); // >>> [bike:2] long res15 = jedis.rpush("bikes:repairs", "bike:1"); System.out.println(res15); // >>> 1 long res16 = jedis.rpush("bikes:repairs", "bike:2"); System.out.println(res16); // >>> 2 long res17 = jedis.lpush("bikes:repairs", "bike:important_bike"); System.out.println(res17); // >>> 3 List<String> res18 = jedis.lrange("bikes:repairs", 0, -1); System.out.println(res18); // >>> [bike:important_bike, bike:1, bike:2] long res19 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3"); System.out.println(res19); // >>> 3 long res20 = jedis.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike"); System.out.println(res20); // >>> 5 List<String> res21 = jedis.lrange("bikes:repairs", 0, -1); System.out.println(res21); // >>> [bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3] long res22 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3"); System.out.println(res22); // >>> 3 String res23 = jedis.rpop("bikes:repairs"); System.out.println(res23); // >>> bike:3 String res24 = jedis.lpop("bikes:repairs"); System.out.println(res24); // >>> bike:1 String res25 = jedis.rpop("bikes:repairs"); System.out.println(res25); // >>> bike:2 String res26 = jedis.rpop("bikes:repairs"); System.out.println(res26); // >>> null long res27 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5"); System.out.println(res27); // >>> 5 String res28 = jedis.ltrim("bikes:repairs", 0, 2); System.out.println(res28); // >>> OK List<String> res29 = jedis.lrange("bikes:repairs", 0, -1); System.out.println(res29); // >>> [bike:5, bike:4, bike:3] res27 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5"); System.out.println(res27); // >>> 5 res28 = jedis.ltrim("bikes:repairs", -3, -1); System.out.println(res2); // >>> OK res29 = jedis.lrange("bikes:repairs", 0, -1); System.out.println(res29); // >>> [bike:3, bike:4, bike:5] long res31 = jedis.rpush("bikes:repairs", "bike:1", "bike:2"); System.out.println(res31); // >>> 2 List<String> res32 = jedis.brpop(1, "bikes:repairs"); System.out.println(res32); // >>> (bikes:repairs, bike:2) List<String> res33 = jedis.brpop(1,"bikes:repairs"); System.out.println(res33); // >>> (bikes:repairs, bike:1) List<String> res34 = jedis.brpop(1,"bikes:repairs"); System.out.println(res34); // >>> null long res35 = jedis.del("new_bikes"); System.out.println(res35); // >>> 0 long res36 = jedis.lpush("new_bikes", "bike:1", "bike:2", "bike:3"); System.out.println(res36); // >>> 3 String res37 = jedis.set("new_bikes", "bike:1"); System.out.println(res37); // >>> OK String res38 = jedis.type("new_bikes"); System.out.println(res38); // >>> string try { long res39 = jedis.lpush("new_bikes", "bike:2", "bike:3"); } catch (Exception e) { e.printStackTrace(); // >>> redis.clients.jedis.exceptions.JedisDataException: // >>> WRONGTYPE Operation against a key holding the wrong kind of value } jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3"); System.out.println(res36); // >>> 3 boolean res40 = jedis.exists("bikes:repairs"); System.out.println(res40); // >>> true String res41 = jedis.lpop("bikes:repairs"); System.out.println(res41); // >>> bike:3 String res42 = jedis.lpop("bikes:repairs"); System.out.println(res42); // >>> bike:2 String res43 = jedis.lpop("bikes:repairs"); System.out.println(res43); // >>> bike:1 boolean res44 = jedis.exists("bikes:repairs"); System.out.println(res44); // >>> false long res45 = jedis.del("bikes:repairs"); System.out.println(res45); // >>> 0 long res46 = jedis.llen("bikes:repairs"); System.out.println(res46); // >>> 0 String res47 = jedis.lpop("bikes:repairs"); System.out.println(res47); // >>> null long res48 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5"); System.out.println(res48); // >>> 5 String res49 = jedis.ltrim("bikes:repairs", 0, 2); System.out.println(res49); // >>> OK List<String> res50 = jedis.lrange("bikes:repairs", 0, -1); System.out.println(res50); // >>> [bike:5, bike:4, bike:3] } }
package example_commands_test import ( "context" "fmt" "github.com/redis/go-redis/v9" ) func ExampleClient_queue() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res1, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result() if err != nil { panic(err) } fmt.Println(res1) // >>> 1 res2, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result() if err != nil { panic(err) } fmt.Println(res2) // >>> 2 res3, err := rdb.RPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res3) // >>> bike:1 res4, err := rdb.RPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res4) // >>> bike:2 } func ExampleClient_stack() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res5, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result() if err != nil { panic(err) } fmt.Println(res5) // >>> 1 res6, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result() if err != nil { panic(err) } fmt.Println(res6) // >>> 2 res7, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res7) // >>> bike:2 res8, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res8) // >>> bike:1 } func ExampleClient_llen() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res9, err := rdb.LLen(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res9) // >>> 0 } func ExampleClient_lmove_lrange() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res10, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result() if err != nil { panic(err) } fmt.Println(res10) // >>> 1 res11, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result() if err != nil { panic(err) } fmt.Println(res11) // >>> 2 res12, err := rdb.LMove(ctx, "bikes:repairs", "bikes:finished", "LEFT", "LEFT").Result() if err != nil { panic(err) } fmt.Println(res12) // >>> bike:2 res13, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res13) // >>> [bike:1] res14, err := rdb.LRange(ctx, "bikes:finished", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res14) // >>> [bike:2] } func ExampleClient_lpush_rpush() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res15, err := rdb.RPush(ctx, "bikes:repairs", "bike:1").Result() if err != nil { panic(err) } fmt.Println(res15) // >>> 1 res16, err := rdb.RPush(ctx, "bikes:repairs", "bike:2").Result() if err != nil { panic(err) } fmt.Println(res16) // >>> 2 res17, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike").Result() if err != nil { panic(err) } fmt.Println(res17) // >>> 3 res18, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res18) // >>> [bike:important_bike bike:1 bike:2] } func ExampleClient_variadic() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res19, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result() if err != nil { panic(err) } fmt.Println(res19) // >>> 3 res20, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike", "bike:very_important_bike").Result() if err != nil { panic(err) } fmt.Println(res20) // >>> 5 res21, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res21) // >>> [bike:very_important_bike bike:important_bike bike:1 bike:2 bike:3] } func ExampleClient_lpop_rpop() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res22, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result() if err != nil { panic(err) } fmt.Println(res22) // >>> 3 res23, err := rdb.RPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res23) // >>> bike:3 res24, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res24) // >>> bike:1 res25, err := rdb.RPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res25) // >>> bike:2 res26, err := rdb.RPop(ctx, "bikes:repairs").Result() if err != nil { fmt.Println(err) // >>> redis: nil } fmt.Println(res26) // >>> <empty string> } func ExampleClient_ltrim() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res27, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result() if err != nil { panic(err) } fmt.Println(res27) // >>> 5 res28, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result() if err != nil { panic(err) } fmt.Println(res28) // >>> OK res29, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res29) // >>> [bike:5 bike:4 bike:3] } func ExampleClient_ltrim_end_of_list() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res30, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result() if err != nil { panic(err) } fmt.Println(res30) // >>> 5 res31, err := rdb.LTrim(ctx, "bikes:repairs", -3, -1).Result() if err != nil { panic(err) } fmt.Println(res31) // >>> OK res32, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res32) // >>> [bike:3 bike:4 bike:5] } func ExampleClient_brpop() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res33, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2").Result() if err != nil { panic(err) } fmt.Println(res33) // >>> 2 res34, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res34) // >>> [bikes:repairs bike:2] res35, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res35) // >>> [bikes:repairs bike:1] res36, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result() if err != nil { fmt.Println(err) // >>> redis: nil } fmt.Println(res36) // >>> [] } func ExampleClient_rule1() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res37, err := rdb.Del(ctx, "new_bikes").Result() if err != nil { panic(err) } fmt.Println(res37) // >>> 0 res38, err := rdb.LPush(ctx, "new_bikes", "bike:1", "bike:2", "bike:3").Result() if err != nil { panic(err) } fmt.Println(res38) // >>> 3 } func ExampleClient_rule11() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res39, err := rdb.Set(ctx, "new_bikes", "bike:1", 0).Result() if err != nil { panic(err) } fmt.Println(res39) // >>> OK res40, err := rdb.Type(ctx, "new_bikes").Result() if err != nil { panic(err) } fmt.Println(res40) // >>> string res41, err := rdb.LPush(ctx, "new_bikes", "bike:2", "bike:3").Result() if err != nil { fmt.Println(err) // >>> WRONGTYPE Operation against a key holding the wrong kind of value } fmt.Println(res41) } func ExampleClient_rule2() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res42, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result() if err != nil { panic(err) } fmt.Println(res42) // >>> 3 res43, err := rdb.Exists(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res43) // >>> 1 res44, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res44) // >>> bike:3 res45, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res45) // >>> bike:2 res46, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res46) // >>> bike:1 res47, err := rdb.Exists(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res47) // >>> 0 } func ExampleClient_rule3() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res48, err := rdb.Del(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res48) // >>> 0 res49, err := rdb.LLen(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res49) // >>> 0 res50, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { fmt.Println(err) // >>> redis: nil } fmt.Println(res50) // >>> <empty string> } func ExampleClient_ltrim1() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res51, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result() if err != nil { panic(err) } fmt.Println(res51) // >>> 5 res52, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result() if err != nil { panic(err) } fmt.Println(res52) // >>> OK res53, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res53) // >>> [bike:5 bike:4 bike:3] }
public class ListExample { public void run() { var muxer = ConnectionMultiplexer.Connect("localhost:6379"); var db = muxer.GetDatabase(); long res1 = db.ListLeftPush("bikes:repairs", "bike:1"); Console.WriteLine(res1); // >>> 1 long res2 = db.ListLeftPush("bikes:repairs", "bike:2"); Console.WriteLine(res2); // >>> 2 RedisValue res3 = db.ListRightPop("bikes:repairs"); Console.WriteLine(res3); // >>> "bike:1" RedisValue res4 = db.ListRightPop("bikes:repairs"); Console.WriteLine(res4); // >>> "bike:2" long res5 = db.ListLeftPush("bikes:repairs", "bike:1"); Console.WriteLine(res5); // >>> 1 long res6 = db.ListLeftPush("bikes:repairs", "bike:2"); Console.WriteLine(res6); // >>> 2 RedisValue res7 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res7); // >>> "bike:2" RedisValue res8 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res8); // >>> "bike:1" long res9 = db.ListLength("bikes:repairs"); Console.WriteLine(res9); // >>> 0 long res10 = db.ListLeftPush("{bikes}:repairs", "bike:1"); Console.WriteLine(res10); // >>> 1 long res11 = db.ListLeftPush("{bikes}:repairs", "bike:2"); Console.WriteLine(res11); // >>> 2 RedisValue res12 = db.ListMove("{bikes}:repairs", "{bikes}:finished", ListSide.Left, ListSide.Left); Console.Write(res12); // >>> "bike:2" RedisValue[] res13 = db.ListRange("{bikes}:repairs", 0, -1); Console.WriteLine(string.Join(", ", res13)); // >>> "bike:1" RedisValue[] res14 = db.ListRange("{bikes}:finished", 0, -1); Console.WriteLine(string.Join(", ", res14)); // >>> "bike:2" long res15 = db.ListRightPush("bikes:repairs", "bike:1"); Console.WriteLine(res15); // >>> 1 long res16 = db.ListRightPush("bikes:repairs", "bike:2"); Console.WriteLine(res16); // >>> 2 long res17 = db.ListLeftPush("bikes:repairs", "bike:important_bike"); Console.WriteLine(res17); // >>> 3 RedisValue[] res18 = db.ListRange("bikes:repairs", 0, -1); Console.WriteLine(string.Join(", ", res18)); // >>> "bike:important_bike, bike:1, bike:2" long res19 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); Console.WriteLine(res19); // >>> 3 long res20 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:important_bike", "bike:very_important_bike" }); Console.WriteLine(res20); // >>> 5 RedisValue[] res21 = db.ListRange("bikes:repairs", 0, -1); Console.WriteLine(string.Join(", ", res21)); // >>> "bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3" long res22 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); Console.WriteLine(res22); // >>> 3 RedisValue res23 = db.ListRightPop("bikes:repairs"); Console.WriteLine(res23); // >>> "bike:3" RedisValue res24 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res24); // >>> "bike:1" RedisValue res25 = db.ListRightPop("bikes:repairs"); Console.WriteLine(res25); // >>> "bike:2" RedisValue res26 = db.ListRightPop("bikes:repairs"); Console.WriteLine(res26); // >>> <Empty string> long res27 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" }); Console.WriteLine(res27); // >>> 5 db.ListTrim("bikes:repairs", 0, 2); RedisValue[] res28 = db.ListRange("bikes:repairs", 0, -1); Console.WriteLine(string.Join(", ", res28)); // "bike:5, bike:4, bike:3" long res29 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" }); Console.WriteLine(res29); // >>> 5 db.ListTrim("bikes:repairs", -3, -1); RedisValue[] res30 = db.ListRange("bikes:repairs", 0, -1); Console.WriteLine(string.Join(", ", res30)); // >>> "bike:3, bike:4, bike:5" long res31 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2" }); Console.WriteLine(res31); // >>> 2 Tuple<RedisKey, RedisValue>? res32 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1); if (res32 != null) Console.WriteLine($"{res32.Item1} -> {res32.Item2}"); // >>> "bikes:repairs -> bike:2" Tuple<RedisKey, RedisValue>? res33 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1); if (res33 != null) Console.WriteLine($"{res33.Item1} -> {res33.Item2}"); // >>> "bikes:repairs -> bike:1" Tuple<RedisKey, RedisValue>? res34 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1); Console.WriteLine(res34); // >>> "Null" bool res35 = db.KeyDelete("new_bikes"); Console.WriteLine(res35); // >>> False long res36 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); Console.WriteLine(res36); // >>> 3 bool res37 = db.StringSet("new_bikes", "bike:1"); Console.WriteLine(res37); // >>> True RedisType res38 = db.KeyType("new_bikes"); Console.WriteLine(res38); // >>> RedisType.String try { long res39 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:2", "bike:3" }); } catch (Exception e) { Console.WriteLine(e); } long res40 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); Console.WriteLine(res40); // >>> 3 bool res41 = db.KeyExists("bikes:repairs"); Console.WriteLine(res41); // >>> True RedisValue res42 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res42); // >>> "bike:3" RedisValue res43 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res43); // >>> "bike:2" RedisValue res44 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res44); // >>> "bike:1" bool res45 = db.KeyExists("bikes:repairs"); Console.WriteLine(res45); // >>> False bool res46 = db.KeyDelete("bikes:repairs"); Console.WriteLine(res46); // >>> False long res47 = db.ListLength("bikes:repairs"); Console.WriteLine(res47); // >>> 0 RedisValue res48 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res48); // >>> Null long res49 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" }); Console.WriteLine(res49); // >>> 5 db.ListTrim("bikes:repairs", 0, 2); RedisValue[] res50 = db.ListRange("bikes:repairs", 0, -1); Console.WriteLine(string.Join(", ", res50)); // >>> "bike:5, bike:4, bike:3" } }
-
Atomically pop an element from one list and push to another:
> LPUSH bikes:repairs bike:1 (integer) 1 > LPUSH bikes:repairs bike:2 (integer) 2 > LMOVE bikes:repairs bikes:finished LEFT LEFT "bike:2" > LRANGE bikes:repairs 0 -1 1) "bike:1" > LRANGE bikes:finished 0 -1 1) "bike:2"
Are you tired of using redis-cli? Try Redis Insight - the developer GUI for Redis.""" Code samples for List doc pages: https://redis.io/docs/latest/develop/data-types/lists/ """ import redis r = redis.Redis(decode_responses=True) res1 = r.lpush("bikes:repairs", "bike:1") print(res1) # >>> 1 res2 = r.lpush("bikes:repairs", "bike:2") print(res2) # >>> 2 res3 = r.rpop("bikes:repairs") print(res3) # >>> bike:1 res4 = r.rpop("bikes:repairs") print(res4) # >>> bike:2 res5 = r.lpush("bikes:repairs", "bike:1") print(res5) # >>> 1 res6 = r.lpush("bikes:repairs", "bike:2") print(res6) # >>> 2 res7 = r.lpop("bikes:repairs") print(res7) # >>> bike:2 res8 = r.lpop("bikes:repairs") print(res8) # >>> bike:1 res9 = r.llen("bikes:repairs") print(res9) # >>> 0 res10 = r.lpush("bikes:repairs", "bike:1") print(res10) # >>> 1 res11 = r.lpush("bikes:repairs", "bike:2") print(res11) # >>> 2 res12 = r.lmove("bikes:repairs", "bikes:finished", "LEFT", "LEFT") print(res12) # >>> 'bike:2' res13 = r.lrange("bikes:repairs", 0, -1) print(res13) # >>> ['bike:1'] res14 = r.lrange("bikes:finished", 0, -1) print(res14) # >>> ['bike:2'] res15 = r.rpush("bikes:repairs", "bike:1") print(res15) # >>> 1 res16 = r.rpush("bikes:repairs", "bike:2") print(res16) # >>> 2 res17 = r.lpush("bikes:repairs", "bike:important_bike") print(res17) # >>> 3 res18 = r.lrange("bikes:repairs", 0, -1) print(res18) # >>> ['bike:important_bike', 'bike:1', 'bike:2'] res19 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3") print(res19) # >>> 3 res20 = r.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike") print(res20) # >>> 5 res21 = r.lrange("bikes:repairs", 0, -1) print( res21 ) # >>> ['bike:very_important_bike', 'bike:important_bike', 'bike:1', ... res22 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3") print(res22) # >>> 3 res23 = r.rpop("bikes:repairs") print(res23) # >>> 'bike:3' res24 = r.lpop("bikes:repairs") print(res24) # >>> 'bike:1' res25 = r.rpop("bikes:repairs") print(res25) # >>> 'bike:2' res26 = r.rpop("bikes:repairs") print(res26) # >>> None res27 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5") print(res27) # >>> 5 res28 = r.ltrim("bikes:repairs", 0, 2) print(res28) # >>> True res29 = r.lrange("bikes:repairs", 0, -1) print(res29) # >>> ['bike:5', 'bike:4', 'bike:3'] res27 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5") print(res27) # >>> 5 res28 = r.ltrim("bikes:repairs", -3, -1) print(res28) # >>> True res29 = r.lrange("bikes:repairs", 0, -1) print(res29) # >>> ['bike:3', 'bike:4', 'bike:5'] res31 = r.rpush("bikes:repairs", "bike:1", "bike:2") print(res31) # >>> 2 res32 = r.brpop("bikes:repairs", timeout=1) print(res32) # >>> ('bikes:repairs', 'bike:2') res33 = r.brpop("bikes:repairs", timeout=1) print(res33) # >>> ('bikes:repairs', 'bike:1') res34 = r.brpop("bikes:repairs", timeout=1) print(res34) # >>> None res35 = r.delete("new_bikes") print(res35) # >>> 0 res36 = r.lpush("new_bikes", "bike:1", "bike:2", "bike:3") print(res36) # >>> 3 res37 = r.set("new_bikes", "bike:1") print(res37) # >>> True res38 = r.type("new_bikes") print(res38) # >>> 'string' try: res39 = r.lpush("new_bikes", "bike:2", "bike:3") # >>> redis.exceptions.ResponseError: # >>> WRONGTYPE Operation against a key holding the wrong kind of value except redis.exceptions.ResponseError as e: print(e) r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3") print(res36) # >>> 3 res40 = r.exists("bikes:repairs") print(res40) # >>> 1 res41 = r.lpop("bikes:repairs") print(res41) # >>> 'bike:3' res42 = r.lpop("bikes:repairs") print(res42) # >>> 'bike:2' res43 = r.lpop("bikes:repairs") print(res43) # >>> 'bike:1' res44 = r.exists("bikes:repairs") print(res44) # >>> False res45 = r.delete("bikes:repairs") print(res45) # >>> 0 res46 = r.llen("bikes:repairs") print(res46) # >>> 0 res47 = r.lpop("bikes:repairs") print(res47) # >>> None res48 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5") print(res48) # >>> 5 res49 = r.ltrim("bikes:repairs", 0, 2) print(res49) # >>> True res50 = r.lrange("bikes:repairs", 0, -1) print(res50) # >>> ['bike:5', 'bike:4', 'bike:3']
import assert from 'assert'; import { createClient } from 'redis'; const client = await createClient(); await client.connect(); const res1 = await client.lPush('bikes:repairs', 'bike:1'); console.log(res1); // 1 const res2 = await client.lPush('bikes:repairs', 'bike:2'); console.log(res2); // 2 const res3 = await client.rPop('bikes:repairs'); console.log(res3); // bike:1 const res4 = await client.rPop('bikes:repairs'); console.log(res4); // bike:2 const res5 = await client.lPush('bikes:repairs', 'bike:1'); console.log(res5); // 1 const res6 = await client.lPush('bikes:repairs', 'bike:2'); console.log(res6); // 2 const res7 = await client.lPop('bikes:repairs'); console.log(res7); // bike:2 const res8 = await client.lPop('bikes:repairs'); console.log(res8); // bike:1 const res9 = await client.lLen('bikes:repairs'); console.log(res9); // 0 const res10 = await client.lPush('bikes:repairs', 'bike:1'); console.log(res10); // 1 const res11 = await client.lPush('bikes:repairs', 'bike:2'); console.log(res11); // 2 const res12 = await client.lMove('bikes:repairs', 'bikes:finished', 'LEFT', 'LEFT'); console.log(res12); // 'bike:2' const res13 = await client.lRange('bikes:repairs', 0, -1); console.log(res13); // ['bike:1'] const res14 = await client.lRange('bikes:finished', 0, -1); console.log(res14); // ['bike:2'] const res15 = await client.rPush('bikes:repairs', 'bike:1'); console.log(res15); // 1 const res16 = await client.rPush('bikes:repairs', 'bike:2'); console.log(res16); // 2 const res17 = await client.lPush('bikes:repairs', 'bike:important_bike'); console.log(res17); // 3 const res18 = await client.lRange('bikes:repairs', 0, -1); console.log(res18); // ['bike:important_bike', 'bike:1', 'bike:2'] const res19 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']); console.log(res19); // 3 const res20 = await client.lPush( 'bikes:repairs', ['bike:important_bike', 'bike:very_important_bike'] ); console.log(res20); // 5 const res21 = await client.lRange('bikes:repairs', 0, -1); console.log(res21); // ['bike:very_important_bike', 'bike:important_bike', 'bike:1', 'bike:2', 'bike:3'] const res22 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']); console.log(res22); // 3 const res23 = await client.rPop('bikes:repairs'); console.log(res23); // 'bike:3' const res24 = await client.lPop('bikes:repairs'); console.log(res24); // 'bike:1' const res25 = await client.rPop('bikes:repairs'); console.log(res25); // 'bike:2' const res26 = await client.rPop('bikes:repairs'); console.log(res26); // None const res27 = await client.lPush( 'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5'] ); console.log(res27); // 5 const res28 = await client.lTrim('bikes:repairs', 0, 2); console.log(res28); // true const res29 = await client.lRange('bikes:repairs', 0, -1); console.log(res29); // ['bike:5', 'bike:4', 'bike:3'] const res27eol = await client.rPush( 'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5'] ); console.log(res27eol); // 5 const res28eol = await client.lTrim('bikes:repairs', -3, -1); console.log(res28eol); // 'OK' const res29eol = await client.lRange('bikes:repairs', 0, -1); console.log(res29eol); // ['bike:3', 'bike:4', 'bike:5'] const res31 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2']); console.log(res31); // 2 const res32 = await client.brPop('bikes:repairs', 1); console.log(res32); // { key: 'bikes:repairs', element: 'bike:2' } const res33 = await client.brPop('bikes:repairs', 1); console.log(res33); // { key: 'bikes:repairs', element: 'bike:1' } const res34 = await client.brPop('bikes:repairs', 1); console.log(res34); // null const res35 = await client.del('new_bikes'); console.log(res35); // 0 const res36 = await client.lPush('new_bikes', ['bike:1', 'bike:2', 'bike:3']); console.log(res36); // 3 const res37 = await client.set('new_bikes', 'bike:1'); console.log(res37); // 'OK' const res38 = await client.type('new_bikes'); console.log(res38); // 'string' try { const res39 = await client.lPush('new_bikes', 'bike:2', 'bike:3'); // redis.exceptions.ResponseError: // [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value] } catch(e){ console.log(e); } await client.lPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']); console.log(res36); // 3 const res40 = await client.exists('bikes:repairs') console.log(res40); // true const res41 = await client.lPop('bikes:repairs'); console.log(res41); // 'bike:3' const res42 = await client.lPop('bikes:repairs'); console.log(res42); // 'bike:2' const res43 = await client.lPop('bikes:repairs'); console.log(res43); // 'bike:1' const res44 = await client.exists('bikes:repairs'); console.log(res44); // 0 const res45 = await client.del('bikes:repairs'); console.log(res45); // 0 const res46 = await client.lLen('bikes:repairs'); console.log(res46); // 0 const res47 = await client.lPop('bikes:repairs'); console.log(res47); // null const res48 = await client.lPush( 'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5'] ); console.log(res48); // 5 const res49 = await client.lTrim('bikes:repairs', 0, 2); console.log(res49); // 'OK' const res50 = await client.lRange('bikes:repairs', 0, -1); console.log(res50); // ['bike:5', 'bike:4', 'bike:3']
package io.redis.examples; import org.junit.Test; import redis.clients.jedis.UnifiedJedis; import redis.clients.jedis.args.ListDirection; import java.util.List; import static org.junit.Assert.*; public class ListExample { public void run() { UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); long res1 = jedis.lpush("bikes:repairs", "bike:1"); System.out.println(res1); // >>> 1 long res2 = jedis.lpush("bikes:repairs", "bike:2"); System.out.println(res2); // >>> 2 String res3 = jedis.rpop("bikes:repairs"); System.out.println(res3); // >>> bike:1 String res4 = jedis.rpop("bikes:repairs"); System.out.println(res4); // >>> bike:2 long res5 = jedis.lpush("bikes:repairs", "bike:1"); System.out.println(res5); // >>> 1 long res6 = jedis.lpush("bikes:repairs", "bike:2"); System.out.println(res6); // >>> 2 String res7 = jedis.lpop("bikes:repairs"); System.out.println(res7); // >>> bike:2 String res8 = jedis.lpop("bikes:repairs"); System.out.println(res8); // >>> bike:1 long res9 = jedis.llen("bikes:repairs"); System.out.println(res9); // >>> 0 long res10 = jedis.lpush("bikes:repairs", "bike:1"); System.out.println(res10); // >>> 1 long res11 = jedis.lpush("bikes:repairs", "bike:2"); System.out.println(res11); // >>> 2 String res12 = jedis.lmove("bikes:repairs", "bikes:finished", ListDirection.LEFT, ListDirection.LEFT); System.out.println(res12); // >>> bike:2 List<String> res13 = jedis.lrange("bikes:repairs", 0, -1); System.out.println(res13); // >>> [bike:1] List<String> res14 = jedis.lrange("bikes:finished", 0, -1); System.out.println(res14); // >>> [bike:2] long res15 = jedis.rpush("bikes:repairs", "bike:1"); System.out.println(res15); // >>> 1 long res16 = jedis.rpush("bikes:repairs", "bike:2"); System.out.println(res16); // >>> 2 long res17 = jedis.lpush("bikes:repairs", "bike:important_bike"); System.out.println(res17); // >>> 3 List<String> res18 = jedis.lrange("bikes:repairs", 0, -1); System.out.println(res18); // >>> [bike:important_bike, bike:1, bike:2] long res19 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3"); System.out.println(res19); // >>> 3 long res20 = jedis.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike"); System.out.println(res20); // >>> 5 List<String> res21 = jedis.lrange("bikes:repairs", 0, -1); System.out.println(res21); // >>> [bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3] long res22 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3"); System.out.println(res22); // >>> 3 String res23 = jedis.rpop("bikes:repairs"); System.out.println(res23); // >>> bike:3 String res24 = jedis.lpop("bikes:repairs"); System.out.println(res24); // >>> bike:1 String res25 = jedis.rpop("bikes:repairs"); System.out.println(res25); // >>> bike:2 String res26 = jedis.rpop("bikes:repairs"); System.out.println(res26); // >>> null long res27 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5"); System.out.println(res27); // >>> 5 String res28 = jedis.ltrim("bikes:repairs", 0, 2); System.out.println(res28); // >>> OK List<String> res29 = jedis.lrange("bikes:repairs", 0, -1); System.out.println(res29); // >>> [bike:5, bike:4, bike:3] res27 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5"); System.out.println(res27); // >>> 5 res28 = jedis.ltrim("bikes:repairs", -3, -1); System.out.println(res2); // >>> OK res29 = jedis.lrange("bikes:repairs", 0, -1); System.out.println(res29); // >>> [bike:3, bike:4, bike:5] long res31 = jedis.rpush("bikes:repairs", "bike:1", "bike:2"); System.out.println(res31); // >>> 2 List<String> res32 = jedis.brpop(1, "bikes:repairs"); System.out.println(res32); // >>> (bikes:repairs, bike:2) List<String> res33 = jedis.brpop(1,"bikes:repairs"); System.out.println(res33); // >>> (bikes:repairs, bike:1) List<String> res34 = jedis.brpop(1,"bikes:repairs"); System.out.println(res34); // >>> null long res35 = jedis.del("new_bikes"); System.out.println(res35); // >>> 0 long res36 = jedis.lpush("new_bikes", "bike:1", "bike:2", "bike:3"); System.out.println(res36); // >>> 3 String res37 = jedis.set("new_bikes", "bike:1"); System.out.println(res37); // >>> OK String res38 = jedis.type("new_bikes"); System.out.println(res38); // >>> string try { long res39 = jedis.lpush("new_bikes", "bike:2", "bike:3"); } catch (Exception e) { e.printStackTrace(); // >>> redis.clients.jedis.exceptions.JedisDataException: // >>> WRONGTYPE Operation against a key holding the wrong kind of value } jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3"); System.out.println(res36); // >>> 3 boolean res40 = jedis.exists("bikes:repairs"); System.out.println(res40); // >>> true String res41 = jedis.lpop("bikes:repairs"); System.out.println(res41); // >>> bike:3 String res42 = jedis.lpop("bikes:repairs"); System.out.println(res42); // >>> bike:2 String res43 = jedis.lpop("bikes:repairs"); System.out.println(res43); // >>> bike:1 boolean res44 = jedis.exists("bikes:repairs"); System.out.println(res44); // >>> false long res45 = jedis.del("bikes:repairs"); System.out.println(res45); // >>> 0 long res46 = jedis.llen("bikes:repairs"); System.out.println(res46); // >>> 0 String res47 = jedis.lpop("bikes:repairs"); System.out.println(res47); // >>> null long res48 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5"); System.out.println(res48); // >>> 5 String res49 = jedis.ltrim("bikes:repairs", 0, 2); System.out.println(res49); // >>> OK List<String> res50 = jedis.lrange("bikes:repairs", 0, -1); System.out.println(res50); // >>> [bike:5, bike:4, bike:3] } }
package example_commands_test import ( "context" "fmt" "github.com/redis/go-redis/v9" ) func ExampleClient_queue() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res1, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result() if err != nil { panic(err) } fmt.Println(res1) // >>> 1 res2, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result() if err != nil { panic(err) } fmt.Println(res2) // >>> 2 res3, err := rdb.RPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res3) // >>> bike:1 res4, err := rdb.RPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res4) // >>> bike:2 } func ExampleClient_stack() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res5, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result() if err != nil { panic(err) } fmt.Println(res5) // >>> 1 res6, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result() if err != nil { panic(err) } fmt.Println(res6) // >>> 2 res7, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res7) // >>> bike:2 res8, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res8) // >>> bike:1 } func ExampleClient_llen() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res9, err := rdb.LLen(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res9) // >>> 0 } func ExampleClient_lmove_lrange() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res10, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result() if err != nil { panic(err) } fmt.Println(res10) // >>> 1 res11, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result() if err != nil { panic(err) } fmt.Println(res11) // >>> 2 res12, err := rdb.LMove(ctx, "bikes:repairs", "bikes:finished", "LEFT", "LEFT").Result() if err != nil { panic(err) } fmt.Println(res12) // >>> bike:2 res13, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res13) // >>> [bike:1] res14, err := rdb.LRange(ctx, "bikes:finished", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res14) // >>> [bike:2] } func ExampleClient_lpush_rpush() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res15, err := rdb.RPush(ctx, "bikes:repairs", "bike:1").Result() if err != nil { panic(err) } fmt.Println(res15) // >>> 1 res16, err := rdb.RPush(ctx, "bikes:repairs", "bike:2").Result() if err != nil { panic(err) } fmt.Println(res16) // >>> 2 res17, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike").Result() if err != nil { panic(err) } fmt.Println(res17) // >>> 3 res18, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res18) // >>> [bike:important_bike bike:1 bike:2] } func ExampleClient_variadic() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res19, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result() if err != nil { panic(err) } fmt.Println(res19) // >>> 3 res20, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike", "bike:very_important_bike").Result() if err != nil { panic(err) } fmt.Println(res20) // >>> 5 res21, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res21) // >>> [bike:very_important_bike bike:important_bike bike:1 bike:2 bike:3] } func ExampleClient_lpop_rpop() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res22, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result() if err != nil { panic(err) } fmt.Println(res22) // >>> 3 res23, err := rdb.RPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res23) // >>> bike:3 res24, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res24) // >>> bike:1 res25, err := rdb.RPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res25) // >>> bike:2 res26, err := rdb.RPop(ctx, "bikes:repairs").Result() if err != nil { fmt.Println(err) // >>> redis: nil } fmt.Println(res26) // >>> <empty string> } func ExampleClient_ltrim() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res27, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result() if err != nil { panic(err) } fmt.Println(res27) // >>> 5 res28, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result() if err != nil { panic(err) } fmt.Println(res28) // >>> OK res29, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res29) // >>> [bike:5 bike:4 bike:3] } func ExampleClient_ltrim_end_of_list() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res30, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result() if err != nil { panic(err) } fmt.Println(res30) // >>> 5 res31, err := rdb.LTrim(ctx, "bikes:repairs", -3, -1).Result() if err != nil { panic(err) } fmt.Println(res31) // >>> OK res32, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res32) // >>> [bike:3 bike:4 bike:5] } func ExampleClient_brpop() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res33, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2").Result() if err != nil { panic(err) } fmt.Println(res33) // >>> 2 res34, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res34) // >>> [bikes:repairs bike:2] res35, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res35) // >>> [bikes:repairs bike:1] res36, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result() if err != nil { fmt.Println(err) // >>> redis: nil } fmt.Println(res36) // >>> [] } func ExampleClient_rule1() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res37, err := rdb.Del(ctx, "new_bikes").Result() if err != nil { panic(err) } fmt.Println(res37) // >>> 0 res38, err := rdb.LPush(ctx, "new_bikes", "bike:1", "bike:2", "bike:3").Result() if err != nil { panic(err) } fmt.Println(res38) // >>> 3 } func ExampleClient_rule11() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res39, err := rdb.Set(ctx, "new_bikes", "bike:1", 0).Result() if err != nil { panic(err) } fmt.Println(res39) // >>> OK res40, err := rdb.Type(ctx, "new_bikes").Result() if err != nil { panic(err) } fmt.Println(res40) // >>> string res41, err := rdb.LPush(ctx, "new_bikes", "bike:2", "bike:3").Result() if err != nil { fmt.Println(err) // >>> WRONGTYPE Operation against a key holding the wrong kind of value } fmt.Println(res41) } func ExampleClient_rule2() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res42, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result() if err != nil { panic(err) } fmt.Println(res42) // >>> 3 res43, err := rdb.Exists(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res43) // >>> 1 res44, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res44) // >>> bike:3 res45, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res45) // >>> bike:2 res46, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res46) // >>> bike:1 res47, err := rdb.Exists(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res47) // >>> 0 } func ExampleClient_rule3() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res48, err := rdb.Del(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res48) // >>> 0 res49, err := rdb.LLen(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res49) // >>> 0 res50, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { fmt.Println(err) // >>> redis: nil } fmt.Println(res50) // >>> <empty string> } func ExampleClient_ltrim1() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res51, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result() if err != nil { panic(err) } fmt.Println(res51) // >>> 5 res52, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result() if err != nil { panic(err) } fmt.Println(res52) // >>> OK res53, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res53) // >>> [bike:5 bike:4 bike:3] }
public class ListExample { public void run() { var muxer = ConnectionMultiplexer.Connect("localhost:6379"); var db = muxer.GetDatabase(); long res1 = db.ListLeftPush("bikes:repairs", "bike:1"); Console.WriteLine(res1); // >>> 1 long res2 = db.ListLeftPush("bikes:repairs", "bike:2"); Console.WriteLine(res2); // >>> 2 RedisValue res3 = db.ListRightPop("bikes:repairs"); Console.WriteLine(res3); // >>> "bike:1" RedisValue res4 = db.ListRightPop("bikes:repairs"); Console.WriteLine(res4); // >>> "bike:2" long res5 = db.ListLeftPush("bikes:repairs", "bike:1"); Console.WriteLine(res5); // >>> 1 long res6 = db.ListLeftPush("bikes:repairs", "bike:2"); Console.WriteLine(res6); // >>> 2 RedisValue res7 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res7); // >>> "bike:2" RedisValue res8 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res8); // >>> "bike:1" long res9 = db.ListLength("bikes:repairs"); Console.WriteLine(res9); // >>> 0 long res10 = db.ListLeftPush("{bikes}:repairs", "bike:1"); Console.WriteLine(res10); // >>> 1 long res11 = db.ListLeftPush("{bikes}:repairs", "bike:2"); Console.WriteLine(res11); // >>> 2 RedisValue res12 = db.ListMove("{bikes}:repairs", "{bikes}:finished", ListSide.Left, ListSide.Left); Console.Write(res12); // >>> "bike:2" RedisValue[] res13 = db.ListRange("{bikes}:repairs", 0, -1); Console.WriteLine(string.Join(", ", res13)); // >>> "bike:1" RedisValue[] res14 = db.ListRange("{bikes}:finished", 0, -1); Console.WriteLine(string.Join(", ", res14)); // >>> "bike:2" long res15 = db.ListRightPush("bikes:repairs", "bike:1"); Console.WriteLine(res15); // >>> 1 long res16 = db.ListRightPush("bikes:repairs", "bike:2"); Console.WriteLine(res16); // >>> 2 long res17 = db.ListLeftPush("bikes:repairs", "bike:important_bike"); Console.WriteLine(res17); // >>> 3 RedisValue[] res18 = db.ListRange("bikes:repairs", 0, -1); Console.WriteLine(string.Join(", ", res18)); // >>> "bike:important_bike, bike:1, bike:2" long res19 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); Console.WriteLine(res19); // >>> 3 long res20 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:important_bike", "bike:very_important_bike" }); Console.WriteLine(res20); // >>> 5 RedisValue[] res21 = db.ListRange("bikes:repairs", 0, -1); Console.WriteLine(string.Join(", ", res21)); // >>> "bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3" long res22 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); Console.WriteLine(res22); // >>> 3 RedisValue res23 = db.ListRightPop("bikes:repairs"); Console.WriteLine(res23); // >>> "bike:3" RedisValue res24 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res24); // >>> "bike:1" RedisValue res25 = db.ListRightPop("bikes:repairs"); Console.WriteLine(res25); // >>> "bike:2" RedisValue res26 = db.ListRightPop("bikes:repairs"); Console.WriteLine(res26); // >>> <Empty string> long res27 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" }); Console.WriteLine(res27); // >>> 5 db.ListTrim("bikes:repairs", 0, 2); RedisValue[] res28 = db.ListRange("bikes:repairs", 0, -1); Console.WriteLine(string.Join(", ", res28)); // "bike:5, bike:4, bike:3" long res29 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" }); Console.WriteLine(res29); // >>> 5 db.ListTrim("bikes:repairs", -3, -1); RedisValue[] res30 = db.ListRange("bikes:repairs", 0, -1); Console.WriteLine(string.Join(", ", res30)); // >>> "bike:3, bike:4, bike:5" long res31 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2" }); Console.WriteLine(res31); // >>> 2 Tuple<RedisKey, RedisValue>? res32 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1); if (res32 != null) Console.WriteLine($"{res32.Item1} -> {res32.Item2}"); // >>> "bikes:repairs -> bike:2" Tuple<RedisKey, RedisValue>? res33 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1); if (res33 != null) Console.WriteLine($"{res33.Item1} -> {res33.Item2}"); // >>> "bikes:repairs -> bike:1" Tuple<RedisKey, RedisValue>? res34 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1); Console.WriteLine(res34); // >>> "Null" bool res35 = db.KeyDelete("new_bikes"); Console.WriteLine(res35); // >>> False long res36 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); Console.WriteLine(res36); // >>> 3 bool res37 = db.StringSet("new_bikes", "bike:1"); Console.WriteLine(res37); // >>> True RedisType res38 = db.KeyType("new_bikes"); Console.WriteLine(res38); // >>> RedisType.String try { long res39 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:2", "bike:3" }); } catch (Exception e) { Console.WriteLine(e); } long res40 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); Console.WriteLine(res40); // >>> 3 bool res41 = db.KeyExists("bikes:repairs"); Console.WriteLine(res41); // >>> True RedisValue res42 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res42); // >>> "bike:3" RedisValue res43 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res43); // >>> "bike:2" RedisValue res44 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res44); // >>> "bike:1" bool res45 = db.KeyExists("bikes:repairs"); Console.WriteLine(res45); // >>> False bool res46 = db.KeyDelete("bikes:repairs"); Console.WriteLine(res46); // >>> False long res47 = db.ListLength("bikes:repairs"); Console.WriteLine(res47); // >>> 0 RedisValue res48 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res48); // >>> Null long res49 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" }); Console.WriteLine(res49); // >>> 5 db.ListTrim("bikes:repairs", 0, 2); RedisValue[] res50 = db.ListRange("bikes:repairs", 0, -1); Console.WriteLine(string.Join(", ", res50)); // >>> "bike:5, bike:4, bike:3" } }
-
To limit the length of a list you can call
LTRIM
:> RPUSH bikes:repairs bike:1 bike:2 bike:3 bike:4 bike:5 (integer) 5 > LTRIM bikes:repairs 0 2 OK > LRANGE bikes:repairs 0 -1 1) "bike:1" 2) "bike:2" 3) "bike:3"
Are you tired of using redis-cli? Try Redis Insight - the developer GUI for Redis.""" Code samples for List doc pages: https://redis.io/docs/latest/develop/data-types/lists/ """ import redis r = redis.Redis(decode_responses=True) res1 = r.lpush("bikes:repairs", "bike:1") print(res1) # >>> 1 res2 = r.lpush("bikes:repairs", "bike:2") print(res2) # >>> 2 res3 = r.rpop("bikes:repairs") print(res3) # >>> bike:1 res4 = r.rpop("bikes:repairs") print(res4) # >>> bike:2 res5 = r.lpush("bikes:repairs", "bike:1") print(res5) # >>> 1 res6 = r.lpush("bikes:repairs", "bike:2") print(res6) # >>> 2 res7 = r.lpop("bikes:repairs") print(res7) # >>> bike:2 res8 = r.lpop("bikes:repairs") print(res8) # >>> bike:1 res9 = r.llen("bikes:repairs") print(res9) # >>> 0 res10 = r.lpush("bikes:repairs", "bike:1") print(res10) # >>> 1 res11 = r.lpush("bikes:repairs", "bike:2") print(res11) # >>> 2 res12 = r.lmove("bikes:repairs", "bikes:finished", "LEFT", "LEFT") print(res12) # >>> 'bike:2' res13 = r.lrange("bikes:repairs", 0, -1) print(res13) # >>> ['bike:1'] res14 = r.lrange("bikes:finished", 0, -1) print(res14) # >>> ['bike:2'] res15 = r.rpush("bikes:repairs", "bike:1") print(res15) # >>> 1 res16 = r.rpush("bikes:repairs", "bike:2") print(res16) # >>> 2 res17 = r.lpush("bikes:repairs", "bike:important_bike") print(res17) # >>> 3 res18 = r.lrange("bikes:repairs", 0, -1) print(res18) # >>> ['bike:important_bike', 'bike:1', 'bike:2'] res19 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3") print(res19) # >>> 3 res20 = r.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike") print(res20) # >>> 5 res21 = r.lrange("bikes:repairs", 0, -1) print( res21 ) # >>> ['bike:very_important_bike', 'bike:important_bike', 'bike:1', ... res22 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3") print(res22) # >>> 3 res23 = r.rpop("bikes:repairs") print(res23) # >>> 'bike:3' res24 = r.lpop("bikes:repairs") print(res24) # >>> 'bike:1' res25 = r.rpop("bikes:repairs") print(res25) # >>> 'bike:2' res26 = r.rpop("bikes:repairs") print(res26) # >>> None res27 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5") print(res27) # >>> 5 res28 = r.ltrim("bikes:repairs", 0, 2) print(res28) # >>> True res29 = r.lrange("bikes:repairs", 0, -1) print(res29) # >>> ['bike:5', 'bike:4', 'bike:3'] res27 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5") print(res27) # >>> 5 res28 = r.ltrim("bikes:repairs", -3, -1) print(res28) # >>> True res29 = r.lrange("bikes:repairs", 0, -1) print(res29) # >>> ['bike:3', 'bike:4', 'bike:5'] res31 = r.rpush("bikes:repairs", "bike:1", "bike:2") print(res31) # >>> 2 res32 = r.brpop("bikes:repairs", timeout=1) print(res32) # >>> ('bikes:repairs', 'bike:2') res33 = r.brpop("bikes:repairs", timeout=1) print(res33) # >>> ('bikes:repairs', 'bike:1') res34 = r.brpop("bikes:repairs", timeout=1) print(res34) # >>> None res35 = r.delete("new_bikes") print(res35) # >>> 0 res36 = r.lpush("new_bikes", "bike:1", "bike:2", "bike:3") print(res36) # >>> 3 res37 = r.set("new_bikes", "bike:1") print(res37) # >>> True res38 = r.type("new_bikes") print(res38) # >>> 'string' try: res39 = r.lpush("new_bikes", "bike:2", "bike:3") # >>> redis.exceptions.ResponseError: # >>> WRONGTYPE Operation against a key holding the wrong kind of value except redis.exceptions.ResponseError as e: print(e) r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3") print(res36) # >>> 3 res40 = r.exists("bikes:repairs") print(res40) # >>> 1 res41 = r.lpop("bikes:repairs") print(res41) # >>> 'bike:3' res42 = r.lpop("bikes:repairs") print(res42) # >>> 'bike:2' res43 = r.lpop("bikes:repairs") print(res43) # >>> 'bike:1' res44 = r.exists("bikes:repairs") print(res44) # >>> False res45 = r.delete("bikes:repairs") print(res45) # >>> 0 res46 = r.llen("bikes:repairs") print(res46) # >>> 0 res47 = r.lpop("bikes:repairs") print(res47) # >>> None res48 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5") print(res48) # >>> 5 res49 = r.ltrim("bikes:repairs", 0, 2) print(res49) # >>> True res50 = r.lrange("bikes:repairs", 0, -1) print(res50) # >>> ['bike:5', 'bike:4', 'bike:3']
import assert from 'assert'; import { createClient } from 'redis'; const client = await createClient(); await client.connect(); const res1 = await client.lPush('bikes:repairs', 'bike:1'); console.log(res1); // 1 const res2 = await client.lPush('bikes:repairs', 'bike:2'); console.log(res2); // 2 const res3 = await client.rPop('bikes:repairs'); console.log(res3); // bike:1 const res4 = await client.rPop('bikes:repairs'); console.log(res4); // bike:2 const res5 = await client.lPush('bikes:repairs', 'bike:1'); console.log(res5); // 1 const res6 = await client.lPush('bikes:repairs', 'bike:2'); console.log(res6); // 2 const res7 = await client.lPop('bikes:repairs'); console.log(res7); // bike:2 const res8 = await client.lPop('bikes:repairs'); console.log(res8); // bike:1 const res9 = await client.lLen('bikes:repairs'); console.log(res9); // 0 const res10 = await client.lPush('bikes:repairs', 'bike:1'); console.log(res10); // 1 const res11 = await client.lPush('bikes:repairs', 'bike:2'); console.log(res11); // 2 const res12 = await client.lMove('bikes:repairs', 'bikes:finished', 'LEFT', 'LEFT'); console.log(res12); // 'bike:2' const res13 = await client.lRange('bikes:repairs', 0, -1); console.log(res13); // ['bike:1'] const res14 = await client.lRange('bikes:finished', 0, -1); console.log(res14); // ['bike:2'] const res15 = await client.rPush('bikes:repairs', 'bike:1'); console.log(res15); // 1 const res16 = await client.rPush('bikes:repairs', 'bike:2'); console.log(res16); // 2 const res17 = await client.lPush('bikes:repairs', 'bike:important_bike'); console.log(res17); // 3 const res18 = await client.lRange('bikes:repairs', 0, -1); console.log(res18); // ['bike:important_bike', 'bike:1', 'bike:2'] const res19 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']); console.log(res19); // 3 const res20 = await client.lPush( 'bikes:repairs', ['bike:important_bike', 'bike:very_important_bike'] ); console.log(res20); // 5 const res21 = await client.lRange('bikes:repairs', 0, -1); console.log(res21); // ['bike:very_important_bike', 'bike:important_bike', 'bike:1', 'bike:2', 'bike:3'] const res22 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']); console.log(res22); // 3 const res23 = await client.rPop('bikes:repairs'); console.log(res23); // 'bike:3' const res24 = await client.lPop('bikes:repairs'); console.log(res24); // 'bike:1' const res25 = await client.rPop('bikes:repairs'); console.log(res25); // 'bike:2' const res26 = await client.rPop('bikes:repairs'); console.log(res26); // None const res27 = await client.lPush( 'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5'] ); console.log(res27); // 5 const res28 = await client.lTrim('bikes:repairs', 0, 2); console.log(res28); // true const res29 = await client.lRange('bikes:repairs', 0, -1); console.log(res29); // ['bike:5', 'bike:4', 'bike:3'] const res27eol = await client.rPush( 'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5'] ); console.log(res27eol); // 5 const res28eol = await client.lTrim('bikes:repairs', -3, -1); console.log(res28eol); // 'OK' const res29eol = await client.lRange('bikes:repairs', 0, -1); console.log(res29eol); // ['bike:3', 'bike:4', 'bike:5'] const res31 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2']); console.log(res31); // 2 const res32 = await client.brPop('bikes:repairs', 1); console.log(res32); // { key: 'bikes:repairs', element: 'bike:2' } const res33 = await client.brPop('bikes:repairs', 1); console.log(res33); // { key: 'bikes:repairs', element: 'bike:1' } const res34 = await client.brPop('bikes:repairs', 1); console.log(res34); // null const res35 = await client.del('new_bikes'); console.log(res35); // 0 const res36 = await client.lPush('new_bikes', ['bike:1', 'bike:2', 'bike:3']); console.log(res36); // 3 const res37 = await client.set('new_bikes', 'bike:1'); console.log(res37); // 'OK' const res38 = await client.type('new_bikes'); console.log(res38); // 'string' try { const res39 = await client.lPush('new_bikes', 'bike:2', 'bike:3'); // redis.exceptions.ResponseError: // [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value] } catch(e){ console.log(e); } await client.lPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']); console.log(res36); // 3 const res40 = await client.exists('bikes:repairs') console.log(res40); // true const res41 = await client.lPop('bikes:repairs'); console.log(res41); // 'bike:3' const res42 = await client.lPop('bikes:repairs'); console.log(res42); // 'bike:2' const res43 = await client.lPop('bikes:repairs'); console.log(res43); // 'bike:1' const res44 = await client.exists('bikes:repairs'); console.log(res44); // 0 const res45 = await client.del('bikes:repairs'); console.log(res45); // 0 const res46 = await client.lLen('bikes:repairs'); console.log(res46); // 0 const res47 = await client.lPop('bikes:repairs'); console.log(res47); // null const res48 = await client.lPush( 'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5'] ); console.log(res48); // 5 const res49 = await client.lTrim('bikes:repairs', 0, 2); console.log(res49); // 'OK' const res50 = await client.lRange('bikes:repairs', 0, -1); console.log(res50); // ['bike:5', 'bike:4', 'bike:3']
package io.redis.examples; import org.junit.Test; import redis.clients.jedis.UnifiedJedis; import redis.clients.jedis.args.ListDirection; import java.util.List; import static org.junit.Assert.*; public class ListExample { public void run() { UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); long res1 = jedis.lpush("bikes:repairs", "bike:1"); System.out.println(res1); // >>> 1 long res2 = jedis.lpush("bikes:repairs", "bike:2"); System.out.println(res2); // >>> 2 String res3 = jedis.rpop("bikes:repairs"); System.out.println(res3); // >>> bike:1 String res4 = jedis.rpop("bikes:repairs"); System.out.println(res4); // >>> bike:2 long res5 = jedis.lpush("bikes:repairs", "bike:1"); System.out.println(res5); // >>> 1 long res6 = jedis.lpush("bikes:repairs", "bike:2"); System.out.println(res6); // >>> 2 String res7 = jedis.lpop("bikes:repairs"); System.out.println(res7); // >>> bike:2 String res8 = jedis.lpop("bikes:repairs"); System.out.println(res8); // >>> bike:1 long res9 = jedis.llen("bikes:repairs"); System.out.println(res9); // >>> 0 long res10 = jedis.lpush("bikes:repairs", "bike:1"); System.out.println(res10); // >>> 1 long res11 = jedis.lpush("bikes:repairs", "bike:2"); System.out.println(res11); // >>> 2 String res12 = jedis.lmove("bikes:repairs", "bikes:finished", ListDirection.LEFT, ListDirection.LEFT); System.out.println(res12); // >>> bike:2 List<String> res13 = jedis.lrange("bikes:repairs", 0, -1); System.out.println(res13); // >>> [bike:1] List<String> res14 = jedis.lrange("bikes:finished", 0, -1); System.out.println(res14); // >>> [bike:2] long res15 = jedis.rpush("bikes:repairs", "bike:1"); System.out.println(res15); // >>> 1 long res16 = jedis.rpush("bikes:repairs", "bike:2"); System.out.println(res16); // >>> 2 long res17 = jedis.lpush("bikes:repairs", "bike:important_bike"); System.out.println(res17); // >>> 3 List<String> res18 = jedis.lrange("bikes:repairs", 0, -1); System.out.println(res18); // >>> [bike:important_bike, bike:1, bike:2] long res19 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3"); System.out.println(res19); // >>> 3 long res20 = jedis.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike"); System.out.println(res20); // >>> 5 List<String> res21 = jedis.lrange("bikes:repairs", 0, -1); System.out.println(res21); // >>> [bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3] long res22 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3"); System.out.println(res22); // >>> 3 String res23 = jedis.rpop("bikes:repairs"); System.out.println(res23); // >>> bike:3 String res24 = jedis.lpop("bikes:repairs"); System.out.println(res24); // >>> bike:1 String res25 = jedis.rpop("bikes:repairs"); System.out.println(res25); // >>> bike:2 String res26 = jedis.rpop("bikes:repairs"); System.out.println(res26); // >>> null long res27 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5"); System.out.println(res27); // >>> 5 String res28 = jedis.ltrim("bikes:repairs", 0, 2); System.out.println(res28); // >>> OK List<String> res29 = jedis.lrange("bikes:repairs", 0, -1); System.out.println(res29); // >>> [bike:5, bike:4, bike:3] res27 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5"); System.out.println(res27); // >>> 5 res28 = jedis.ltrim("bikes:repairs", -3, -1); System.out.println(res2); // >>> OK res29 = jedis.lrange("bikes:repairs", 0, -1); System.out.println(res29); // >>> [bike:3, bike:4, bike:5] long res31 = jedis.rpush("bikes:repairs", "bike:1", "bike:2"); System.out.println(res31); // >>> 2 List<String> res32 = jedis.brpop(1, "bikes:repairs"); System.out.println(res32); // >>> (bikes:repairs, bike:2) List<String> res33 = jedis.brpop(1,"bikes:repairs"); System.out.println(res33); // >>> (bikes:repairs, bike:1) List<String> res34 = jedis.brpop(1,"bikes:repairs"); System.out.println(res34); // >>> null long res35 = jedis.del("new_bikes"); System.out.println(res35); // >>> 0 long res36 = jedis.lpush("new_bikes", "bike:1", "bike:2", "bike:3"); System.out.println(res36); // >>> 3 String res37 = jedis.set("new_bikes", "bike:1"); System.out.println(res37); // >>> OK String res38 = jedis.type("new_bikes"); System.out.println(res38); // >>> string try { long res39 = jedis.lpush("new_bikes", "bike:2", "bike:3"); } catch (Exception e) { e.printStackTrace(); // >>> redis.clients.jedis.exceptions.JedisDataException: // >>> WRONGTYPE Operation against a key holding the wrong kind of value } jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3"); System.out.println(res36); // >>> 3 boolean res40 = jedis.exists("bikes:repairs"); System.out.println(res40); // >>> true String res41 = jedis.lpop("bikes:repairs"); System.out.println(res41); // >>> bike:3 String res42 = jedis.lpop("bikes:repairs"); System.out.println(res42); // >>> bike:2 String res43 = jedis.lpop("bikes:repairs"); System.out.println(res43); // >>> bike:1 boolean res44 = jedis.exists("bikes:repairs"); System.out.println(res44); // >>> false long res45 = jedis.del("bikes:repairs"); System.out.println(res45); // >>> 0 long res46 = jedis.llen("bikes:repairs"); System.out.println(res46); // >>> 0 String res47 = jedis.lpop("bikes:repairs"); System.out.println(res47); // >>> null long res48 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5"); System.out.println(res48); // >>> 5 String res49 = jedis.ltrim("bikes:repairs", 0, 2); System.out.println(res49); // >>> OK List<String> res50 = jedis.lrange("bikes:repairs", 0, -1); System.out.println(res50); // >>> [bike:5, bike:4, bike:3] } }
package example_commands_test import ( "context" "fmt" "github.com/redis/go-redis/v9" ) func ExampleClient_queue() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res1, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result() if err != nil { panic(err) } fmt.Println(res1) // >>> 1 res2, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result() if err != nil { panic(err) } fmt.Println(res2) // >>> 2 res3, err := rdb.RPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res3) // >>> bike:1 res4, err := rdb.RPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res4) // >>> bike:2 } func ExampleClient_stack() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res5, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result() if err != nil { panic(err) } fmt.Println(res5) // >>> 1 res6, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result() if err != nil { panic(err) } fmt.Println(res6) // >>> 2 res7, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res7) // >>> bike:2 res8, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res8) // >>> bike:1 } func ExampleClient_llen() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res9, err := rdb.LLen(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res9) // >>> 0 } func ExampleClient_lmove_lrange() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res10, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result() if err != nil { panic(err) } fmt.Println(res10) // >>> 1 res11, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result() if err != nil { panic(err) } fmt.Println(res11) // >>> 2 res12, err := rdb.LMove(ctx, "bikes:repairs", "bikes:finished", "LEFT", "LEFT").Result() if err != nil { panic(err) } fmt.Println(res12) // >>> bike:2 res13, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res13) // >>> [bike:1] res14, err := rdb.LRange(ctx, "bikes:finished", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res14) // >>> [bike:2] } func ExampleClient_lpush_rpush() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res15, err := rdb.RPush(ctx, "bikes:repairs", "bike:1").Result() if err != nil { panic(err) } fmt.Println(res15) // >>> 1 res16, err := rdb.RPush(ctx, "bikes:repairs", "bike:2").Result() if err != nil { panic(err) } fmt.Println(res16) // >>> 2 res17, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike").Result() if err != nil { panic(err) } fmt.Println(res17) // >>> 3 res18, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res18) // >>> [bike:important_bike bike:1 bike:2] } func ExampleClient_variadic() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res19, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result() if err != nil { panic(err) } fmt.Println(res19) // >>> 3 res20, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike", "bike:very_important_bike").Result() if err != nil { panic(err) } fmt.Println(res20) // >>> 5 res21, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res21) // >>> [bike:very_important_bike bike:important_bike bike:1 bike:2 bike:3] } func ExampleClient_lpop_rpop() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res22, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result() if err != nil { panic(err) } fmt.Println(res22) // >>> 3 res23, err := rdb.RPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res23) // >>> bike:3 res24, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res24) // >>> bike:1 res25, err := rdb.RPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res25) // >>> bike:2 res26, err := rdb.RPop(ctx, "bikes:repairs").Result() if err != nil { fmt.Println(err) // >>> redis: nil } fmt.Println(res26) // >>> <empty string> } func ExampleClient_ltrim() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res27, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result() if err != nil { panic(err) } fmt.Println(res27) // >>> 5 res28, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result() if err != nil { panic(err) } fmt.Println(res28) // >>> OK res29, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res29) // >>> [bike:5 bike:4 bike:3] } func ExampleClient_ltrim_end_of_list() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res30, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result() if err != nil { panic(err) } fmt.Println(res30) // >>> 5 res31, err := rdb.LTrim(ctx, "bikes:repairs", -3, -1).Result() if err != nil { panic(err) } fmt.Println(res31) // >>> OK res32, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res32) // >>> [bike:3 bike:4 bike:5] } func ExampleClient_brpop() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res33, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2").Result() if err != nil { panic(err) } fmt.Println(res33) // >>> 2 res34, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res34) // >>> [bikes:repairs bike:2] res35, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res35) // >>> [bikes:repairs bike:1] res36, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result() if err != nil { fmt.Println(err) // >>> redis: nil } fmt.Println(res36) // >>> [] } func ExampleClient_rule1() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res37, err := rdb.Del(ctx, "new_bikes").Result() if err != nil { panic(err) } fmt.Println(res37) // >>> 0 res38, err := rdb.LPush(ctx, "new_bikes", "bike:1", "bike:2", "bike:3").Result() if err != nil { panic(err) } fmt.Println(res38) // >>> 3 } func ExampleClient_rule11() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res39, err := rdb.Set(ctx, "new_bikes", "bike:1", 0).Result() if err != nil { panic(err) } fmt.Println(res39) // >>> OK res40, err := rdb.Type(ctx, "new_bikes").Result() if err != nil { panic(err) } fmt.Println(res40) // >>> string res41, err := rdb.LPush(ctx, "new_bikes", "bike:2", "bike:3").Result() if err != nil { fmt.Println(err) // >>> WRONGTYPE Operation against a key holding the wrong kind of value } fmt.Println(res41) } func ExampleClient_rule2() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res42, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result() if err != nil { panic(err) } fmt.Println(res42) // >>> 3 res43, err := rdb.Exists(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res43) // >>> 1 res44, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res44) // >>> bike:3 res45, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res45) // >>> bike:2 res46, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res46) // >>> bike:1 res47, err := rdb.Exists(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res47) // >>> 0 } func ExampleClient_rule3() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res48, err := rdb.Del(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res48) // >>> 0 res49, err := rdb.LLen(ctx, "bikes:repairs").Result() if err != nil { panic(err) } fmt.Println(res49) // >>> 0 res50, err := rdb.LPop(ctx, "bikes:repairs").Result() if err != nil { fmt.Println(err) // >>> redis: nil } fmt.Println(res50) // >>> <empty string> } func ExampleClient_ltrim1() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password docs DB: 0, // use default DB }) res51, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result() if err != nil { panic(err) } fmt.Println(res51) // >>> 5 res52, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result() if err != nil { panic(err) } fmt.Println(res52) // >>> OK res53, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result() if err != nil { panic(err) } fmt.Println(res53) // >>> [bike:5 bike:4 bike:3] }
public class ListExample { public void run() { var muxer = ConnectionMultiplexer.Connect("localhost:6379"); var db = muxer.GetDatabase(); long res1 = db.ListLeftPush("bikes:repairs", "bike:1"); Console.WriteLine(res1); // >>> 1 long res2 = db.ListLeftPush("bikes:repairs", "bike:2"); Console.WriteLine(res2); // >>> 2 RedisValue res3 = db.ListRightPop("bikes:repairs"); Console.WriteLine(res3); // >>> "bike:1" RedisValue res4 = db.ListRightPop("bikes:repairs"); Console.WriteLine(res4); // >>> "bike:2" long res5 = db.ListLeftPush("bikes:repairs", "bike:1"); Console.WriteLine(res5); // >>> 1 long res6 = db.ListLeftPush("bikes:repairs", "bike:2"); Console.WriteLine(res6); // >>> 2 RedisValue res7 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res7); // >>> "bike:2" RedisValue res8 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res8); // >>> "bike:1" long res9 = db.ListLength("bikes:repairs"); Console.WriteLine(res9); // >>> 0 long res10 = db.ListLeftPush("{bikes}:repairs", "bike:1"); Console.WriteLine(res10); // >>> 1 long res11 = db.ListLeftPush("{bikes}:repairs", "bike:2"); Console.WriteLine(res11); // >>> 2 RedisValue res12 = db.ListMove("{bikes}:repairs", "{bikes}:finished", ListSide.Left, ListSide.Left); Console.Write(res12); // >>> "bike:2" RedisValue[] res13 = db.ListRange("{bikes}:repairs", 0, -1); Console.WriteLine(string.Join(", ", res13)); // >>> "bike:1" RedisValue[] res14 = db.ListRange("{bikes}:finished", 0, -1); Console.WriteLine(string.Join(", ", res14)); // >>> "bike:2" long res15 = db.ListRightPush("bikes:repairs", "bike:1"); Console.WriteLine(res15); // >>> 1 long res16 = db.ListRightPush("bikes:repairs", "bike:2"); Console.WriteLine(res16); // >>> 2 long res17 = db.ListLeftPush("bikes:repairs", "bike:important_bike"); Console.WriteLine(res17); // >>> 3 RedisValue[] res18 = db.ListRange("bikes:repairs", 0, -1); Console.WriteLine(string.Join(", ", res18)); // >>> "bike:important_bike, bike:1, bike:2" long res19 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); Console.WriteLine(res19); // >>> 3 long res20 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:important_bike", "bike:very_important_bike" }); Console.WriteLine(res20); // >>> 5 RedisValue[] res21 = db.ListRange("bikes:repairs", 0, -1); Console.WriteLine(string.Join(", ", res21)); // >>> "bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3" long res22 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); Console.WriteLine(res22); // >>> 3 RedisValue res23 = db.ListRightPop("bikes:repairs"); Console.WriteLine(res23); // >>> "bike:3" RedisValue res24 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res24); // >>> "bike:1" RedisValue res25 = db.ListRightPop("bikes:repairs"); Console.WriteLine(res25); // >>> "bike:2" RedisValue res26 = db.ListRightPop("bikes:repairs"); Console.WriteLine(res26); // >>> <Empty string> long res27 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" }); Console.WriteLine(res27); // >>> 5 db.ListTrim("bikes:repairs", 0, 2); RedisValue[] res28 = db.ListRange("bikes:repairs", 0, -1); Console.WriteLine(string.Join(", ", res28)); // "bike:5, bike:4, bike:3" long res29 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" }); Console.WriteLine(res29); // >>> 5 db.ListTrim("bikes:repairs", -3, -1); RedisValue[] res30 = db.ListRange("bikes:repairs", 0, -1); Console.WriteLine(string.Join(", ", res30)); // >>> "bike:3, bike:4, bike:5" long res31 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2" }); Console.WriteLine(res31); // >>> 2 Tuple<RedisKey, RedisValue>? res32 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1); if (res32 != null) Console.WriteLine($"{res32.Item1} -> {res32.Item2}"); // >>> "bikes:repairs -> bike:2" Tuple<RedisKey, RedisValue>? res33 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1); if (res33 != null) Console.WriteLine($"{res33.Item1} -> {res33.Item2}"); // >>> "bikes:repairs -> bike:1" Tuple<RedisKey, RedisValue>? res34 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1); Console.WriteLine(res34); // >>> "Null" bool res35 = db.KeyDelete("new_bikes"); Console.WriteLine(res35); // >>> False long res36 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); Console.WriteLine(res36); // >>> 3 bool res37 = db.StringSet("new_bikes", "bike:1"); Console.WriteLine(res37); // >>> True RedisType res38 = db.KeyType("new_bikes"); Console.WriteLine(res38); // >>> RedisType.String try { long res39 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:2", "bike:3" }); } catch (Exception e) { Console.WriteLine(e); } long res40 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" }); Console.WriteLine(res40); // >>> 3 bool res41 = db.KeyExists("bikes:repairs"); Console.WriteLine(res41); // >>> True RedisValue res42 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res42); // >>> "bike:3" RedisValue res43 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res43); // >>> "bike:2" RedisValue res44 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res44); // >>> "bike:1" bool res45 = db.KeyExists("bikes:repairs"); Console.WriteLine(res45); // >>> False bool res46 = db.KeyDelete("bikes:repairs"); Console.WriteLine(res46); // >>> False long res47 = db.ListLength("bikes:repairs"); Console.WriteLine(res47); // >>> 0 RedisValue res48 = db.ListLeftPop("bikes:repairs"); Console.WriteLine(res48); // >>> Null long res49 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" }); Console.WriteLine(res49); // >>> 5 db.ListTrim("bikes:repairs", 0, 2); RedisValue[] res50 = db.ListRange("bikes:repairs", 0, -1); Console.WriteLine(string.Join(", ", res50)); // >>> "bike:5, bike:4, bike:3" } }
What are Lists?
To explain the List data type it's better to start with a little bit of theory, as the term List is often used in an improper way by information technology folks. For instance "Python Lists" are not what the name may suggest (Linked Lists), but rather Arrays (the same data type is called Array in Ruby actually).
From a very general point of view a List is just a sequence of ordered elements: 10,20,1,2,3 is a list. But the properties of a List implemented using an Array are very different from the properties of a List implemented using a Linked List.
Redis lists are implemented via Linked Lists. This means that even if you have
millions of elements inside a list, the operation of adding a new element in
the head or in the tail of the list is performed in constant time. The speed of adding a
new element with the LPUSH
command to the head of a list with ten
elements is the same as adding an element to the head of list with 10
million elements.
What's the downside? Accessing an element by index is very fast in lists implemented with an Array (constant time indexed access) and not so fast in lists implemented by linked lists (where the operation requires an amount of work proportional to the index of the accessed element).
Redis Lists are implemented with linked lists because for a database system it is crucial to be able to add elements to a very long list in a very fast way. Another strong advantage, as you'll see in a moment, is that Redis Lists can be taken at constant length in constant time.
When fast access to the middle of a large collection of elements is important, there is a different data structure that can be used, called sorted sets. Sorted sets are covered in the Sorted sets tutorial page.
First steps with Redis Lists
The LPUSH
command adds a new element into a list, on the
left (at the head), while the RPUSH
command adds a new
element into a list, on the right (at the tail). Finally the
LRANGE
command extracts ranges of elements from lists:
> RPUSH bikes:repairs bike:1
(integer) 1
> RPUSH bikes:repairs bike:2
(integer) 2
> LPUSH bikes:repairs bike:important_bike
(integer) 3
> LRANGE bikes:repairs 0 -1
1) "bike:important_bike"
2) "bike:1"
3) "bike:2"
"""
Code samples for List doc pages:
https://redis.io/docs/latest/develop/data-types/lists/
"""
import redis
r = redis.Redis(decode_responses=True)
res1 = r.lpush("bikes:repairs", "bike:1")
print(res1) # >>> 1
res2 = r.lpush("bikes:repairs", "bike:2")
print(res2) # >>> 2
res3 = r.rpop("bikes:repairs")
print(res3) # >>> bike:1
res4 = r.rpop("bikes:repairs")
print(res4) # >>> bike:2
res5 = r.lpush("bikes:repairs", "bike:1")
print(res5) # >>> 1
res6 = r.lpush("bikes:repairs", "bike:2")
print(res6) # >>> 2
res7 = r.lpop("bikes:repairs")
print(res7) # >>> bike:2
res8 = r.lpop("bikes:repairs")
print(res8) # >>> bike:1
res9 = r.llen("bikes:repairs")
print(res9) # >>> 0
res10 = r.lpush("bikes:repairs", "bike:1")
print(res10) # >>> 1
res11 = r.lpush("bikes:repairs", "bike:2")
print(res11) # >>> 2
res12 = r.lmove("bikes:repairs", "bikes:finished", "LEFT", "LEFT")
print(res12) # >>> 'bike:2'
res13 = r.lrange("bikes:repairs", 0, -1)
print(res13) # >>> ['bike:1']
res14 = r.lrange("bikes:finished", 0, -1)
print(res14) # >>> ['bike:2']
res15 = r.rpush("bikes:repairs", "bike:1")
print(res15) # >>> 1
res16 = r.rpush("bikes:repairs", "bike:2")
print(res16) # >>> 2
res17 = r.lpush("bikes:repairs", "bike:important_bike")
print(res17) # >>> 3
res18 = r.lrange("bikes:repairs", 0, -1)
print(res18) # >>> ['bike:important_bike', 'bike:1', 'bike:2']
res19 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res19) # >>> 3
res20 = r.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike")
print(res20) # >>> 5
res21 = r.lrange("bikes:repairs", 0, -1)
print(
res21
) # >>> ['bike:very_important_bike', 'bike:important_bike', 'bike:1', ...
res22 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res22) # >>> 3
res23 = r.rpop("bikes:repairs")
print(res23) # >>> 'bike:3'
res24 = r.lpop("bikes:repairs")
print(res24) # >>> 'bike:1'
res25 = r.rpop("bikes:repairs")
print(res25) # >>> 'bike:2'
res26 = r.rpop("bikes:repairs")
print(res26) # >>> None
res27 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res27) # >>> 5
res28 = r.ltrim("bikes:repairs", 0, 2)
print(res28) # >>> True
res29 = r.lrange("bikes:repairs", 0, -1)
print(res29) # >>> ['bike:5', 'bike:4', 'bike:3']
res27 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res27) # >>> 5
res28 = r.ltrim("bikes:repairs", -3, -1)
print(res28) # >>> True
res29 = r.lrange("bikes:repairs", 0, -1)
print(res29) # >>> ['bike:3', 'bike:4', 'bike:5']
res31 = r.rpush("bikes:repairs", "bike:1", "bike:2")
print(res31) # >>> 2
res32 = r.brpop("bikes:repairs", timeout=1)
print(res32) # >>> ('bikes:repairs', 'bike:2')
res33 = r.brpop("bikes:repairs", timeout=1)
print(res33) # >>> ('bikes:repairs', 'bike:1')
res34 = r.brpop("bikes:repairs", timeout=1)
print(res34) # >>> None
res35 = r.delete("new_bikes")
print(res35) # >>> 0
res36 = r.lpush("new_bikes", "bike:1", "bike:2", "bike:3")
print(res36) # >>> 3
res37 = r.set("new_bikes", "bike:1")
print(res37) # >>> True
res38 = r.type("new_bikes")
print(res38) # >>> 'string'
try:
res39 = r.lpush("new_bikes", "bike:2", "bike:3")
# >>> redis.exceptions.ResponseError:
# >>> WRONGTYPE Operation against a key holding the wrong kind of value
except redis.exceptions.ResponseError as e:
print(e)
r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res36) # >>> 3
res40 = r.exists("bikes:repairs")
print(res40) # >>> 1
res41 = r.lpop("bikes:repairs")
print(res41) # >>> 'bike:3'
res42 = r.lpop("bikes:repairs")
print(res42) # >>> 'bike:2'
res43 = r.lpop("bikes:repairs")
print(res43) # >>> 'bike:1'
res44 = r.exists("bikes:repairs")
print(res44) # >>> False
res45 = r.delete("bikes:repairs")
print(res45) # >>> 0
res46 = r.llen("bikes:repairs")
print(res46) # >>> 0
res47 = r.lpop("bikes:repairs")
print(res47) # >>> None
res48 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res48) # >>> 5
res49 = r.ltrim("bikes:repairs", 0, 2)
print(res49) # >>> True
res50 = r.lrange("bikes:repairs", 0, -1)
print(res50) # >>> ['bike:5', 'bike:4', 'bike:3']
import assert from 'assert';
import { createClient } from 'redis';
const client = await createClient();
await client.connect();
const res1 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res1); // 1
const res2 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res2); // 2
const res3 = await client.rPop('bikes:repairs');
console.log(res3); // bike:1
const res4 = await client.rPop('bikes:repairs');
console.log(res4); // bike:2
const res5 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res5); // 1
const res6 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res6); // 2
const res7 = await client.lPop('bikes:repairs');
console.log(res7); // bike:2
const res8 = await client.lPop('bikes:repairs');
console.log(res8); // bike:1
const res9 = await client.lLen('bikes:repairs');
console.log(res9); // 0
const res10 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res10); // 1
const res11 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res11); // 2
const res12 = await client.lMove('bikes:repairs', 'bikes:finished', 'LEFT', 'LEFT');
console.log(res12); // 'bike:2'
const res13 = await client.lRange('bikes:repairs', 0, -1);
console.log(res13); // ['bike:1']
const res14 = await client.lRange('bikes:finished', 0, -1);
console.log(res14); // ['bike:2']
const res15 = await client.rPush('bikes:repairs', 'bike:1');
console.log(res15); // 1
const res16 = await client.rPush('bikes:repairs', 'bike:2');
console.log(res16); // 2
const res17 = await client.lPush('bikes:repairs', 'bike:important_bike');
console.log(res17); // 3
const res18 = await client.lRange('bikes:repairs', 0, -1);
console.log(res18); // ['bike:important_bike', 'bike:1', 'bike:2']
const res19 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res19); // 3
const res20 = await client.lPush(
'bikes:repairs', ['bike:important_bike', 'bike:very_important_bike']
);
console.log(res20); // 5
const res21 = await client.lRange('bikes:repairs', 0, -1);
console.log(res21); // ['bike:very_important_bike', 'bike:important_bike', 'bike:1', 'bike:2', 'bike:3']
const res22 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res22); // 3
const res23 = await client.rPop('bikes:repairs');
console.log(res23); // 'bike:3'
const res24 = await client.lPop('bikes:repairs');
console.log(res24); // 'bike:1'
const res25 = await client.rPop('bikes:repairs');
console.log(res25); // 'bike:2'
const res26 = await client.rPop('bikes:repairs');
console.log(res26); // None
const res27 = await client.lPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res27); // 5
const res28 = await client.lTrim('bikes:repairs', 0, 2);
console.log(res28); // true
const res29 = await client.lRange('bikes:repairs', 0, -1);
console.log(res29); // ['bike:5', 'bike:4', 'bike:3']
const res27eol = await client.rPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res27eol); // 5
const res28eol = await client.lTrim('bikes:repairs', -3, -1);
console.log(res28eol); // 'OK'
const res29eol = await client.lRange('bikes:repairs', 0, -1);
console.log(res29eol); // ['bike:3', 'bike:4', 'bike:5']
const res31 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2']);
console.log(res31); // 2
const res32 = await client.brPop('bikes:repairs', 1);
console.log(res32); // { key: 'bikes:repairs', element: 'bike:2' }
const res33 = await client.brPop('bikes:repairs', 1);
console.log(res33); // { key: 'bikes:repairs', element: 'bike:1' }
const res34 = await client.brPop('bikes:repairs', 1);
console.log(res34); // null
const res35 = await client.del('new_bikes');
console.log(res35); // 0
const res36 = await client.lPush('new_bikes', ['bike:1', 'bike:2', 'bike:3']);
console.log(res36); // 3
const res37 = await client.set('new_bikes', 'bike:1');
console.log(res37); // 'OK'
const res38 = await client.type('new_bikes');
console.log(res38); // 'string'
try {
const res39 = await client.lPush('new_bikes', 'bike:2', 'bike:3');
// redis.exceptions.ResponseError:
// [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value]
}
catch(e){
console.log(e);
}
await client.lPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res36); // 3
const res40 = await client.exists('bikes:repairs')
console.log(res40); // true
const res41 = await client.lPop('bikes:repairs');
console.log(res41); // 'bike:3'
const res42 = await client.lPop('bikes:repairs');
console.log(res42); // 'bike:2'
const res43 = await client.lPop('bikes:repairs');
console.log(res43); // 'bike:1'
const res44 = await client.exists('bikes:repairs');
console.log(res44); // 0
const res45 = await client.del('bikes:repairs');
console.log(res45); // 0
const res46 = await client.lLen('bikes:repairs');
console.log(res46); // 0
const res47 = await client.lPop('bikes:repairs');
console.log(res47); // null
const res48 = await client.lPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res48); // 5
const res49 = await client.lTrim('bikes:repairs', 0, 2);
console.log(res49); // 'OK'
const res50 = await client.lRange('bikes:repairs', 0, -1);
console.log(res50); // ['bike:5', 'bike:4', 'bike:3']
package io.redis.examples;
import org.junit.Test;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.args.ListDirection;
import java.util.List;
import static org.junit.Assert.*;
public class ListExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
long res1 = jedis.lpush("bikes:repairs", "bike:1");
System.out.println(res1); // >>> 1
long res2 = jedis.lpush("bikes:repairs", "bike:2");
System.out.println(res2); // >>> 2
String res3 = jedis.rpop("bikes:repairs");
System.out.println(res3); // >>> bike:1
String res4 = jedis.rpop("bikes:repairs");
System.out.println(res4); // >>> bike:2
long res5 = jedis.lpush("bikes:repairs", "bike:1");
System.out.println(res5); // >>> 1
long res6 = jedis.lpush("bikes:repairs", "bike:2");
System.out.println(res6); // >>> 2
String res7 = jedis.lpop("bikes:repairs");
System.out.println(res7); // >>> bike:2
String res8 = jedis.lpop("bikes:repairs");
System.out.println(res8); // >>> bike:1
long res9 = jedis.llen("bikes:repairs");
System.out.println(res9); // >>> 0
long res10 = jedis.lpush("bikes:repairs", "bike:1");
System.out.println(res10); // >>> 1
long res11 = jedis.lpush("bikes:repairs", "bike:2");
System.out.println(res11); // >>> 2
String res12 = jedis.lmove("bikes:repairs", "bikes:finished", ListDirection.LEFT, ListDirection.LEFT);
System.out.println(res12); // >>> bike:2
List<String> res13 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res13); // >>> [bike:1]
List<String> res14 = jedis.lrange("bikes:finished", 0, -1);
System.out.println(res14); // >>> [bike:2]
long res15 = jedis.rpush("bikes:repairs", "bike:1");
System.out.println(res15); // >>> 1
long res16 = jedis.rpush("bikes:repairs", "bike:2");
System.out.println(res16); // >>> 2
long res17 = jedis.lpush("bikes:repairs", "bike:important_bike");
System.out.println(res17); // >>> 3
List<String> res18 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res18); // >>> [bike:important_bike, bike:1, bike:2]
long res19 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3");
System.out.println(res19); // >>> 3
long res20 = jedis.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike");
System.out.println(res20); // >>> 5
List<String> res21 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res21); // >>> [bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3]
long res22 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3");
System.out.println(res22); // >>> 3
String res23 = jedis.rpop("bikes:repairs");
System.out.println(res23); // >>> bike:3
String res24 = jedis.lpop("bikes:repairs");
System.out.println(res24); // >>> bike:1
String res25 = jedis.rpop("bikes:repairs");
System.out.println(res25); // >>> bike:2
String res26 = jedis.rpop("bikes:repairs");
System.out.println(res26); // >>> null
long res27 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
System.out.println(res27); // >>> 5
String res28 = jedis.ltrim("bikes:repairs", 0, 2);
System.out.println(res28); // >>> OK
List<String> res29 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res29); // >>> [bike:5, bike:4, bike:3]
res27 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
System.out.println(res27); // >>> 5
res28 = jedis.ltrim("bikes:repairs", -3, -1);
System.out.println(res2); // >>> OK
res29 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res29); // >>> [bike:3, bike:4, bike:5]
long res31 = jedis.rpush("bikes:repairs", "bike:1", "bike:2");
System.out.println(res31); // >>> 2
List<String> res32 = jedis.brpop(1, "bikes:repairs");
System.out.println(res32); // >>> (bikes:repairs, bike:2)
List<String> res33 = jedis.brpop(1,"bikes:repairs");
System.out.println(res33); // >>> (bikes:repairs, bike:1)
List<String> res34 = jedis.brpop(1,"bikes:repairs");
System.out.println(res34); // >>> null
long res35 = jedis.del("new_bikes");
System.out.println(res35); // >>> 0
long res36 = jedis.lpush("new_bikes", "bike:1", "bike:2", "bike:3");
System.out.println(res36); // >>> 3
String res37 = jedis.set("new_bikes", "bike:1");
System.out.println(res37); // >>> OK
String res38 = jedis.type("new_bikes");
System.out.println(res38); // >>> string
try {
long res39 = jedis.lpush("new_bikes", "bike:2", "bike:3");
} catch (Exception e) {
e.printStackTrace();
// >>> redis.clients.jedis.exceptions.JedisDataException:
// >>> WRONGTYPE Operation against a key holding the wrong kind of value
}
jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3");
System.out.println(res36); // >>> 3
boolean res40 = jedis.exists("bikes:repairs");
System.out.println(res40); // >>> true
String res41 = jedis.lpop("bikes:repairs");
System.out.println(res41); // >>> bike:3
String res42 = jedis.lpop("bikes:repairs");
System.out.println(res42); // >>> bike:2
String res43 = jedis.lpop("bikes:repairs");
System.out.println(res43); // >>> bike:1
boolean res44 = jedis.exists("bikes:repairs");
System.out.println(res44); // >>> false
long res45 = jedis.del("bikes:repairs");
System.out.println(res45); // >>> 0
long res46 = jedis.llen("bikes:repairs");
System.out.println(res46); // >>> 0
String res47 = jedis.lpop("bikes:repairs");
System.out.println(res47); // >>> null
long res48 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
System.out.println(res48); // >>> 5
String res49 = jedis.ltrim("bikes:repairs", 0, 2);
System.out.println(res49); // >>> OK
List<String> res50 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res50); // >>> [bike:5, bike:4, bike:3]
}
}
package example_commands_test
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
func ExampleClient_queue() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res1, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res1) // >>> 1
res2, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res2) // >>> 2
res3, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res3) // >>> bike:1
res4, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res4) // >>> bike:2
}
func ExampleClient_stack() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res5, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res5) // >>> 1
res6, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res6) // >>> 2
res7, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res7) // >>> bike:2
res8, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res8) // >>> bike:1
}
func ExampleClient_llen() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res9, err := rdb.LLen(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res9) // >>> 0
}
func ExampleClient_lmove_lrange() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res10, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res10) // >>> 1
res11, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res11) // >>> 2
res12, err := rdb.LMove(ctx, "bikes:repairs", "bikes:finished", "LEFT", "LEFT").Result()
if err != nil {
panic(err)
}
fmt.Println(res12) // >>> bike:2
res13, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res13) // >>> [bike:1]
res14, err := rdb.LRange(ctx, "bikes:finished", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res14) // >>> [bike:2]
}
func ExampleClient_lpush_rpush() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res15, err := rdb.RPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res15) // >>> 1
res16, err := rdb.RPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res16) // >>> 2
res17, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike").Result()
if err != nil {
panic(err)
}
fmt.Println(res17) // >>> 3
res18, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res18) // >>> [bike:important_bike bike:1 bike:2]
}
func ExampleClient_variadic() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res19, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res19) // >>> 3
res20, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike", "bike:very_important_bike").Result()
if err != nil {
panic(err)
}
fmt.Println(res20) // >>> 5
res21, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res21) // >>> [bike:very_important_bike bike:important_bike bike:1 bike:2 bike:3]
}
func ExampleClient_lpop_rpop() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res22, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res22) // >>> 3
res23, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res23) // >>> bike:3
res24, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res24) // >>> bike:1
res25, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res25) // >>> bike:2
res26, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
fmt.Println(err) // >>> redis: nil
}
fmt.Println(res26) // >>> <empty string>
}
func ExampleClient_ltrim() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res27, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
fmt.Println(res27) // >>> 5
res28, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result()
if err != nil {
panic(err)
}
fmt.Println(res28) // >>> OK
res29, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res29) // >>> [bike:5 bike:4 bike:3]
}
func ExampleClient_ltrim_end_of_list() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res30, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
fmt.Println(res30) // >>> 5
res31, err := rdb.LTrim(ctx, "bikes:repairs", -3, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res31) // >>> OK
res32, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res32) // >>> [bike:3 bike:4 bike:5]
}
func ExampleClient_brpop() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res33, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res33) // >>> 2
res34, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res34) // >>> [bikes:repairs bike:2]
res35, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res35) // >>> [bikes:repairs bike:1]
res36, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
if err != nil {
fmt.Println(err) // >>> redis: nil
}
fmt.Println(res36) // >>> []
}
func ExampleClient_rule1() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res37, err := rdb.Del(ctx, "new_bikes").Result()
if err != nil {
panic(err)
}
fmt.Println(res37) // >>> 0
res38, err := rdb.LPush(ctx, "new_bikes", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res38) // >>> 3
}
func ExampleClient_rule11() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res39, err := rdb.Set(ctx, "new_bikes", "bike:1", 0).Result()
if err != nil {
panic(err)
}
fmt.Println(res39) // >>> OK
res40, err := rdb.Type(ctx, "new_bikes").Result()
if err != nil {
panic(err)
}
fmt.Println(res40) // >>> string
res41, err := rdb.LPush(ctx, "new_bikes", "bike:2", "bike:3").Result()
if err != nil {
fmt.Println(err)
// >>> WRONGTYPE Operation against a key holding the wrong kind of value
}
fmt.Println(res41)
}
func ExampleClient_rule2() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res42, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res42) // >>> 3
res43, err := rdb.Exists(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res43) // >>> 1
res44, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res44) // >>> bike:3
res45, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res45) // >>> bike:2
res46, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res46) // >>> bike:1
res47, err := rdb.Exists(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res47) // >>> 0
}
func ExampleClient_rule3() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res48, err := rdb.Del(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res48) // >>> 0
res49, err := rdb.LLen(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res49) // >>> 0
res50, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
fmt.Println(err) // >>> redis: nil
}
fmt.Println(res50) // >>> <empty string>
}
func ExampleClient_ltrim1() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res51, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
fmt.Println(res51) // >>> 5
res52, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result()
if err != nil {
panic(err)
}
fmt.Println(res52) // >>> OK
res53, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res53) // >>> [bike:5 bike:4 bike:3]
}
public class ListExample
{
public void run()
{
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
long res1 = db.ListLeftPush("bikes:repairs", "bike:1");
Console.WriteLine(res1); // >>> 1
long res2 = db.ListLeftPush("bikes:repairs", "bike:2");
Console.WriteLine(res2); // >>> 2
RedisValue res3 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res3); // >>> "bike:1"
RedisValue res4 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res4); // >>> "bike:2"
long res5 = db.ListLeftPush("bikes:repairs", "bike:1");
Console.WriteLine(res5); // >>> 1
long res6 = db.ListLeftPush("bikes:repairs", "bike:2");
Console.WriteLine(res6); // >>> 2
RedisValue res7 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res7); // >>> "bike:2"
RedisValue res8 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res8); // >>> "bike:1"
long res9 = db.ListLength("bikes:repairs");
Console.WriteLine(res9); // >>> 0
long res10 = db.ListLeftPush("{bikes}:repairs", "bike:1");
Console.WriteLine(res10); // >>> 1
long res11 = db.ListLeftPush("{bikes}:repairs", "bike:2");
Console.WriteLine(res11); // >>> 2
RedisValue res12 = db.ListMove("{bikes}:repairs", "{bikes}:finished", ListSide.Left, ListSide.Left);
Console.Write(res12); // >>> "bike:2"
RedisValue[] res13 = db.ListRange("{bikes}:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res13)); // >>> "bike:1"
RedisValue[] res14 = db.ListRange("{bikes}:finished", 0, -1);
Console.WriteLine(string.Join(", ", res14)); // >>> "bike:2"
long res15 = db.ListRightPush("bikes:repairs", "bike:1");
Console.WriteLine(res15); // >>> 1
long res16 = db.ListRightPush("bikes:repairs", "bike:2");
Console.WriteLine(res16); // >>> 2
long res17 = db.ListLeftPush("bikes:repairs", "bike:important_bike");
Console.WriteLine(res17); // >>> 3
RedisValue[] res18 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res18)); // >>> "bike:important_bike, bike:1, bike:2"
long res19 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res19); // >>> 3
long res20 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:important_bike", "bike:very_important_bike" });
Console.WriteLine(res20); // >>> 5
RedisValue[] res21 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res21));
// >>> "bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3"
long res22 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res22); // >>> 3
RedisValue res23 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res23); // >>> "bike:3"
RedisValue res24 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res24); // >>> "bike:1"
RedisValue res25 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res25); // >>> "bike:2"
RedisValue res26 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res26); // >>> <Empty string>
long res27 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
Console.WriteLine(res27); // >>> 5
db.ListTrim("bikes:repairs", 0, 2);
RedisValue[] res28 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res28)); // "bike:5, bike:4, bike:3"
long res29 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
Console.WriteLine(res29); // >>> 5
db.ListTrim("bikes:repairs", -3, -1);
RedisValue[] res30 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res30)); // >>> "bike:3, bike:4, bike:5"
long res31 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2" });
Console.WriteLine(res31); // >>> 2
Tuple<RedisKey, RedisValue>? res32 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1);
if (res32 != null)
Console.WriteLine($"{res32.Item1} -> {res32.Item2}"); // >>> "bikes:repairs -> bike:2"
Tuple<RedisKey, RedisValue>? res33 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1);
if (res33 != null)
Console.WriteLine($"{res33.Item1} -> {res33.Item2}"); // >>> "bikes:repairs -> bike:1"
Tuple<RedisKey, RedisValue>? res34 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1);
Console.WriteLine(res34); // >>> "Null"
bool res35 = db.KeyDelete("new_bikes");
Console.WriteLine(res35); // >>> False
long res36 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res36); // >>> 3
bool res37 = db.StringSet("new_bikes", "bike:1");
Console.WriteLine(res37); // >>> True
RedisType res38 = db.KeyType("new_bikes");
Console.WriteLine(res38); // >>> RedisType.String
try
{
long res39 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:2", "bike:3" });
}
catch (Exception e)
{
Console.WriteLine(e);
}
long res40 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res40); // >>> 3
bool res41 = db.KeyExists("bikes:repairs");
Console.WriteLine(res41); // >>> True
RedisValue res42 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res42); // >>> "bike:3"
RedisValue res43 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res43); // >>> "bike:2"
RedisValue res44 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res44); // >>> "bike:1"
bool res45 = db.KeyExists("bikes:repairs");
Console.WriteLine(res45); // >>> False
bool res46 = db.KeyDelete("bikes:repairs");
Console.WriteLine(res46); // >>> False
long res47 = db.ListLength("bikes:repairs");
Console.WriteLine(res47); // >>> 0
RedisValue res48 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res48); // >>> Null
long res49 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
Console.WriteLine(res49); // >>> 5
db.ListTrim("bikes:repairs", 0, 2);
RedisValue[] res50 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res50)); // >>> "bike:5, bike:4, bike:3"
}
}
Note that LRANGE
takes two indexes, the first and the last
element of the range to return. Both the indexes can be negative, telling Redis
to start counting from the end: so -1 is the last element, -2 is the
penultimate element of the list, and so forth.
As you can see RPUSH
appended the elements on the right of the list, while
the final LPUSH
appended the element on the left.
Both commands are variadic commands, meaning that you are free to push multiple elements into a list in a single call:
> RPUSH bikes:repairs bike:1 bike:2 bike:3
(integer) 3
> LPUSH bikes:repairs bike:important_bike bike:very_important_bike
> LRANGE mylist 0 -1
1) "bike:very_important_bike"
2) "bike:important_bike"
3) "bike:1"
4) "bike:2"
5) "bike:3"
"""
Code samples for List doc pages:
https://redis.io/docs/latest/develop/data-types/lists/
"""
import redis
r = redis.Redis(decode_responses=True)
res1 = r.lpush("bikes:repairs", "bike:1")
print(res1) # >>> 1
res2 = r.lpush("bikes:repairs", "bike:2")
print(res2) # >>> 2
res3 = r.rpop("bikes:repairs")
print(res3) # >>> bike:1
res4 = r.rpop("bikes:repairs")
print(res4) # >>> bike:2
res5 = r.lpush("bikes:repairs", "bike:1")
print(res5) # >>> 1
res6 = r.lpush("bikes:repairs", "bike:2")
print(res6) # >>> 2
res7 = r.lpop("bikes:repairs")
print(res7) # >>> bike:2
res8 = r.lpop("bikes:repairs")
print(res8) # >>> bike:1
res9 = r.llen("bikes:repairs")
print(res9) # >>> 0
res10 = r.lpush("bikes:repairs", "bike:1")
print(res10) # >>> 1
res11 = r.lpush("bikes:repairs", "bike:2")
print(res11) # >>> 2
res12 = r.lmove("bikes:repairs", "bikes:finished", "LEFT", "LEFT")
print(res12) # >>> 'bike:2'
res13 = r.lrange("bikes:repairs", 0, -1)
print(res13) # >>> ['bike:1']
res14 = r.lrange("bikes:finished", 0, -1)
print(res14) # >>> ['bike:2']
res15 = r.rpush("bikes:repairs", "bike:1")
print(res15) # >>> 1
res16 = r.rpush("bikes:repairs", "bike:2")
print(res16) # >>> 2
res17 = r.lpush("bikes:repairs", "bike:important_bike")
print(res17) # >>> 3
res18 = r.lrange("bikes:repairs", 0, -1)
print(res18) # >>> ['bike:important_bike', 'bike:1', 'bike:2']
res19 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res19) # >>> 3
res20 = r.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike")
print(res20) # >>> 5
res21 = r.lrange("bikes:repairs", 0, -1)
print(
res21
) # >>> ['bike:very_important_bike', 'bike:important_bike', 'bike:1', ...
res22 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res22) # >>> 3
res23 = r.rpop("bikes:repairs")
print(res23) # >>> 'bike:3'
res24 = r.lpop("bikes:repairs")
print(res24) # >>> 'bike:1'
res25 = r.rpop("bikes:repairs")
print(res25) # >>> 'bike:2'
res26 = r.rpop("bikes:repairs")
print(res26) # >>> None
res27 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res27) # >>> 5
res28 = r.ltrim("bikes:repairs", 0, 2)
print(res28) # >>> True
res29 = r.lrange("bikes:repairs", 0, -1)
print(res29) # >>> ['bike:5', 'bike:4', 'bike:3']
res27 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res27) # >>> 5
res28 = r.ltrim("bikes:repairs", -3, -1)
print(res28) # >>> True
res29 = r.lrange("bikes:repairs", 0, -1)
print(res29) # >>> ['bike:3', 'bike:4', 'bike:5']
res31 = r.rpush("bikes:repairs", "bike:1", "bike:2")
print(res31) # >>> 2
res32 = r.brpop("bikes:repairs", timeout=1)
print(res32) # >>> ('bikes:repairs', 'bike:2')
res33 = r.brpop("bikes:repairs", timeout=1)
print(res33) # >>> ('bikes:repairs', 'bike:1')
res34 = r.brpop("bikes:repairs", timeout=1)
print(res34) # >>> None
res35 = r.delete("new_bikes")
print(res35) # >>> 0
res36 = r.lpush("new_bikes", "bike:1", "bike:2", "bike:3")
print(res36) # >>> 3
res37 = r.set("new_bikes", "bike:1")
print(res37) # >>> True
res38 = r.type("new_bikes")
print(res38) # >>> 'string'
try:
res39 = r.lpush("new_bikes", "bike:2", "bike:3")
# >>> redis.exceptions.ResponseError:
# >>> WRONGTYPE Operation against a key holding the wrong kind of value
except redis.exceptions.ResponseError as e:
print(e)
r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res36) # >>> 3
res40 = r.exists("bikes:repairs")
print(res40) # >>> 1
res41 = r.lpop("bikes:repairs")
print(res41) # >>> 'bike:3'
res42 = r.lpop("bikes:repairs")
print(res42) # >>> 'bike:2'
res43 = r.lpop("bikes:repairs")
print(res43) # >>> 'bike:1'
res44 = r.exists("bikes:repairs")
print(res44) # >>> False
res45 = r.delete("bikes:repairs")
print(res45) # >>> 0
res46 = r.llen("bikes:repairs")
print(res46) # >>> 0
res47 = r.lpop("bikes:repairs")
print(res47) # >>> None
res48 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res48) # >>> 5
res49 = r.ltrim("bikes:repairs", 0, 2)
print(res49) # >>> True
res50 = r.lrange("bikes:repairs", 0, -1)
print(res50) # >>> ['bike:5', 'bike:4', 'bike:3']
import assert from 'assert';
import { createClient } from 'redis';
const client = await createClient();
await client.connect();
const res1 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res1); // 1
const res2 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res2); // 2
const res3 = await client.rPop('bikes:repairs');
console.log(res3); // bike:1
const res4 = await client.rPop('bikes:repairs');
console.log(res4); // bike:2
const res5 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res5); // 1
const res6 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res6); // 2
const res7 = await client.lPop('bikes:repairs');
console.log(res7); // bike:2
const res8 = await client.lPop('bikes:repairs');
console.log(res8); // bike:1
const res9 = await client.lLen('bikes:repairs');
console.log(res9); // 0
const res10 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res10); // 1
const res11 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res11); // 2
const res12 = await client.lMove('bikes:repairs', 'bikes:finished', 'LEFT', 'LEFT');
console.log(res12); // 'bike:2'
const res13 = await client.lRange('bikes:repairs', 0, -1);
console.log(res13); // ['bike:1']
const res14 = await client.lRange('bikes:finished', 0, -1);
console.log(res14); // ['bike:2']
const res15 = await client.rPush('bikes:repairs', 'bike:1');
console.log(res15); // 1
const res16 = await client.rPush('bikes:repairs', 'bike:2');
console.log(res16); // 2
const res17 = await client.lPush('bikes:repairs', 'bike:important_bike');
console.log(res17); // 3
const res18 = await client.lRange('bikes:repairs', 0, -1);
console.log(res18); // ['bike:important_bike', 'bike:1', 'bike:2']
const res19 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res19); // 3
const res20 = await client.lPush(
'bikes:repairs', ['bike:important_bike', 'bike:very_important_bike']
);
console.log(res20); // 5
const res21 = await client.lRange('bikes:repairs', 0, -1);
console.log(res21); // ['bike:very_important_bike', 'bike:important_bike', 'bike:1', 'bike:2', 'bike:3']
const res22 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res22); // 3
const res23 = await client.rPop('bikes:repairs');
console.log(res23); // 'bike:3'
const res24 = await client.lPop('bikes:repairs');
console.log(res24); // 'bike:1'
const res25 = await client.rPop('bikes:repairs');
console.log(res25); // 'bike:2'
const res26 = await client.rPop('bikes:repairs');
console.log(res26); // None
const res27 = await client.lPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res27); // 5
const res28 = await client.lTrim('bikes:repairs', 0, 2);
console.log(res28); // true
const res29 = await client.lRange('bikes:repairs', 0, -1);
console.log(res29); // ['bike:5', 'bike:4', 'bike:3']
const res27eol = await client.rPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res27eol); // 5
const res28eol = await client.lTrim('bikes:repairs', -3, -1);
console.log(res28eol); // 'OK'
const res29eol = await client.lRange('bikes:repairs', 0, -1);
console.log(res29eol); // ['bike:3', 'bike:4', 'bike:5']
const res31 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2']);
console.log(res31); // 2
const res32 = await client.brPop('bikes:repairs', 1);
console.log(res32); // { key: 'bikes:repairs', element: 'bike:2' }
const res33 = await client.brPop('bikes:repairs', 1);
console.log(res33); // { key: 'bikes:repairs', element: 'bike:1' }
const res34 = await client.brPop('bikes:repairs', 1);
console.log(res34); // null
const res35 = await client.del('new_bikes');
console.log(res35); // 0
const res36 = await client.lPush('new_bikes', ['bike:1', 'bike:2', 'bike:3']);
console.log(res36); // 3
const res37 = await client.set('new_bikes', 'bike:1');
console.log(res37); // 'OK'
const res38 = await client.type('new_bikes');
console.log(res38); // 'string'
try {
const res39 = await client.lPush('new_bikes', 'bike:2', 'bike:3');
// redis.exceptions.ResponseError:
// [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value]
}
catch(e){
console.log(e);
}
await client.lPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res36); // 3
const res40 = await client.exists('bikes:repairs')
console.log(res40); // true
const res41 = await client.lPop('bikes:repairs');
console.log(res41); // 'bike:3'
const res42 = await client.lPop('bikes:repairs');
console.log(res42); // 'bike:2'
const res43 = await client.lPop('bikes:repairs');
console.log(res43); // 'bike:1'
const res44 = await client.exists('bikes:repairs');
console.log(res44); // 0
const res45 = await client.del('bikes:repairs');
console.log(res45); // 0
const res46 = await client.lLen('bikes:repairs');
console.log(res46); // 0
const res47 = await client.lPop('bikes:repairs');
console.log(res47); // null
const res48 = await client.lPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res48); // 5
const res49 = await client.lTrim('bikes:repairs', 0, 2);
console.log(res49); // 'OK'
const res50 = await client.lRange('bikes:repairs', 0, -1);
console.log(res50); // ['bike:5', 'bike:4', 'bike:3']
package io.redis.examples;
import org.junit.Test;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.args.ListDirection;
import java.util.List;
import static org.junit.Assert.*;
public class ListExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
long res1 = jedis.lpush("bikes:repairs", "bike:1");
System.out.println(res1); // >>> 1
long res2 = jedis.lpush("bikes:repairs", "bike:2");
System.out.println(res2); // >>> 2
String res3 = jedis.rpop("bikes:repairs");
System.out.println(res3); // >>> bike:1
String res4 = jedis.rpop("bikes:repairs");
System.out.println(res4); // >>> bike:2
long res5 = jedis.lpush("bikes:repairs", "bike:1");
System.out.println(res5); // >>> 1
long res6 = jedis.lpush("bikes:repairs", "bike:2");
System.out.println(res6); // >>> 2
String res7 = jedis.lpop("bikes:repairs");
System.out.println(res7); // >>> bike:2
String res8 = jedis.lpop("bikes:repairs");
System.out.println(res8); // >>> bike:1
long res9 = jedis.llen("bikes:repairs");
System.out.println(res9); // >>> 0
long res10 = jedis.lpush("bikes:repairs", "bike:1");
System.out.println(res10); // >>> 1
long res11 = jedis.lpush("bikes:repairs", "bike:2");
System.out.println(res11); // >>> 2
String res12 = jedis.lmove("bikes:repairs", "bikes:finished", ListDirection.LEFT, ListDirection.LEFT);
System.out.println(res12); // >>> bike:2
List<String> res13 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res13); // >>> [bike:1]
List<String> res14 = jedis.lrange("bikes:finished", 0, -1);
System.out.println(res14); // >>> [bike:2]
long res15 = jedis.rpush("bikes:repairs", "bike:1");
System.out.println(res15); // >>> 1
long res16 = jedis.rpush("bikes:repairs", "bike:2");
System.out.println(res16); // >>> 2
long res17 = jedis.lpush("bikes:repairs", "bike:important_bike");
System.out.println(res17); // >>> 3
List<String> res18 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res18); // >>> [bike:important_bike, bike:1, bike:2]
long res19 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3");
System.out.println(res19); // >>> 3
long res20 = jedis.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike");
System.out.println(res20); // >>> 5
List<String> res21 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res21); // >>> [bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3]
long res22 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3");
System.out.println(res22); // >>> 3
String res23 = jedis.rpop("bikes:repairs");
System.out.println(res23); // >>> bike:3
String res24 = jedis.lpop("bikes:repairs");
System.out.println(res24); // >>> bike:1
String res25 = jedis.rpop("bikes:repairs");
System.out.println(res25); // >>> bike:2
String res26 = jedis.rpop("bikes:repairs");
System.out.println(res26); // >>> null
long res27 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
System.out.println(res27); // >>> 5
String res28 = jedis.ltrim("bikes:repairs", 0, 2);
System.out.println(res28); // >>> OK
List<String> res29 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res29); // >>> [bike:5, bike:4, bike:3]
res27 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
System.out.println(res27); // >>> 5
res28 = jedis.ltrim("bikes:repairs", -3, -1);
System.out.println(res2); // >>> OK
res29 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res29); // >>> [bike:3, bike:4, bike:5]
long res31 = jedis.rpush("bikes:repairs", "bike:1", "bike:2");
System.out.println(res31); // >>> 2
List<String> res32 = jedis.brpop(1, "bikes:repairs");
System.out.println(res32); // >>> (bikes:repairs, bike:2)
List<String> res33 = jedis.brpop(1,"bikes:repairs");
System.out.println(res33); // >>> (bikes:repairs, bike:1)
List<String> res34 = jedis.brpop(1,"bikes:repairs");
System.out.println(res34); // >>> null
long res35 = jedis.del("new_bikes");
System.out.println(res35); // >>> 0
long res36 = jedis.lpush("new_bikes", "bike:1", "bike:2", "bike:3");
System.out.println(res36); // >>> 3
String res37 = jedis.set("new_bikes", "bike:1");
System.out.println(res37); // >>> OK
String res38 = jedis.type("new_bikes");
System.out.println(res38); // >>> string
try {
long res39 = jedis.lpush("new_bikes", "bike:2", "bike:3");
} catch (Exception e) {
e.printStackTrace();
// >>> redis.clients.jedis.exceptions.JedisDataException:
// >>> WRONGTYPE Operation against a key holding the wrong kind of value
}
jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3");
System.out.println(res36); // >>> 3
boolean res40 = jedis.exists("bikes:repairs");
System.out.println(res40); // >>> true
String res41 = jedis.lpop("bikes:repairs");
System.out.println(res41); // >>> bike:3
String res42 = jedis.lpop("bikes:repairs");
System.out.println(res42); // >>> bike:2
String res43 = jedis.lpop("bikes:repairs");
System.out.println(res43); // >>> bike:1
boolean res44 = jedis.exists("bikes:repairs");
System.out.println(res44); // >>> false
long res45 = jedis.del("bikes:repairs");
System.out.println(res45); // >>> 0
long res46 = jedis.llen("bikes:repairs");
System.out.println(res46); // >>> 0
String res47 = jedis.lpop("bikes:repairs");
System.out.println(res47); // >>> null
long res48 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
System.out.println(res48); // >>> 5
String res49 = jedis.ltrim("bikes:repairs", 0, 2);
System.out.println(res49); // >>> OK
List<String> res50 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res50); // >>> [bike:5, bike:4, bike:3]
}
}
package example_commands_test
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
func ExampleClient_queue() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res1, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res1) // >>> 1
res2, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res2) // >>> 2
res3, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res3) // >>> bike:1
res4, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res4) // >>> bike:2
}
func ExampleClient_stack() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res5, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res5) // >>> 1
res6, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res6) // >>> 2
res7, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res7) // >>> bike:2
res8, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res8) // >>> bike:1
}
func ExampleClient_llen() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res9, err := rdb.LLen(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res9) // >>> 0
}
func ExampleClient_lmove_lrange() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res10, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res10) // >>> 1
res11, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res11) // >>> 2
res12, err := rdb.LMove(ctx, "bikes:repairs", "bikes:finished", "LEFT", "LEFT").Result()
if err != nil {
panic(err)
}
fmt.Println(res12) // >>> bike:2
res13, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res13) // >>> [bike:1]
res14, err := rdb.LRange(ctx, "bikes:finished", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res14) // >>> [bike:2]
}
func ExampleClient_lpush_rpush() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res15, err := rdb.RPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res15) // >>> 1
res16, err := rdb.RPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res16) // >>> 2
res17, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike").Result()
if err != nil {
panic(err)
}
fmt.Println(res17) // >>> 3
res18, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res18) // >>> [bike:important_bike bike:1 bike:2]
}
func ExampleClient_variadic() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res19, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res19) // >>> 3
res20, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike", "bike:very_important_bike").Result()
if err != nil {
panic(err)
}
fmt.Println(res20) // >>> 5
res21, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res21) // >>> [bike:very_important_bike bike:important_bike bike:1 bike:2 bike:3]
}
func ExampleClient_lpop_rpop() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res22, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res22) // >>> 3
res23, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res23) // >>> bike:3
res24, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res24) // >>> bike:1
res25, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res25) // >>> bike:2
res26, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
fmt.Println(err) // >>> redis: nil
}
fmt.Println(res26) // >>> <empty string>
}
func ExampleClient_ltrim() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res27, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
fmt.Println(res27) // >>> 5
res28, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result()
if err != nil {
panic(err)
}
fmt.Println(res28) // >>> OK
res29, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res29) // >>> [bike:5 bike:4 bike:3]
}
func ExampleClient_ltrim_end_of_list() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res30, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
fmt.Println(res30) // >>> 5
res31, err := rdb.LTrim(ctx, "bikes:repairs", -3, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res31) // >>> OK
res32, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res32) // >>> [bike:3 bike:4 bike:5]
}
func ExampleClient_brpop() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res33, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res33) // >>> 2
res34, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res34) // >>> [bikes:repairs bike:2]
res35, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res35) // >>> [bikes:repairs bike:1]
res36, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
if err != nil {
fmt.Println(err) // >>> redis: nil
}
fmt.Println(res36) // >>> []
}
func ExampleClient_rule1() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res37, err := rdb.Del(ctx, "new_bikes").Result()
if err != nil {
panic(err)
}
fmt.Println(res37) // >>> 0
res38, err := rdb.LPush(ctx, "new_bikes", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res38) // >>> 3
}
func ExampleClient_rule11() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res39, err := rdb.Set(ctx, "new_bikes", "bike:1", 0).Result()
if err != nil {
panic(err)
}
fmt.Println(res39) // >>> OK
res40, err := rdb.Type(ctx, "new_bikes").Result()
if err != nil {
panic(err)
}
fmt.Println(res40) // >>> string
res41, err := rdb.LPush(ctx, "new_bikes", "bike:2", "bike:3").Result()
if err != nil {
fmt.Println(err)
// >>> WRONGTYPE Operation against a key holding the wrong kind of value
}
fmt.Println(res41)
}
func ExampleClient_rule2() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res42, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res42) // >>> 3
res43, err := rdb.Exists(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res43) // >>> 1
res44, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res44) // >>> bike:3
res45, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res45) // >>> bike:2
res46, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res46) // >>> bike:1
res47, err := rdb.Exists(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res47) // >>> 0
}
func ExampleClient_rule3() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res48, err := rdb.Del(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res48) // >>> 0
res49, err := rdb.LLen(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res49) // >>> 0
res50, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
fmt.Println(err) // >>> redis: nil
}
fmt.Println(res50) // >>> <empty string>
}
func ExampleClient_ltrim1() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res51, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
fmt.Println(res51) // >>> 5
res52, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result()
if err != nil {
panic(err)
}
fmt.Println(res52) // >>> OK
res53, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res53) // >>> [bike:5 bike:4 bike:3]
}
public class ListExample
{
public void run()
{
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
long res1 = db.ListLeftPush("bikes:repairs", "bike:1");
Console.WriteLine(res1); // >>> 1
long res2 = db.ListLeftPush("bikes:repairs", "bike:2");
Console.WriteLine(res2); // >>> 2
RedisValue res3 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res3); // >>> "bike:1"
RedisValue res4 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res4); // >>> "bike:2"
long res5 = db.ListLeftPush("bikes:repairs", "bike:1");
Console.WriteLine(res5); // >>> 1
long res6 = db.ListLeftPush("bikes:repairs", "bike:2");
Console.WriteLine(res6); // >>> 2
RedisValue res7 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res7); // >>> "bike:2"
RedisValue res8 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res8); // >>> "bike:1"
long res9 = db.ListLength("bikes:repairs");
Console.WriteLine(res9); // >>> 0
long res10 = db.ListLeftPush("{bikes}:repairs", "bike:1");
Console.WriteLine(res10); // >>> 1
long res11 = db.ListLeftPush("{bikes}:repairs", "bike:2");
Console.WriteLine(res11); // >>> 2
RedisValue res12 = db.ListMove("{bikes}:repairs", "{bikes}:finished", ListSide.Left, ListSide.Left);
Console.Write(res12); // >>> "bike:2"
RedisValue[] res13 = db.ListRange("{bikes}:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res13)); // >>> "bike:1"
RedisValue[] res14 = db.ListRange("{bikes}:finished", 0, -1);
Console.WriteLine(string.Join(", ", res14)); // >>> "bike:2"
long res15 = db.ListRightPush("bikes:repairs", "bike:1");
Console.WriteLine(res15); // >>> 1
long res16 = db.ListRightPush("bikes:repairs", "bike:2");
Console.WriteLine(res16); // >>> 2
long res17 = db.ListLeftPush("bikes:repairs", "bike:important_bike");
Console.WriteLine(res17); // >>> 3
RedisValue[] res18 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res18)); // >>> "bike:important_bike, bike:1, bike:2"
long res19 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res19); // >>> 3
long res20 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:important_bike", "bike:very_important_bike" });
Console.WriteLine(res20); // >>> 5
RedisValue[] res21 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res21));
// >>> "bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3"
long res22 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res22); // >>> 3
RedisValue res23 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res23); // >>> "bike:3"
RedisValue res24 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res24); // >>> "bike:1"
RedisValue res25 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res25); // >>> "bike:2"
RedisValue res26 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res26); // >>> <Empty string>
long res27 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
Console.WriteLine(res27); // >>> 5
db.ListTrim("bikes:repairs", 0, 2);
RedisValue[] res28 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res28)); // "bike:5, bike:4, bike:3"
long res29 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
Console.WriteLine(res29); // >>> 5
db.ListTrim("bikes:repairs", -3, -1);
RedisValue[] res30 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res30)); // >>> "bike:3, bike:4, bike:5"
long res31 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2" });
Console.WriteLine(res31); // >>> 2
Tuple<RedisKey, RedisValue>? res32 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1);
if (res32 != null)
Console.WriteLine($"{res32.Item1} -> {res32.Item2}"); // >>> "bikes:repairs -> bike:2"
Tuple<RedisKey, RedisValue>? res33 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1);
if (res33 != null)
Console.WriteLine($"{res33.Item1} -> {res33.Item2}"); // >>> "bikes:repairs -> bike:1"
Tuple<RedisKey, RedisValue>? res34 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1);
Console.WriteLine(res34); // >>> "Null"
bool res35 = db.KeyDelete("new_bikes");
Console.WriteLine(res35); // >>> False
long res36 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res36); // >>> 3
bool res37 = db.StringSet("new_bikes", "bike:1");
Console.WriteLine(res37); // >>> True
RedisType res38 = db.KeyType("new_bikes");
Console.WriteLine(res38); // >>> RedisType.String
try
{
long res39 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:2", "bike:3" });
}
catch (Exception e)
{
Console.WriteLine(e);
}
long res40 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res40); // >>> 3
bool res41 = db.KeyExists("bikes:repairs");
Console.WriteLine(res41); // >>> True
RedisValue res42 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res42); // >>> "bike:3"
RedisValue res43 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res43); // >>> "bike:2"
RedisValue res44 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res44); // >>> "bike:1"
bool res45 = db.KeyExists("bikes:repairs");
Console.WriteLine(res45); // >>> False
bool res46 = db.KeyDelete("bikes:repairs");
Console.WriteLine(res46); // >>> False
long res47 = db.ListLength("bikes:repairs");
Console.WriteLine(res47); // >>> 0
RedisValue res48 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res48); // >>> Null
long res49 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
Console.WriteLine(res49); // >>> 5
db.ListTrim("bikes:repairs", 0, 2);
RedisValue[] res50 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res50)); // >>> "bike:5, bike:4, bike:3"
}
}
An important operation defined on Redis lists is the ability to pop elements. Popping elements is the operation of both retrieving the element from the list, and eliminating it from the list, at the same time. You can pop elements from left and right, similarly to how you can push elements in both sides of the list. We'll add three elements and pop three elements, so at the end of this sequence of commands the list is empty and there are no more elements to pop:
> RPUSH bikes:repairs bike:1 bike:2 bike:3
(integer) 3
> RPOP bikes:repairs
"bike:3"
> LPOP bikes:repairs
"bike:1"
> RPOP bikes:repairs
"bike:2"
> RPOP bikes:repairs
(nil)
"""
Code samples for List doc pages:
https://redis.io/docs/latest/develop/data-types/lists/
"""
import redis
r = redis.Redis(decode_responses=True)
res1 = r.lpush("bikes:repairs", "bike:1")
print(res1) # >>> 1
res2 = r.lpush("bikes:repairs", "bike:2")
print(res2) # >>> 2
res3 = r.rpop("bikes:repairs")
print(res3) # >>> bike:1
res4 = r.rpop("bikes:repairs")
print(res4) # >>> bike:2
res5 = r.lpush("bikes:repairs", "bike:1")
print(res5) # >>> 1
res6 = r.lpush("bikes:repairs", "bike:2")
print(res6) # >>> 2
res7 = r.lpop("bikes:repairs")
print(res7) # >>> bike:2
res8 = r.lpop("bikes:repairs")
print(res8) # >>> bike:1
res9 = r.llen("bikes:repairs")
print(res9) # >>> 0
res10 = r.lpush("bikes:repairs", "bike:1")
print(res10) # >>> 1
res11 = r.lpush("bikes:repairs", "bike:2")
print(res11) # >>> 2
res12 = r.lmove("bikes:repairs", "bikes:finished", "LEFT", "LEFT")
print(res12) # >>> 'bike:2'
res13 = r.lrange("bikes:repairs", 0, -1)
print(res13) # >>> ['bike:1']
res14 = r.lrange("bikes:finished", 0, -1)
print(res14) # >>> ['bike:2']
res15 = r.rpush("bikes:repairs", "bike:1")
print(res15) # >>> 1
res16 = r.rpush("bikes:repairs", "bike:2")
print(res16) # >>> 2
res17 = r.lpush("bikes:repairs", "bike:important_bike")
print(res17) # >>> 3
res18 = r.lrange("bikes:repairs", 0, -1)
print(res18) # >>> ['bike:important_bike', 'bike:1', 'bike:2']
res19 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res19) # >>> 3
res20 = r.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike")
print(res20) # >>> 5
res21 = r.lrange("bikes:repairs", 0, -1)
print(
res21
) # >>> ['bike:very_important_bike', 'bike:important_bike', 'bike:1', ...
res22 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res22) # >>> 3
res23 = r.rpop("bikes:repairs")
print(res23) # >>> 'bike:3'
res24 = r.lpop("bikes:repairs")
print(res24) # >>> 'bike:1'
res25 = r.rpop("bikes:repairs")
print(res25) # >>> 'bike:2'
res26 = r.rpop("bikes:repairs")
print(res26) # >>> None
res27 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res27) # >>> 5
res28 = r.ltrim("bikes:repairs", 0, 2)
print(res28) # >>> True
res29 = r.lrange("bikes:repairs", 0, -1)
print(res29) # >>> ['bike:5', 'bike:4', 'bike:3']
res27 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res27) # >>> 5
res28 = r.ltrim("bikes:repairs", -3, -1)
print(res28) # >>> True
res29 = r.lrange("bikes:repairs", 0, -1)
print(res29) # >>> ['bike:3', 'bike:4', 'bike:5']
res31 = r.rpush("bikes:repairs", "bike:1", "bike:2")
print(res31) # >>> 2
res32 = r.brpop("bikes:repairs", timeout=1)
print(res32) # >>> ('bikes:repairs', 'bike:2')
res33 = r.brpop("bikes:repairs", timeout=1)
print(res33) # >>> ('bikes:repairs', 'bike:1')
res34 = r.brpop("bikes:repairs", timeout=1)
print(res34) # >>> None
res35 = r.delete("new_bikes")
print(res35) # >>> 0
res36 = r.lpush("new_bikes", "bike:1", "bike:2", "bike:3")
print(res36) # >>> 3
res37 = r.set("new_bikes", "bike:1")
print(res37) # >>> True
res38 = r.type("new_bikes")
print(res38) # >>> 'string'
try:
res39 = r.lpush("new_bikes", "bike:2", "bike:3")
# >>> redis.exceptions.ResponseError:
# >>> WRONGTYPE Operation against a key holding the wrong kind of value
except redis.exceptions.ResponseError as e:
print(e)
r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res36) # >>> 3
res40 = r.exists("bikes:repairs")
print(res40) # >>> 1
res41 = r.lpop("bikes:repairs")
print(res41) # >>> 'bike:3'
res42 = r.lpop("bikes:repairs")
print(res42) # >>> 'bike:2'
res43 = r.lpop("bikes:repairs")
print(res43) # >>> 'bike:1'
res44 = r.exists("bikes:repairs")
print(res44) # >>> False
res45 = r.delete("bikes:repairs")
print(res45) # >>> 0
res46 = r.llen("bikes:repairs")
print(res46) # >>> 0
res47 = r.lpop("bikes:repairs")
print(res47) # >>> None
res48 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res48) # >>> 5
res49 = r.ltrim("bikes:repairs", 0, 2)
print(res49) # >>> True
res50 = r.lrange("bikes:repairs", 0, -1)
print(res50) # >>> ['bike:5', 'bike:4', 'bike:3']
import assert from 'assert';
import { createClient } from 'redis';
const client = await createClient();
await client.connect();
const res1 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res1); // 1
const res2 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res2); // 2
const res3 = await client.rPop('bikes:repairs');
console.log(res3); // bike:1
const res4 = await client.rPop('bikes:repairs');
console.log(res4); // bike:2
const res5 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res5); // 1
const res6 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res6); // 2
const res7 = await client.lPop('bikes:repairs');
console.log(res7); // bike:2
const res8 = await client.lPop('bikes:repairs');
console.log(res8); // bike:1
const res9 = await client.lLen('bikes:repairs');
console.log(res9); // 0
const res10 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res10); // 1
const res11 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res11); // 2
const res12 = await client.lMove('bikes:repairs', 'bikes:finished', 'LEFT', 'LEFT');
console.log(res12); // 'bike:2'
const res13 = await client.lRange('bikes:repairs', 0, -1);
console.log(res13); // ['bike:1']
const res14 = await client.lRange('bikes:finished', 0, -1);
console.log(res14); // ['bike:2']
const res15 = await client.rPush('bikes:repairs', 'bike:1');
console.log(res15); // 1
const res16 = await client.rPush('bikes:repairs', 'bike:2');
console.log(res16); // 2
const res17 = await client.lPush('bikes:repairs', 'bike:important_bike');
console.log(res17); // 3
const res18 = await client.lRange('bikes:repairs', 0, -1);
console.log(res18); // ['bike:important_bike', 'bike:1', 'bike:2']
const res19 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res19); // 3
const res20 = await client.lPush(
'bikes:repairs', ['bike:important_bike', 'bike:very_important_bike']
);
console.log(res20); // 5
const res21 = await client.lRange('bikes:repairs', 0, -1);
console.log(res21); // ['bike:very_important_bike', 'bike:important_bike', 'bike:1', 'bike:2', 'bike:3']
const res22 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res22); // 3
const res23 = await client.rPop('bikes:repairs');
console.log(res23); // 'bike:3'
const res24 = await client.lPop('bikes:repairs');
console.log(res24); // 'bike:1'
const res25 = await client.rPop('bikes:repairs');
console.log(res25); // 'bike:2'
const res26 = await client.rPop('bikes:repairs');
console.log(res26); // None
const res27 = await client.lPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res27); // 5
const res28 = await client.lTrim('bikes:repairs', 0, 2);
console.log(res28); // true
const res29 = await client.lRange('bikes:repairs', 0, -1);
console.log(res29); // ['bike:5', 'bike:4', 'bike:3']
const res27eol = await client.rPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res27eol); // 5
const res28eol = await client.lTrim('bikes:repairs', -3, -1);
console.log(res28eol); // 'OK'
const res29eol = await client.lRange('bikes:repairs', 0, -1);
console.log(res29eol); // ['bike:3', 'bike:4', 'bike:5']
const res31 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2']);
console.log(res31); // 2
const res32 = await client.brPop('bikes:repairs', 1);
console.log(res32); // { key: 'bikes:repairs', element: 'bike:2' }
const res33 = await client.brPop('bikes:repairs', 1);
console.log(res33); // { key: 'bikes:repairs', element: 'bike:1' }
const res34 = await client.brPop('bikes:repairs', 1);
console.log(res34); // null
const res35 = await client.del('new_bikes');
console.log(res35); // 0
const res36 = await client.lPush('new_bikes', ['bike:1', 'bike:2', 'bike:3']);
console.log(res36); // 3
const res37 = await client.set('new_bikes', 'bike:1');
console.log(res37); // 'OK'
const res38 = await client.type('new_bikes');
console.log(res38); // 'string'
try {
const res39 = await client.lPush('new_bikes', 'bike:2', 'bike:3');
// redis.exceptions.ResponseError:
// [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value]
}
catch(e){
console.log(e);
}
await client.lPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res36); // 3
const res40 = await client.exists('bikes:repairs')
console.log(res40); // true
const res41 = await client.lPop('bikes:repairs');
console.log(res41); // 'bike:3'
const res42 = await client.lPop('bikes:repairs');
console.log(res42); // 'bike:2'
const res43 = await client.lPop('bikes:repairs');
console.log(res43); // 'bike:1'
const res44 = await client.exists('bikes:repairs');
console.log(res44); // 0
const res45 = await client.del('bikes:repairs');
console.log(res45); // 0
const res46 = await client.lLen('bikes:repairs');
console.log(res46); // 0
const res47 = await client.lPop('bikes:repairs');
console.log(res47); // null
const res48 = await client.lPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res48); // 5
const res49 = await client.lTrim('bikes:repairs', 0, 2);
console.log(res49); // 'OK'
const res50 = await client.lRange('bikes:repairs', 0, -1);
console.log(res50); // ['bike:5', 'bike:4', 'bike:3']
package io.redis.examples;
import org.junit.Test;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.args.ListDirection;
import java.util.List;
import static org.junit.Assert.*;
public class ListExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
long res1 = jedis.lpush("bikes:repairs", "bike:1");
System.out.println(res1); // >>> 1
long res2 = jedis.lpush("bikes:repairs", "bike:2");
System.out.println(res2); // >>> 2
String res3 = jedis.rpop("bikes:repairs");
System.out.println(res3); // >>> bike:1
String res4 = jedis.rpop("bikes:repairs");
System.out.println(res4); // >>> bike:2
long res5 = jedis.lpush("bikes:repairs", "bike:1");
System.out.println(res5); // >>> 1
long res6 = jedis.lpush("bikes:repairs", "bike:2");
System.out.println(res6); // >>> 2
String res7 = jedis.lpop("bikes:repairs");
System.out.println(res7); // >>> bike:2
String res8 = jedis.lpop("bikes:repairs");
System.out.println(res8); // >>> bike:1
long res9 = jedis.llen("bikes:repairs");
System.out.println(res9); // >>> 0
long res10 = jedis.lpush("bikes:repairs", "bike:1");
System.out.println(res10); // >>> 1
long res11 = jedis.lpush("bikes:repairs", "bike:2");
System.out.println(res11); // >>> 2
String res12 = jedis.lmove("bikes:repairs", "bikes:finished", ListDirection.LEFT, ListDirection.LEFT);
System.out.println(res12); // >>> bike:2
List<String> res13 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res13); // >>> [bike:1]
List<String> res14 = jedis.lrange("bikes:finished", 0, -1);
System.out.println(res14); // >>> [bike:2]
long res15 = jedis.rpush("bikes:repairs", "bike:1");
System.out.println(res15); // >>> 1
long res16 = jedis.rpush("bikes:repairs", "bike:2");
System.out.println(res16); // >>> 2
long res17 = jedis.lpush("bikes:repairs", "bike:important_bike");
System.out.println(res17); // >>> 3
List<String> res18 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res18); // >>> [bike:important_bike, bike:1, bike:2]
long res19 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3");
System.out.println(res19); // >>> 3
long res20 = jedis.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike");
System.out.println(res20); // >>> 5
List<String> res21 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res21); // >>> [bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3]
long res22 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3");
System.out.println(res22); // >>> 3
String res23 = jedis.rpop("bikes:repairs");
System.out.println(res23); // >>> bike:3
String res24 = jedis.lpop("bikes:repairs");
System.out.println(res24); // >>> bike:1
String res25 = jedis.rpop("bikes:repairs");
System.out.println(res25); // >>> bike:2
String res26 = jedis.rpop("bikes:repairs");
System.out.println(res26); // >>> null
long res27 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
System.out.println(res27); // >>> 5
String res28 = jedis.ltrim("bikes:repairs", 0, 2);
System.out.println(res28); // >>> OK
List<String> res29 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res29); // >>> [bike:5, bike:4, bike:3]
res27 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
System.out.println(res27); // >>> 5
res28 = jedis.ltrim("bikes:repairs", -3, -1);
System.out.println(res2); // >>> OK
res29 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res29); // >>> [bike:3, bike:4, bike:5]
long res31 = jedis.rpush("bikes:repairs", "bike:1", "bike:2");
System.out.println(res31); // >>> 2
List<String> res32 = jedis.brpop(1, "bikes:repairs");
System.out.println(res32); // >>> (bikes:repairs, bike:2)
List<String> res33 = jedis.brpop(1,"bikes:repairs");
System.out.println(res33); // >>> (bikes:repairs, bike:1)
List<String> res34 = jedis.brpop(1,"bikes:repairs");
System.out.println(res34); // >>> null
long res35 = jedis.del("new_bikes");
System.out.println(res35); // >>> 0
long res36 = jedis.lpush("new_bikes", "bike:1", "bike:2", "bike:3");
System.out.println(res36); // >>> 3
String res37 = jedis.set("new_bikes", "bike:1");
System.out.println(res37); // >>> OK
String res38 = jedis.type("new_bikes");
System.out.println(res38); // >>> string
try {
long res39 = jedis.lpush("new_bikes", "bike:2", "bike:3");
} catch (Exception e) {
e.printStackTrace();
// >>> redis.clients.jedis.exceptions.JedisDataException:
// >>> WRONGTYPE Operation against a key holding the wrong kind of value
}
jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3");
System.out.println(res36); // >>> 3
boolean res40 = jedis.exists("bikes:repairs");
System.out.println(res40); // >>> true
String res41 = jedis.lpop("bikes:repairs");
System.out.println(res41); // >>> bike:3
String res42 = jedis.lpop("bikes:repairs");
System.out.println(res42); // >>> bike:2
String res43 = jedis.lpop("bikes:repairs");
System.out.println(res43); // >>> bike:1
boolean res44 = jedis.exists("bikes:repairs");
System.out.println(res44); // >>> false
long res45 = jedis.del("bikes:repairs");
System.out.println(res45); // >>> 0
long res46 = jedis.llen("bikes:repairs");
System.out.println(res46); // >>> 0
String res47 = jedis.lpop("bikes:repairs");
System.out.println(res47); // >>> null
long res48 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
System.out.println(res48); // >>> 5
String res49 = jedis.ltrim("bikes:repairs", 0, 2);
System.out.println(res49); // >>> OK
List<String> res50 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res50); // >>> [bike:5, bike:4, bike:3]
}
}
package example_commands_test
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
func ExampleClient_queue() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res1, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res1) // >>> 1
res2, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res2) // >>> 2
res3, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res3) // >>> bike:1
res4, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res4) // >>> bike:2
}
func ExampleClient_stack() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res5, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res5) // >>> 1
res6, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res6) // >>> 2
res7, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res7) // >>> bike:2
res8, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res8) // >>> bike:1
}
func ExampleClient_llen() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res9, err := rdb.LLen(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res9) // >>> 0
}
func ExampleClient_lmove_lrange() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res10, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res10) // >>> 1
res11, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res11) // >>> 2
res12, err := rdb.LMove(ctx, "bikes:repairs", "bikes:finished", "LEFT", "LEFT").Result()
if err != nil {
panic(err)
}
fmt.Println(res12) // >>> bike:2
res13, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res13) // >>> [bike:1]
res14, err := rdb.LRange(ctx, "bikes:finished", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res14) // >>> [bike:2]
}
func ExampleClient_lpush_rpush() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res15, err := rdb.RPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res15) // >>> 1
res16, err := rdb.RPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res16) // >>> 2
res17, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike").Result()
if err != nil {
panic(err)
}
fmt.Println(res17) // >>> 3
res18, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res18) // >>> [bike:important_bike bike:1 bike:2]
}
func ExampleClient_variadic() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res19, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res19) // >>> 3
res20, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike", "bike:very_important_bike").Result()
if err != nil {
panic(err)
}
fmt.Println(res20) // >>> 5
res21, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res21) // >>> [bike:very_important_bike bike:important_bike bike:1 bike:2 bike:3]
}
func ExampleClient_lpop_rpop() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res22, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res22) // >>> 3
res23, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res23) // >>> bike:3
res24, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res24) // >>> bike:1
res25, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res25) // >>> bike:2
res26, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
fmt.Println(err) // >>> redis: nil
}
fmt.Println(res26) // >>> <empty string>
}
func ExampleClient_ltrim() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res27, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
fmt.Println(res27) // >>> 5
res28, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result()
if err != nil {
panic(err)
}
fmt.Println(res28) // >>> OK
res29, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res29) // >>> [bike:5 bike:4 bike:3]
}
func ExampleClient_ltrim_end_of_list() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res30, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
fmt.Println(res30) // >>> 5
res31, err := rdb.LTrim(ctx, "bikes:repairs", -3, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res31) // >>> OK
res32, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res32) // >>> [bike:3 bike:4 bike:5]
}
func ExampleClient_brpop() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res33, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res33) // >>> 2
res34, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res34) // >>> [bikes:repairs bike:2]
res35, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res35) // >>> [bikes:repairs bike:1]
res36, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
if err != nil {
fmt.Println(err) // >>> redis: nil
}
fmt.Println(res36) // >>> []
}
func ExampleClient_rule1() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res37, err := rdb.Del(ctx, "new_bikes").Result()
if err != nil {
panic(err)
}
fmt.Println(res37) // >>> 0
res38, err := rdb.LPush(ctx, "new_bikes", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res38) // >>> 3
}
func ExampleClient_rule11() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res39, err := rdb.Set(ctx, "new_bikes", "bike:1", 0).Result()
if err != nil {
panic(err)
}
fmt.Println(res39) // >>> OK
res40, err := rdb.Type(ctx, "new_bikes").Result()
if err != nil {
panic(err)
}
fmt.Println(res40) // >>> string
res41, err := rdb.LPush(ctx, "new_bikes", "bike:2", "bike:3").Result()
if err != nil {
fmt.Println(err)
// >>> WRONGTYPE Operation against a key holding the wrong kind of value
}
fmt.Println(res41)
}
func ExampleClient_rule2() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res42, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res42) // >>> 3
res43, err := rdb.Exists(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res43) // >>> 1
res44, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res44) // >>> bike:3
res45, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res45) // >>> bike:2
res46, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res46) // >>> bike:1
res47, err := rdb.Exists(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res47) // >>> 0
}
func ExampleClient_rule3() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res48, err := rdb.Del(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res48) // >>> 0
res49, err := rdb.LLen(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res49) // >>> 0
res50, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
fmt.Println(err) // >>> redis: nil
}
fmt.Println(res50) // >>> <empty string>
}
func ExampleClient_ltrim1() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res51, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
fmt.Println(res51) // >>> 5
res52, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result()
if err != nil {
panic(err)
}
fmt.Println(res52) // >>> OK
res53, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res53) // >>> [bike:5 bike:4 bike:3]
}
public class ListExample
{
public void run()
{
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
long res1 = db.ListLeftPush("bikes:repairs", "bike:1");
Console.WriteLine(res1); // >>> 1
long res2 = db.ListLeftPush("bikes:repairs", "bike:2");
Console.WriteLine(res2); // >>> 2
RedisValue res3 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res3); // >>> "bike:1"
RedisValue res4 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res4); // >>> "bike:2"
long res5 = db.ListLeftPush("bikes:repairs", "bike:1");
Console.WriteLine(res5); // >>> 1
long res6 = db.ListLeftPush("bikes:repairs", "bike:2");
Console.WriteLine(res6); // >>> 2
RedisValue res7 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res7); // >>> "bike:2"
RedisValue res8 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res8); // >>> "bike:1"
long res9 = db.ListLength("bikes:repairs");
Console.WriteLine(res9); // >>> 0
long res10 = db.ListLeftPush("{bikes}:repairs", "bike:1");
Console.WriteLine(res10); // >>> 1
long res11 = db.ListLeftPush("{bikes}:repairs", "bike:2");
Console.WriteLine(res11); // >>> 2
RedisValue res12 = db.ListMove("{bikes}:repairs", "{bikes}:finished", ListSide.Left, ListSide.Left);
Console.Write(res12); // >>> "bike:2"
RedisValue[] res13 = db.ListRange("{bikes}:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res13)); // >>> "bike:1"
RedisValue[] res14 = db.ListRange("{bikes}:finished", 0, -1);
Console.WriteLine(string.Join(", ", res14)); // >>> "bike:2"
long res15 = db.ListRightPush("bikes:repairs", "bike:1");
Console.WriteLine(res15); // >>> 1
long res16 = db.ListRightPush("bikes:repairs", "bike:2");
Console.WriteLine(res16); // >>> 2
long res17 = db.ListLeftPush("bikes:repairs", "bike:important_bike");
Console.WriteLine(res17); // >>> 3
RedisValue[] res18 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res18)); // >>> "bike:important_bike, bike:1, bike:2"
long res19 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res19); // >>> 3
long res20 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:important_bike", "bike:very_important_bike" });
Console.WriteLine(res20); // >>> 5
RedisValue[] res21 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res21));
// >>> "bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3"
long res22 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res22); // >>> 3
RedisValue res23 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res23); // >>> "bike:3"
RedisValue res24 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res24); // >>> "bike:1"
RedisValue res25 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res25); // >>> "bike:2"
RedisValue res26 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res26); // >>> <Empty string>
long res27 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
Console.WriteLine(res27); // >>> 5
db.ListTrim("bikes:repairs", 0, 2);
RedisValue[] res28 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res28)); // "bike:5, bike:4, bike:3"
long res29 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
Console.WriteLine(res29); // >>> 5
db.ListTrim("bikes:repairs", -3, -1);
RedisValue[] res30 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res30)); // >>> "bike:3, bike:4, bike:5"
long res31 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2" });
Console.WriteLine(res31); // >>> 2
Tuple<RedisKey, RedisValue>? res32 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1);
if (res32 != null)
Console.WriteLine($"{res32.Item1} -> {res32.Item2}"); // >>> "bikes:repairs -> bike:2"
Tuple<RedisKey, RedisValue>? res33 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1);
if (res33 != null)
Console.WriteLine($"{res33.Item1} -> {res33.Item2}"); // >>> "bikes:repairs -> bike:1"
Tuple<RedisKey, RedisValue>? res34 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1);
Console.WriteLine(res34); // >>> "Null"
bool res35 = db.KeyDelete("new_bikes");
Console.WriteLine(res35); // >>> False
long res36 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res36); // >>> 3
bool res37 = db.StringSet("new_bikes", "bike:1");
Console.WriteLine(res37); // >>> True
RedisType res38 = db.KeyType("new_bikes");
Console.WriteLine(res38); // >>> RedisType.String
try
{
long res39 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:2", "bike:3" });
}
catch (Exception e)
{
Console.WriteLine(e);
}
long res40 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res40); // >>> 3
bool res41 = db.KeyExists("bikes:repairs");
Console.WriteLine(res41); // >>> True
RedisValue res42 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res42); // >>> "bike:3"
RedisValue res43 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res43); // >>> "bike:2"
RedisValue res44 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res44); // >>> "bike:1"
bool res45 = db.KeyExists("bikes:repairs");
Console.WriteLine(res45); // >>> False
bool res46 = db.KeyDelete("bikes:repairs");
Console.WriteLine(res46); // >>> False
long res47 = db.ListLength("bikes:repairs");
Console.WriteLine(res47); // >>> 0
RedisValue res48 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res48); // >>> Null
long res49 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
Console.WriteLine(res49); // >>> 5
db.ListTrim("bikes:repairs", 0, 2);
RedisValue[] res50 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res50)); // >>> "bike:5, bike:4, bike:3"
}
}
Redis returned a NULL value to signal that there are no elements in the list.
Common use cases for lists
Lists are useful for a number of tasks, two very representative use cases are the following:
- Remember the latest updates posted by users into a social network.
- Communication between processes, using a consumer-producer pattern where the producer pushes items into a list, and a consumer (usually a worker) consumes those items and executes actions. Redis has special list commands to make this use case both more reliable and efficient.
For example both the popular Ruby libraries resque and sidekiq use Redis lists under the hood in order to implement background jobs.
The popular Twitter social network takes the latest tweets posted by users into Redis lists.
To describe a common use case step by step, imagine your home page shows the latest photos published in a photo sharing social network and you want to speedup access.
- Every time a user posts a new photo, we add its ID into a list with
LPUSH
. - When users visit the home page, we use
LRANGE 0 9
in order to get the latest 10 posted items.
Capped lists
In many use cases we just want to use lists to store the latest items, whatever they are: social network updates, logs, or anything else.
Redis allows us to use lists as a capped collection, only remembering the latest
N items and discarding all the oldest items using the LTRIM
command.
The LTRIM
command is similar to LRANGE
, but instead of displaying the
specified range of elements it sets this range as the new list value. All
the elements outside the given range are removed.
For example, if you're adding bikes on the end of a list of repairs, but only want to worry about the 3 that have been on the list the longest:
> RPUSH bikes:repairs bike:1 bike:2 bike:3 bike:4 bike:5
(integer) 5
> LTRIM bikes:repairs 0 2
OK
> LRANGE bikes:repairs 0 -1
1) "bike:1"
2) "bike:2"
3) "bike:3"
"""
Code samples for List doc pages:
https://redis.io/docs/latest/develop/data-types/lists/
"""
import redis
r = redis.Redis(decode_responses=True)
res1 = r.lpush("bikes:repairs", "bike:1")
print(res1) # >>> 1
res2 = r.lpush("bikes:repairs", "bike:2")
print(res2) # >>> 2
res3 = r.rpop("bikes:repairs")
print(res3) # >>> bike:1
res4 = r.rpop("bikes:repairs")
print(res4) # >>> bike:2
res5 = r.lpush("bikes:repairs", "bike:1")
print(res5) # >>> 1
res6 = r.lpush("bikes:repairs", "bike:2")
print(res6) # >>> 2
res7 = r.lpop("bikes:repairs")
print(res7) # >>> bike:2
res8 = r.lpop("bikes:repairs")
print(res8) # >>> bike:1
res9 = r.llen("bikes:repairs")
print(res9) # >>> 0
res10 = r.lpush("bikes:repairs", "bike:1")
print(res10) # >>> 1
res11 = r.lpush("bikes:repairs", "bike:2")
print(res11) # >>> 2
res12 = r.lmove("bikes:repairs", "bikes:finished", "LEFT", "LEFT")
print(res12) # >>> 'bike:2'
res13 = r.lrange("bikes:repairs", 0, -1)
print(res13) # >>> ['bike:1']
res14 = r.lrange("bikes:finished", 0, -1)
print(res14) # >>> ['bike:2']
res15 = r.rpush("bikes:repairs", "bike:1")
print(res15) # >>> 1
res16 = r.rpush("bikes:repairs", "bike:2")
print(res16) # >>> 2
res17 = r.lpush("bikes:repairs", "bike:important_bike")
print(res17) # >>> 3
res18 = r.lrange("bikes:repairs", 0, -1)
print(res18) # >>> ['bike:important_bike', 'bike:1', 'bike:2']
res19 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res19) # >>> 3
res20 = r.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike")
print(res20) # >>> 5
res21 = r.lrange("bikes:repairs", 0, -1)
print(
res21
) # >>> ['bike:very_important_bike', 'bike:important_bike', 'bike:1', ...
res22 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res22) # >>> 3
res23 = r.rpop("bikes:repairs")
print(res23) # >>> 'bike:3'
res24 = r.lpop("bikes:repairs")
print(res24) # >>> 'bike:1'
res25 = r.rpop("bikes:repairs")
print(res25) # >>> 'bike:2'
res26 = r.rpop("bikes:repairs")
print(res26) # >>> None
res27 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res27) # >>> 5
res28 = r.ltrim("bikes:repairs", 0, 2)
print(res28) # >>> True
res29 = r.lrange("bikes:repairs", 0, -1)
print(res29) # >>> ['bike:5', 'bike:4', 'bike:3']
res27 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res27) # >>> 5
res28 = r.ltrim("bikes:repairs", -3, -1)
print(res28) # >>> True
res29 = r.lrange("bikes:repairs", 0, -1)
print(res29) # >>> ['bike:3', 'bike:4', 'bike:5']
res31 = r.rpush("bikes:repairs", "bike:1", "bike:2")
print(res31) # >>> 2
res32 = r.brpop("bikes:repairs", timeout=1)
print(res32) # >>> ('bikes:repairs', 'bike:2')
res33 = r.brpop("bikes:repairs", timeout=1)
print(res33) # >>> ('bikes:repairs', 'bike:1')
res34 = r.brpop("bikes:repairs", timeout=1)
print(res34) # >>> None
res35 = r.delete("new_bikes")
print(res35) # >>> 0
res36 = r.lpush("new_bikes", "bike:1", "bike:2", "bike:3")
print(res36) # >>> 3
res37 = r.set("new_bikes", "bike:1")
print(res37) # >>> True
res38 = r.type("new_bikes")
print(res38) # >>> 'string'
try:
res39 = r.lpush("new_bikes", "bike:2", "bike:3")
# >>> redis.exceptions.ResponseError:
# >>> WRONGTYPE Operation against a key holding the wrong kind of value
except redis.exceptions.ResponseError as e:
print(e)
r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res36) # >>> 3
res40 = r.exists("bikes:repairs")
print(res40) # >>> 1
res41 = r.lpop("bikes:repairs")
print(res41) # >>> 'bike:3'
res42 = r.lpop("bikes:repairs")
print(res42) # >>> 'bike:2'
res43 = r.lpop("bikes:repairs")
print(res43) # >>> 'bike:1'
res44 = r.exists("bikes:repairs")
print(res44) # >>> False
res45 = r.delete("bikes:repairs")
print(res45) # >>> 0
res46 = r.llen("bikes:repairs")
print(res46) # >>> 0
res47 = r.lpop("bikes:repairs")
print(res47) # >>> None
res48 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res48) # >>> 5
res49 = r.ltrim("bikes:repairs", 0, 2)
print(res49) # >>> True
res50 = r.lrange("bikes:repairs", 0, -1)
print(res50) # >>> ['bike:5', 'bike:4', 'bike:3']
import assert from 'assert';
import { createClient } from 'redis';
const client = await createClient();
await client.connect();
const res1 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res1); // 1
const res2 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res2); // 2
const res3 = await client.rPop('bikes:repairs');
console.log(res3); // bike:1
const res4 = await client.rPop('bikes:repairs');
console.log(res4); // bike:2
const res5 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res5); // 1
const res6 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res6); // 2
const res7 = await client.lPop('bikes:repairs');
console.log(res7); // bike:2
const res8 = await client.lPop('bikes:repairs');
console.log(res8); // bike:1
const res9 = await client.lLen('bikes:repairs');
console.log(res9); // 0
const res10 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res10); // 1
const res11 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res11); // 2
const res12 = await client.lMove('bikes:repairs', 'bikes:finished', 'LEFT', 'LEFT');
console.log(res12); // 'bike:2'
const res13 = await client.lRange('bikes:repairs', 0, -1);
console.log(res13); // ['bike:1']
const res14 = await client.lRange('bikes:finished', 0, -1);
console.log(res14); // ['bike:2']
const res15 = await client.rPush('bikes:repairs', 'bike:1');
console.log(res15); // 1
const res16 = await client.rPush('bikes:repairs', 'bike:2');
console.log(res16); // 2
const res17 = await client.lPush('bikes:repairs', 'bike:important_bike');
console.log(res17); // 3
const res18 = await client.lRange('bikes:repairs', 0, -1);
console.log(res18); // ['bike:important_bike', 'bike:1', 'bike:2']
const res19 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res19); // 3
const res20 = await client.lPush(
'bikes:repairs', ['bike:important_bike', 'bike:very_important_bike']
);
console.log(res20); // 5
const res21 = await client.lRange('bikes:repairs', 0, -1);
console.log(res21); // ['bike:very_important_bike', 'bike:important_bike', 'bike:1', 'bike:2', 'bike:3']
const res22 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res22); // 3
const res23 = await client.rPop('bikes:repairs');
console.log(res23); // 'bike:3'
const res24 = await client.lPop('bikes:repairs');
console.log(res24); // 'bike:1'
const res25 = await client.rPop('bikes:repairs');
console.log(res25); // 'bike:2'
const res26 = await client.rPop('bikes:repairs');
console.log(res26); // None
const res27 = await client.lPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res27); // 5
const res28 = await client.lTrim('bikes:repairs', 0, 2);
console.log(res28); // true
const res29 = await client.lRange('bikes:repairs', 0, -1);
console.log(res29); // ['bike:5', 'bike:4', 'bike:3']
const res27eol = await client.rPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res27eol); // 5
const res28eol = await client.lTrim('bikes:repairs', -3, -1);
console.log(res28eol); // 'OK'
const res29eol = await client.lRange('bikes:repairs', 0, -1);
console.log(res29eol); // ['bike:3', 'bike:4', 'bike:5']
const res31 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2']);
console.log(res31); // 2
const res32 = await client.brPop('bikes:repairs', 1);
console.log(res32); // { key: 'bikes:repairs', element: 'bike:2' }
const res33 = await client.brPop('bikes:repairs', 1);
console.log(res33); // { key: 'bikes:repairs', element: 'bike:1' }
const res34 = await client.brPop('bikes:repairs', 1);
console.log(res34); // null
const res35 = await client.del('new_bikes');
console.log(res35); // 0
const res36 = await client.lPush('new_bikes', ['bike:1', 'bike:2', 'bike:3']);
console.log(res36); // 3
const res37 = await client.set('new_bikes', 'bike:1');
console.log(res37); // 'OK'
const res38 = await client.type('new_bikes');
console.log(res38); // 'string'
try {
const res39 = await client.lPush('new_bikes', 'bike:2', 'bike:3');
// redis.exceptions.ResponseError:
// [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value]
}
catch(e){
console.log(e);
}
await client.lPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res36); // 3
const res40 = await client.exists('bikes:repairs')
console.log(res40); // true
const res41 = await client.lPop('bikes:repairs');
console.log(res41); // 'bike:3'
const res42 = await client.lPop('bikes:repairs');
console.log(res42); // 'bike:2'
const res43 = await client.lPop('bikes:repairs');
console.log(res43); // 'bike:1'
const res44 = await client.exists('bikes:repairs');
console.log(res44); // 0
const res45 = await client.del('bikes:repairs');
console.log(res45); // 0
const res46 = await client.lLen('bikes:repairs');
console.log(res46); // 0
const res47 = await client.lPop('bikes:repairs');
console.log(res47); // null
const res48 = await client.lPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res48); // 5
const res49 = await client.lTrim('bikes:repairs', 0, 2);
console.log(res49); // 'OK'
const res50 = await client.lRange('bikes:repairs', 0, -1);
console.log(res50); // ['bike:5', 'bike:4', 'bike:3']
package io.redis.examples;
import org.junit.Test;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.args.ListDirection;
import java.util.List;
import static org.junit.Assert.*;
public class ListExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
long res1 = jedis.lpush("bikes:repairs", "bike:1");
System.out.println(res1); // >>> 1
long res2 = jedis.lpush("bikes:repairs", "bike:2");
System.out.println(res2); // >>> 2
String res3 = jedis.rpop("bikes:repairs");
System.out.println(res3); // >>> bike:1
String res4 = jedis.rpop("bikes:repairs");
System.out.println(res4); // >>> bike:2
long res5 = jedis.lpush("bikes:repairs", "bike:1");
System.out.println(res5); // >>> 1
long res6 = jedis.lpush("bikes:repairs", "bike:2");
System.out.println(res6); // >>> 2
String res7 = jedis.lpop("bikes:repairs");
System.out.println(res7); // >>> bike:2
String res8 = jedis.lpop("bikes:repairs");
System.out.println(res8); // >>> bike:1
long res9 = jedis.llen("bikes:repairs");
System.out.println(res9); // >>> 0
long res10 = jedis.lpush("bikes:repairs", "bike:1");
System.out.println(res10); // >>> 1
long res11 = jedis.lpush("bikes:repairs", "bike:2");
System.out.println(res11); // >>> 2
String res12 = jedis.lmove("bikes:repairs", "bikes:finished", ListDirection.LEFT, ListDirection.LEFT);
System.out.println(res12); // >>> bike:2
List<String> res13 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res13); // >>> [bike:1]
List<String> res14 = jedis.lrange("bikes:finished", 0, -1);
System.out.println(res14); // >>> [bike:2]
long res15 = jedis.rpush("bikes:repairs", "bike:1");
System.out.println(res15); // >>> 1
long res16 = jedis.rpush("bikes:repairs", "bike:2");
System.out.println(res16); // >>> 2
long res17 = jedis.lpush("bikes:repairs", "bike:important_bike");
System.out.println(res17); // >>> 3
List<String> res18 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res18); // >>> [bike:important_bike, bike:1, bike:2]
long res19 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3");
System.out.println(res19); // >>> 3
long res20 = jedis.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike");
System.out.println(res20); // >>> 5
List<String> res21 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res21); // >>> [bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3]
long res22 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3");
System.out.println(res22); // >>> 3
String res23 = jedis.rpop("bikes:repairs");
System.out.println(res23); // >>> bike:3
String res24 = jedis.lpop("bikes:repairs");
System.out.println(res24); // >>> bike:1
String res25 = jedis.rpop("bikes:repairs");
System.out.println(res25); // >>> bike:2
String res26 = jedis.rpop("bikes:repairs");
System.out.println(res26); // >>> null
long res27 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
System.out.println(res27); // >>> 5
String res28 = jedis.ltrim("bikes:repairs", 0, 2);
System.out.println(res28); // >>> OK
List<String> res29 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res29); // >>> [bike:5, bike:4, bike:3]
res27 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
System.out.println(res27); // >>> 5
res28 = jedis.ltrim("bikes:repairs", -3, -1);
System.out.println(res2); // >>> OK
res29 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res29); // >>> [bike:3, bike:4, bike:5]
long res31 = jedis.rpush("bikes:repairs", "bike:1", "bike:2");
System.out.println(res31); // >>> 2
List<String> res32 = jedis.brpop(1, "bikes:repairs");
System.out.println(res32); // >>> (bikes:repairs, bike:2)
List<String> res33 = jedis.brpop(1,"bikes:repairs");
System.out.println(res33); // >>> (bikes:repairs, bike:1)
List<String> res34 = jedis.brpop(1,"bikes:repairs");
System.out.println(res34); // >>> null
long res35 = jedis.del("new_bikes");
System.out.println(res35); // >>> 0
long res36 = jedis.lpush("new_bikes", "bike:1", "bike:2", "bike:3");
System.out.println(res36); // >>> 3
String res37 = jedis.set("new_bikes", "bike:1");
System.out.println(res37); // >>> OK
String res38 = jedis.type("new_bikes");
System.out.println(res38); // >>> string
try {
long res39 = jedis.lpush("new_bikes", "bike:2", "bike:3");
} catch (Exception e) {
e.printStackTrace();
// >>> redis.clients.jedis.exceptions.JedisDataException:
// >>> WRONGTYPE Operation against a key holding the wrong kind of value
}
jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3");
System.out.println(res36); // >>> 3
boolean res40 = jedis.exists("bikes:repairs");
System.out.println(res40); // >>> true
String res41 = jedis.lpop("bikes:repairs");
System.out.println(res41); // >>> bike:3
String res42 = jedis.lpop("bikes:repairs");
System.out.println(res42); // >>> bike:2
String res43 = jedis.lpop("bikes:repairs");
System.out.println(res43); // >>> bike:1
boolean res44 = jedis.exists("bikes:repairs");
System.out.println(res44); // >>> false
long res45 = jedis.del("bikes:repairs");
System.out.println(res45); // >>> 0
long res46 = jedis.llen("bikes:repairs");
System.out.println(res46); // >>> 0
String res47 = jedis.lpop("bikes:repairs");
System.out.println(res47); // >>> null
long res48 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
System.out.println(res48); // >>> 5
String res49 = jedis.ltrim("bikes:repairs", 0, 2);
System.out.println(res49); // >>> OK
List<String> res50 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res50); // >>> [bike:5, bike:4, bike:3]
}
}
package example_commands_test
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
func ExampleClient_queue() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res1, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res1) // >>> 1
res2, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res2) // >>> 2
res3, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res3) // >>> bike:1
res4, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res4) // >>> bike:2
}
func ExampleClient_stack() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res5, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res5) // >>> 1
res6, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res6) // >>> 2
res7, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res7) // >>> bike:2
res8, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res8) // >>> bike:1
}
func ExampleClient_llen() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res9, err := rdb.LLen(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res9) // >>> 0
}
func ExampleClient_lmove_lrange() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res10, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res10) // >>> 1
res11, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res11) // >>> 2
res12, err := rdb.LMove(ctx, "bikes:repairs", "bikes:finished", "LEFT", "LEFT").Result()
if err != nil {
panic(err)
}
fmt.Println(res12) // >>> bike:2
res13, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res13) // >>> [bike:1]
res14, err := rdb.LRange(ctx, "bikes:finished", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res14) // >>> [bike:2]
}
func ExampleClient_lpush_rpush() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res15, err := rdb.RPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res15) // >>> 1
res16, err := rdb.RPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res16) // >>> 2
res17, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike").Result()
if err != nil {
panic(err)
}
fmt.Println(res17) // >>> 3
res18, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res18) // >>> [bike:important_bike bike:1 bike:2]
}
func ExampleClient_variadic() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res19, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res19) // >>> 3
res20, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike", "bike:very_important_bike").Result()
if err != nil {
panic(err)
}
fmt.Println(res20) // >>> 5
res21, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res21) // >>> [bike:very_important_bike bike:important_bike bike:1 bike:2 bike:3]
}
func ExampleClient_lpop_rpop() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res22, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res22) // >>> 3
res23, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res23) // >>> bike:3
res24, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res24) // >>> bike:1
res25, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res25) // >>> bike:2
res26, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
fmt.Println(err) // >>> redis: nil
}
fmt.Println(res26) // >>> <empty string>
}
func ExampleClient_ltrim() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res27, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
fmt.Println(res27) // >>> 5
res28, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result()
if err != nil {
panic(err)
}
fmt.Println(res28) // >>> OK
res29, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res29) // >>> [bike:5 bike:4 bike:3]
}
func ExampleClient_ltrim_end_of_list() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res30, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
fmt.Println(res30) // >>> 5
res31, err := rdb.LTrim(ctx, "bikes:repairs", -3, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res31) // >>> OK
res32, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res32) // >>> [bike:3 bike:4 bike:5]
}
func ExampleClient_brpop() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res33, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res33) // >>> 2
res34, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res34) // >>> [bikes:repairs bike:2]
res35, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res35) // >>> [bikes:repairs bike:1]
res36, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
if err != nil {
fmt.Println(err) // >>> redis: nil
}
fmt.Println(res36) // >>> []
}
func ExampleClient_rule1() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res37, err := rdb.Del(ctx, "new_bikes").Result()
if err != nil {
panic(err)
}
fmt.Println(res37) // >>> 0
res38, err := rdb.LPush(ctx, "new_bikes", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res38) // >>> 3
}
func ExampleClient_rule11() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res39, err := rdb.Set(ctx, "new_bikes", "bike:1", 0).Result()
if err != nil {
panic(err)
}
fmt.Println(res39) // >>> OK
res40, err := rdb.Type(ctx, "new_bikes").Result()
if err != nil {
panic(err)
}
fmt.Println(res40) // >>> string
res41, err := rdb.LPush(ctx, "new_bikes", "bike:2", "bike:3").Result()
if err != nil {
fmt.Println(err)
// >>> WRONGTYPE Operation against a key holding the wrong kind of value
}
fmt.Println(res41)
}
func ExampleClient_rule2() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res42, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res42) // >>> 3
res43, err := rdb.Exists(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res43) // >>> 1
res44, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res44) // >>> bike:3
res45, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res45) // >>> bike:2
res46, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res46) // >>> bike:1
res47, err := rdb.Exists(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res47) // >>> 0
}
func ExampleClient_rule3() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res48, err := rdb.Del(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res48) // >>> 0
res49, err := rdb.LLen(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res49) // >>> 0
res50, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
fmt.Println(err) // >>> redis: nil
}
fmt.Println(res50) // >>> <empty string>
}
func ExampleClient_ltrim1() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res51, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
fmt.Println(res51) // >>> 5
res52, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result()
if err != nil {
panic(err)
}
fmt.Println(res52) // >>> OK
res53, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res53) // >>> [bike:5 bike:4 bike:3]
}
public class ListExample
{
public void run()
{
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
long res1 = db.ListLeftPush("bikes:repairs", "bike:1");
Console.WriteLine(res1); // >>> 1
long res2 = db.ListLeftPush("bikes:repairs", "bike:2");
Console.WriteLine(res2); // >>> 2
RedisValue res3 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res3); // >>> "bike:1"
RedisValue res4 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res4); // >>> "bike:2"
long res5 = db.ListLeftPush("bikes:repairs", "bike:1");
Console.WriteLine(res5); // >>> 1
long res6 = db.ListLeftPush("bikes:repairs", "bike:2");
Console.WriteLine(res6); // >>> 2
RedisValue res7 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res7); // >>> "bike:2"
RedisValue res8 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res8); // >>> "bike:1"
long res9 = db.ListLength("bikes:repairs");
Console.WriteLine(res9); // >>> 0
long res10 = db.ListLeftPush("{bikes}:repairs", "bike:1");
Console.WriteLine(res10); // >>> 1
long res11 = db.ListLeftPush("{bikes}:repairs", "bike:2");
Console.WriteLine(res11); // >>> 2
RedisValue res12 = db.ListMove("{bikes}:repairs", "{bikes}:finished", ListSide.Left, ListSide.Left);
Console.Write(res12); // >>> "bike:2"
RedisValue[] res13 = db.ListRange("{bikes}:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res13)); // >>> "bike:1"
RedisValue[] res14 = db.ListRange("{bikes}:finished", 0, -1);
Console.WriteLine(string.Join(", ", res14)); // >>> "bike:2"
long res15 = db.ListRightPush("bikes:repairs", "bike:1");
Console.WriteLine(res15); // >>> 1
long res16 = db.ListRightPush("bikes:repairs", "bike:2");
Console.WriteLine(res16); // >>> 2
long res17 = db.ListLeftPush("bikes:repairs", "bike:important_bike");
Console.WriteLine(res17); // >>> 3
RedisValue[] res18 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res18)); // >>> "bike:important_bike, bike:1, bike:2"
long res19 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res19); // >>> 3
long res20 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:important_bike", "bike:very_important_bike" });
Console.WriteLine(res20); // >>> 5
RedisValue[] res21 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res21));
// >>> "bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3"
long res22 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res22); // >>> 3
RedisValue res23 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res23); // >>> "bike:3"
RedisValue res24 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res24); // >>> "bike:1"
RedisValue res25 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res25); // >>> "bike:2"
RedisValue res26 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res26); // >>> <Empty string>
long res27 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
Console.WriteLine(res27); // >>> 5
db.ListTrim("bikes:repairs", 0, 2);
RedisValue[] res28 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res28)); // "bike:5, bike:4, bike:3"
long res29 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
Console.WriteLine(res29); // >>> 5
db.ListTrim("bikes:repairs", -3, -1);
RedisValue[] res30 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res30)); // >>> "bike:3, bike:4, bike:5"
long res31 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2" });
Console.WriteLine(res31); // >>> 2
Tuple<RedisKey, RedisValue>? res32 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1);
if (res32 != null)
Console.WriteLine($"{res32.Item1} -> {res32.Item2}"); // >>> "bikes:repairs -> bike:2"
Tuple<RedisKey, RedisValue>? res33 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1);
if (res33 != null)
Console.WriteLine($"{res33.Item1} -> {res33.Item2}"); // >>> "bikes:repairs -> bike:1"
Tuple<RedisKey, RedisValue>? res34 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1);
Console.WriteLine(res34); // >>> "Null"
bool res35 = db.KeyDelete("new_bikes");
Console.WriteLine(res35); // >>> False
long res36 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res36); // >>> 3
bool res37 = db.StringSet("new_bikes", "bike:1");
Console.WriteLine(res37); // >>> True
RedisType res38 = db.KeyType("new_bikes");
Console.WriteLine(res38); // >>> RedisType.String
try
{
long res39 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:2", "bike:3" });
}
catch (Exception e)
{
Console.WriteLine(e);
}
long res40 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res40); // >>> 3
bool res41 = db.KeyExists("bikes:repairs");
Console.WriteLine(res41); // >>> True
RedisValue res42 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res42); // >>> "bike:3"
RedisValue res43 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res43); // >>> "bike:2"
RedisValue res44 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res44); // >>> "bike:1"
bool res45 = db.KeyExists("bikes:repairs");
Console.WriteLine(res45); // >>> False
bool res46 = db.KeyDelete("bikes:repairs");
Console.WriteLine(res46); // >>> False
long res47 = db.ListLength("bikes:repairs");
Console.WriteLine(res47); // >>> 0
RedisValue res48 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res48); // >>> Null
long res49 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
Console.WriteLine(res49); // >>> 5
db.ListTrim("bikes:repairs", 0, 2);
RedisValue[] res50 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res50)); // >>> "bike:5, bike:4, bike:3"
}
}
The above LTRIM
command tells Redis to keep just list elements from index
0 to 2, everything else will be discarded. This allows for a very simple but
useful pattern: doing a List push operation + a List trim operation together
to add a new element and discard elements exceeding a limit. Using
LTRIM
with negative indexes can then be used to keep only the 3 most recently added:
> RPUSH bikes:repairs bike:1 bike:2 bike:3 bike:4 bike:5
(integer) 5
> LTRIM bikes:repairs -3 -1
OK
> LRANGE bikes:repairs 0 -1
1) "bike:3"
2) "bike:4"
3) "bike:5"
"""
Code samples for List doc pages:
https://redis.io/docs/latest/develop/data-types/lists/
"""
import redis
r = redis.Redis(decode_responses=True)
res1 = r.lpush("bikes:repairs", "bike:1")
print(res1) # >>> 1
res2 = r.lpush("bikes:repairs", "bike:2")
print(res2) # >>> 2
res3 = r.rpop("bikes:repairs")
print(res3) # >>> bike:1
res4 = r.rpop("bikes:repairs")
print(res4) # >>> bike:2
res5 = r.lpush("bikes:repairs", "bike:1")
print(res5) # >>> 1
res6 = r.lpush("bikes:repairs", "bike:2")
print(res6) # >>> 2
res7 = r.lpop("bikes:repairs")
print(res7) # >>> bike:2
res8 = r.lpop("bikes:repairs")
print(res8) # >>> bike:1
res9 = r.llen("bikes:repairs")
print(res9) # >>> 0
res10 = r.lpush("bikes:repairs", "bike:1")
print(res10) # >>> 1
res11 = r.lpush("bikes:repairs", "bike:2")
print(res11) # >>> 2
res12 = r.lmove("bikes:repairs", "bikes:finished", "LEFT", "LEFT")
print(res12) # >>> 'bike:2'
res13 = r.lrange("bikes:repairs", 0, -1)
print(res13) # >>> ['bike:1']
res14 = r.lrange("bikes:finished", 0, -1)
print(res14) # >>> ['bike:2']
res15 = r.rpush("bikes:repairs", "bike:1")
print(res15) # >>> 1
res16 = r.rpush("bikes:repairs", "bike:2")
print(res16) # >>> 2
res17 = r.lpush("bikes:repairs", "bike:important_bike")
print(res17) # >>> 3
res18 = r.lrange("bikes:repairs", 0, -1)
print(res18) # >>> ['bike:important_bike', 'bike:1', 'bike:2']
res19 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res19) # >>> 3
res20 = r.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike")
print(res20) # >>> 5
res21 = r.lrange("bikes:repairs", 0, -1)
print(
res21
) # >>> ['bike:very_important_bike', 'bike:important_bike', 'bike:1', ...
res22 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res22) # >>> 3
res23 = r.rpop("bikes:repairs")
print(res23) # >>> 'bike:3'
res24 = r.lpop("bikes:repairs")
print(res24) # >>> 'bike:1'
res25 = r.rpop("bikes:repairs")
print(res25) # >>> 'bike:2'
res26 = r.rpop("bikes:repairs")
print(res26) # >>> None
res27 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res27) # >>> 5
res28 = r.ltrim("bikes:repairs", 0, 2)
print(res28) # >>> True
res29 = r.lrange("bikes:repairs", 0, -1)
print(res29) # >>> ['bike:5', 'bike:4', 'bike:3']
res27 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res27) # >>> 5
res28 = r.ltrim("bikes:repairs", -3, -1)
print(res28) # >>> True
res29 = r.lrange("bikes:repairs", 0, -1)
print(res29) # >>> ['bike:3', 'bike:4', 'bike:5']
res31 = r.rpush("bikes:repairs", "bike:1", "bike:2")
print(res31) # >>> 2
res32 = r.brpop("bikes:repairs", timeout=1)
print(res32) # >>> ('bikes:repairs', 'bike:2')
res33 = r.brpop("bikes:repairs", timeout=1)
print(res33) # >>> ('bikes:repairs', 'bike:1')
res34 = r.brpop("bikes:repairs", timeout=1)
print(res34) # >>> None
res35 = r.delete("new_bikes")
print(res35) # >>> 0
res36 = r.lpush("new_bikes", "bike:1", "bike:2", "bike:3")
print(res36) # >>> 3
res37 = r.set("new_bikes", "bike:1")
print(res37) # >>> True
res38 = r.type("new_bikes")
print(res38) # >>> 'string'
try:
res39 = r.lpush("new_bikes", "bike:2", "bike:3")
# >>> redis.exceptions.ResponseError:
# >>> WRONGTYPE Operation against a key holding the wrong kind of value
except redis.exceptions.ResponseError as e:
print(e)
r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res36) # >>> 3
res40 = r.exists("bikes:repairs")
print(res40) # >>> 1
res41 = r.lpop("bikes:repairs")
print(res41) # >>> 'bike:3'
res42 = r.lpop("bikes:repairs")
print(res42) # >>> 'bike:2'
res43 = r.lpop("bikes:repairs")
print(res43) # >>> 'bike:1'
res44 = r.exists("bikes:repairs")
print(res44) # >>> False
res45 = r.delete("bikes:repairs")
print(res45) # >>> 0
res46 = r.llen("bikes:repairs")
print(res46) # >>> 0
res47 = r.lpop("bikes:repairs")
print(res47) # >>> None
res48 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res48) # >>> 5
res49 = r.ltrim("bikes:repairs", 0, 2)
print(res49) # >>> True
res50 = r.lrange("bikes:repairs", 0, -1)
print(res50) # >>> ['bike:5', 'bike:4', 'bike:3']
import assert from 'assert';
import { createClient } from 'redis';
const client = await createClient();
await client.connect();
const res1 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res1); // 1
const res2 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res2); // 2
const res3 = await client.rPop('bikes:repairs');
console.log(res3); // bike:1
const res4 = await client.rPop('bikes:repairs');
console.log(res4); // bike:2
const res5 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res5); // 1
const res6 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res6); // 2
const res7 = await client.lPop('bikes:repairs');
console.log(res7); // bike:2
const res8 = await client.lPop('bikes:repairs');
console.log(res8); // bike:1
const res9 = await client.lLen('bikes:repairs');
console.log(res9); // 0
const res10 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res10); // 1
const res11 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res11); // 2
const res12 = await client.lMove('bikes:repairs', 'bikes:finished', 'LEFT', 'LEFT');
console.log(res12); // 'bike:2'
const res13 = await client.lRange('bikes:repairs', 0, -1);
console.log(res13); // ['bike:1']
const res14 = await client.lRange('bikes:finished', 0, -1);
console.log(res14); // ['bike:2']
const res15 = await client.rPush('bikes:repairs', 'bike:1');
console.log(res15); // 1
const res16 = await client.rPush('bikes:repairs', 'bike:2');
console.log(res16); // 2
const res17 = await client.lPush('bikes:repairs', 'bike:important_bike');
console.log(res17); // 3
const res18 = await client.lRange('bikes:repairs', 0, -1);
console.log(res18); // ['bike:important_bike', 'bike:1', 'bike:2']
const res19 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res19); // 3
const res20 = await client.lPush(
'bikes:repairs', ['bike:important_bike', 'bike:very_important_bike']
);
console.log(res20); // 5
const res21 = await client.lRange('bikes:repairs', 0, -1);
console.log(res21); // ['bike:very_important_bike', 'bike:important_bike', 'bike:1', 'bike:2', 'bike:3']
const res22 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res22); // 3
const res23 = await client.rPop('bikes:repairs');
console.log(res23); // 'bike:3'
const res24 = await client.lPop('bikes:repairs');
console.log(res24); // 'bike:1'
const res25 = await client.rPop('bikes:repairs');
console.log(res25); // 'bike:2'
const res26 = await client.rPop('bikes:repairs');
console.log(res26); // None
const res27 = await client.lPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res27); // 5
const res28 = await client.lTrim('bikes:repairs', 0, 2);
console.log(res28); // true
const res29 = await client.lRange('bikes:repairs', 0, -1);
console.log(res29); // ['bike:5', 'bike:4', 'bike:3']
const res27eol = await client.rPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res27eol); // 5
const res28eol = await client.lTrim('bikes:repairs', -3, -1);
console.log(res28eol); // 'OK'
const res29eol = await client.lRange('bikes:repairs', 0, -1);
console.log(res29eol); // ['bike:3', 'bike:4', 'bike:5']
const res31 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2']);
console.log(res31); // 2
const res32 = await client.brPop('bikes:repairs', 1);
console.log(res32); // { key: 'bikes:repairs', element: 'bike:2' }
const res33 = await client.brPop('bikes:repairs', 1);
console.log(res33); // { key: 'bikes:repairs', element: 'bike:1' }
const res34 = await client.brPop('bikes:repairs', 1);
console.log(res34); // null
const res35 = await client.del('new_bikes');
console.log(res35); // 0
const res36 = await client.lPush('new_bikes', ['bike:1', 'bike:2', 'bike:3']);
console.log(res36); // 3
const res37 = await client.set('new_bikes', 'bike:1');
console.log(res37); // 'OK'
const res38 = await client.type('new_bikes');
console.log(res38); // 'string'
try {
const res39 = await client.lPush('new_bikes', 'bike:2', 'bike:3');
// redis.exceptions.ResponseError:
// [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value]
}
catch(e){
console.log(e);
}
await client.lPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res36); // 3
const res40 = await client.exists('bikes:repairs')
console.log(res40); // true
const res41 = await client.lPop('bikes:repairs');
console.log(res41); // 'bike:3'
const res42 = await client.lPop('bikes:repairs');
console.log(res42); // 'bike:2'
const res43 = await client.lPop('bikes:repairs');
console.log(res43); // 'bike:1'
const res44 = await client.exists('bikes:repairs');
console.log(res44); // 0
const res45 = await client.del('bikes:repairs');
console.log(res45); // 0
const res46 = await client.lLen('bikes:repairs');
console.log(res46); // 0
const res47 = await client.lPop('bikes:repairs');
console.log(res47); // null
const res48 = await client.lPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res48); // 5
const res49 = await client.lTrim('bikes:repairs', 0, 2);
console.log(res49); // 'OK'
const res50 = await client.lRange('bikes:repairs', 0, -1);
console.log(res50); // ['bike:5', 'bike:4', 'bike:3']
package io.redis.examples;
import org.junit.Test;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.args.ListDirection;
import java.util.List;
import static org.junit.Assert.*;
public class ListExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
long res1 = jedis.lpush("bikes:repairs", "bike:1");
System.out.println(res1); // >>> 1
long res2 = jedis.lpush("bikes:repairs", "bike:2");
System.out.println(res2); // >>> 2
String res3 = jedis.rpop("bikes:repairs");
System.out.println(res3); // >>> bike:1
String res4 = jedis.rpop("bikes:repairs");
System.out.println(res4); // >>> bike:2
long res5 = jedis.lpush("bikes:repairs", "bike:1");
System.out.println(res5); // >>> 1
long res6 = jedis.lpush("bikes:repairs", "bike:2");
System.out.println(res6); // >>> 2
String res7 = jedis.lpop("bikes:repairs");
System.out.println(res7); // >>> bike:2
String res8 = jedis.lpop("bikes:repairs");
System.out.println(res8); // >>> bike:1
long res9 = jedis.llen("bikes:repairs");
System.out.println(res9); // >>> 0
long res10 = jedis.lpush("bikes:repairs", "bike:1");
System.out.println(res10); // >>> 1
long res11 = jedis.lpush("bikes:repairs", "bike:2");
System.out.println(res11); // >>> 2
String res12 = jedis.lmove("bikes:repairs", "bikes:finished", ListDirection.LEFT, ListDirection.LEFT);
System.out.println(res12); // >>> bike:2
List<String> res13 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res13); // >>> [bike:1]
List<String> res14 = jedis.lrange("bikes:finished", 0, -1);
System.out.println(res14); // >>> [bike:2]
long res15 = jedis.rpush("bikes:repairs", "bike:1");
System.out.println(res15); // >>> 1
long res16 = jedis.rpush("bikes:repairs", "bike:2");
System.out.println(res16); // >>> 2
long res17 = jedis.lpush("bikes:repairs", "bike:important_bike");
System.out.println(res17); // >>> 3
List<String> res18 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res18); // >>> [bike:important_bike, bike:1, bike:2]
long res19 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3");
System.out.println(res19); // >>> 3
long res20 = jedis.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike");
System.out.println(res20); // >>> 5
List<String> res21 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res21); // >>> [bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3]
long res22 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3");
System.out.println(res22); // >>> 3
String res23 = jedis.rpop("bikes:repairs");
System.out.println(res23); // >>> bike:3
String res24 = jedis.lpop("bikes:repairs");
System.out.println(res24); // >>> bike:1
String res25 = jedis.rpop("bikes:repairs");
System.out.println(res25); // >>> bike:2
String res26 = jedis.rpop("bikes:repairs");
System.out.println(res26); // >>> null
long res27 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
System.out.println(res27); // >>> 5
String res28 = jedis.ltrim("bikes:repairs", 0, 2);
System.out.println(res28); // >>> OK
List<String> res29 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res29); // >>> [bike:5, bike:4, bike:3]
res27 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
System.out.println(res27); // >>> 5
res28 = jedis.ltrim("bikes:repairs", -3, -1);
System.out.println(res2); // >>> OK
res29 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res29); // >>> [bike:3, bike:4, bike:5]
long res31 = jedis.rpush("bikes:repairs", "bike:1", "bike:2");
System.out.println(res31); // >>> 2
List<String> res32 = jedis.brpop(1, "bikes:repairs");
System.out.println(res32); // >>> (bikes:repairs, bike:2)
List<String> res33 = jedis.brpop(1,"bikes:repairs");
System.out.println(res33); // >>> (bikes:repairs, bike:1)
List<String> res34 = jedis.brpop(1,"bikes:repairs");
System.out.println(res34); // >>> null
long res35 = jedis.del("new_bikes");
System.out.println(res35); // >>> 0
long res36 = jedis.lpush("new_bikes", "bike:1", "bike:2", "bike:3");
System.out.println(res36); // >>> 3
String res37 = jedis.set("new_bikes", "bike:1");
System.out.println(res37); // >>> OK
String res38 = jedis.type("new_bikes");
System.out.println(res38); // >>> string
try {
long res39 = jedis.lpush("new_bikes", "bike:2", "bike:3");
} catch (Exception e) {
e.printStackTrace();
// >>> redis.clients.jedis.exceptions.JedisDataException:
// >>> WRONGTYPE Operation against a key holding the wrong kind of value
}
jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3");
System.out.println(res36); // >>> 3
boolean res40 = jedis.exists("bikes:repairs");
System.out.println(res40); // >>> true
String res41 = jedis.lpop("bikes:repairs");
System.out.println(res41); // >>> bike:3
String res42 = jedis.lpop("bikes:repairs");
System.out.println(res42); // >>> bike:2
String res43 = jedis.lpop("bikes:repairs");
System.out.println(res43); // >>> bike:1
boolean res44 = jedis.exists("bikes:repairs");
System.out.println(res44); // >>> false
long res45 = jedis.del("bikes:repairs");
System.out.println(res45); // >>> 0
long res46 = jedis.llen("bikes:repairs");
System.out.println(res46); // >>> 0
String res47 = jedis.lpop("bikes:repairs");
System.out.println(res47); // >>> null
long res48 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
System.out.println(res48); // >>> 5
String res49 = jedis.ltrim("bikes:repairs", 0, 2);
System.out.println(res49); // >>> OK
List<String> res50 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res50); // >>> [bike:5, bike:4, bike:3]
}
}
package example_commands_test
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
func ExampleClient_queue() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res1, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res1) // >>> 1
res2, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res2) // >>> 2
res3, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res3) // >>> bike:1
res4, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res4) // >>> bike:2
}
func ExampleClient_stack() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res5, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res5) // >>> 1
res6, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res6) // >>> 2
res7, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res7) // >>> bike:2
res8, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res8) // >>> bike:1
}
func ExampleClient_llen() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res9, err := rdb.LLen(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res9) // >>> 0
}
func ExampleClient_lmove_lrange() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res10, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res10) // >>> 1
res11, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res11) // >>> 2
res12, err := rdb.LMove(ctx, "bikes:repairs", "bikes:finished", "LEFT", "LEFT").Result()
if err != nil {
panic(err)
}
fmt.Println(res12) // >>> bike:2
res13, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res13) // >>> [bike:1]
res14, err := rdb.LRange(ctx, "bikes:finished", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res14) // >>> [bike:2]
}
func ExampleClient_lpush_rpush() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res15, err := rdb.RPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res15) // >>> 1
res16, err := rdb.RPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res16) // >>> 2
res17, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike").Result()
if err != nil {
panic(err)
}
fmt.Println(res17) // >>> 3
res18, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res18) // >>> [bike:important_bike bike:1 bike:2]
}
func ExampleClient_variadic() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res19, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res19) // >>> 3
res20, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike", "bike:very_important_bike").Result()
if err != nil {
panic(err)
}
fmt.Println(res20) // >>> 5
res21, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res21) // >>> [bike:very_important_bike bike:important_bike bike:1 bike:2 bike:3]
}
func ExampleClient_lpop_rpop() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res22, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res22) // >>> 3
res23, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res23) // >>> bike:3
res24, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res24) // >>> bike:1
res25, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res25) // >>> bike:2
res26, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
fmt.Println(err) // >>> redis: nil
}
fmt.Println(res26) // >>> <empty string>
}
func ExampleClient_ltrim() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res27, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
fmt.Println(res27) // >>> 5
res28, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result()
if err != nil {
panic(err)
}
fmt.Println(res28) // >>> OK
res29, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res29) // >>> [bike:5 bike:4 bike:3]
}
func ExampleClient_ltrim_end_of_list() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res30, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
fmt.Println(res30) // >>> 5
res31, err := rdb.LTrim(ctx, "bikes:repairs", -3, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res31) // >>> OK
res32, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res32) // >>> [bike:3 bike:4 bike:5]
}
func ExampleClient_brpop() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res33, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res33) // >>> 2
res34, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res34) // >>> [bikes:repairs bike:2]
res35, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res35) // >>> [bikes:repairs bike:1]
res36, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
if err != nil {
fmt.Println(err) // >>> redis: nil
}
fmt.Println(res36) // >>> []
}
func ExampleClient_rule1() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res37, err := rdb.Del(ctx, "new_bikes").Result()
if err != nil {
panic(err)
}
fmt.Println(res37) // >>> 0
res38, err := rdb.LPush(ctx, "new_bikes", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res38) // >>> 3
}
func ExampleClient_rule11() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res39, err := rdb.Set(ctx, "new_bikes", "bike:1", 0).Result()
if err != nil {
panic(err)
}
fmt.Println(res39) // >>> OK
res40, err := rdb.Type(ctx, "new_bikes").Result()
if err != nil {
panic(err)
}
fmt.Println(res40) // >>> string
res41, err := rdb.LPush(ctx, "new_bikes", "bike:2", "bike:3").Result()
if err != nil {
fmt.Println(err)
// >>> WRONGTYPE Operation against a key holding the wrong kind of value
}
fmt.Println(res41)
}
func ExampleClient_rule2() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res42, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res42) // >>> 3
res43, err := rdb.Exists(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res43) // >>> 1
res44, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res44) // >>> bike:3
res45, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res45) // >>> bike:2
res46, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res46) // >>> bike:1
res47, err := rdb.Exists(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res47) // >>> 0
}
func ExampleClient_rule3() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res48, err := rdb.Del(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res48) // >>> 0
res49, err := rdb.LLen(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res49) // >>> 0
res50, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
fmt.Println(err) // >>> redis: nil
}
fmt.Println(res50) // >>> <empty string>
}
func ExampleClient_ltrim1() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res51, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
fmt.Println(res51) // >>> 5
res52, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result()
if err != nil {
panic(err)
}
fmt.Println(res52) // >>> OK
res53, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res53) // >>> [bike:5 bike:4 bike:3]
}
public class ListExample
{
public void run()
{
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
long res1 = db.ListLeftPush("bikes:repairs", "bike:1");
Console.WriteLine(res1); // >>> 1
long res2 = db.ListLeftPush("bikes:repairs", "bike:2");
Console.WriteLine(res2); // >>> 2
RedisValue res3 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res3); // >>> "bike:1"
RedisValue res4 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res4); // >>> "bike:2"
long res5 = db.ListLeftPush("bikes:repairs", "bike:1");
Console.WriteLine(res5); // >>> 1
long res6 = db.ListLeftPush("bikes:repairs", "bike:2");
Console.WriteLine(res6); // >>> 2
RedisValue res7 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res7); // >>> "bike:2"
RedisValue res8 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res8); // >>> "bike:1"
long res9 = db.ListLength("bikes:repairs");
Console.WriteLine(res9); // >>> 0
long res10 = db.ListLeftPush("{bikes}:repairs", "bike:1");
Console.WriteLine(res10); // >>> 1
long res11 = db.ListLeftPush("{bikes}:repairs", "bike:2");
Console.WriteLine(res11); // >>> 2
RedisValue res12 = db.ListMove("{bikes}:repairs", "{bikes}:finished", ListSide.Left, ListSide.Left);
Console.Write(res12); // >>> "bike:2"
RedisValue[] res13 = db.ListRange("{bikes}:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res13)); // >>> "bike:1"
RedisValue[] res14 = db.ListRange("{bikes}:finished", 0, -1);
Console.WriteLine(string.Join(", ", res14)); // >>> "bike:2"
long res15 = db.ListRightPush("bikes:repairs", "bike:1");
Console.WriteLine(res15); // >>> 1
long res16 = db.ListRightPush("bikes:repairs", "bike:2");
Console.WriteLine(res16); // >>> 2
long res17 = db.ListLeftPush("bikes:repairs", "bike:important_bike");
Console.WriteLine(res17); // >>> 3
RedisValue[] res18 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res18)); // >>> "bike:important_bike, bike:1, bike:2"
long res19 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res19); // >>> 3
long res20 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:important_bike", "bike:very_important_bike" });
Console.WriteLine(res20); // >>> 5
RedisValue[] res21 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res21));
// >>> "bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3"
long res22 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res22); // >>> 3
RedisValue res23 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res23); // >>> "bike:3"
RedisValue res24 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res24); // >>> "bike:1"
RedisValue res25 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res25); // >>> "bike:2"
RedisValue res26 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res26); // >>> <Empty string>
long res27 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
Console.WriteLine(res27); // >>> 5
db.ListTrim("bikes:repairs", 0, 2);
RedisValue[] res28 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res28)); // "bike:5, bike:4, bike:3"
long res29 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
Console.WriteLine(res29); // >>> 5
db.ListTrim("bikes:repairs", -3, -1);
RedisValue[] res30 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res30)); // >>> "bike:3, bike:4, bike:5"
long res31 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2" });
Console.WriteLine(res31); // >>> 2
Tuple<RedisKey, RedisValue>? res32 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1);
if (res32 != null)
Console.WriteLine($"{res32.Item1} -> {res32.Item2}"); // >>> "bikes:repairs -> bike:2"
Tuple<RedisKey, RedisValue>? res33 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1);
if (res33 != null)
Console.WriteLine($"{res33.Item1} -> {res33.Item2}"); // >>> "bikes:repairs -> bike:1"
Tuple<RedisKey, RedisValue>? res34 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1);
Console.WriteLine(res34); // >>> "Null"
bool res35 = db.KeyDelete("new_bikes");
Console.WriteLine(res35); // >>> False
long res36 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res36); // >>> 3
bool res37 = db.StringSet("new_bikes", "bike:1");
Console.WriteLine(res37); // >>> True
RedisType res38 = db.KeyType("new_bikes");
Console.WriteLine(res38); // >>> RedisType.String
try
{
long res39 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:2", "bike:3" });
}
catch (Exception e)
{
Console.WriteLine(e);
}
long res40 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res40); // >>> 3
bool res41 = db.KeyExists("bikes:repairs");
Console.WriteLine(res41); // >>> True
RedisValue res42 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res42); // >>> "bike:3"
RedisValue res43 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res43); // >>> "bike:2"
RedisValue res44 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res44); // >>> "bike:1"
bool res45 = db.KeyExists("bikes:repairs");
Console.WriteLine(res45); // >>> False
bool res46 = db.KeyDelete("bikes:repairs");
Console.WriteLine(res46); // >>> False
long res47 = db.ListLength("bikes:repairs");
Console.WriteLine(res47); // >>> 0
RedisValue res48 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res48); // >>> Null
long res49 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
Console.WriteLine(res49); // >>> 5
db.ListTrim("bikes:repairs", 0, 2);
RedisValue[] res50 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res50)); // >>> "bike:5, bike:4, bike:3"
}
}
The above combination adds new elements and keeps only the 3
newest elements into the list. With LRANGE
you can access the top items
without any need to remember very old data.
Note: while LRANGE
is technically an O(N) command, accessing small ranges
towards the head or the tail of the list is a constant time operation.
Blocking operations on lists
Lists have a special feature that make them suitable to implement queues, and in general as a building block for inter process communication systems: blocking operations.
Imagine you want to push items into a list with one process, and use a different process in order to actually do some kind of work with those items. This is the usual producer / consumer setup, and can be implemented in the following simple way:
- To push items into the list, producers call
LPUSH
. - To extract / process items from the list, consumers call
RPOP
.
However it is possible that sometimes the list is empty and there is nothing
to process, so RPOP
just returns NULL. In this case a consumer is forced to wait
some time and retry again with RPOP
. This is called polling, and is not
a good idea in this context because it has several drawbacks:
- Forces Redis and clients to process useless commands (all the requests when the list is empty will get no actual work done, they'll just return NULL).
- Adds a delay to the processing of items, since after a worker receives a NULL, it waits some time. To make the delay smaller, we could wait less between calls to
RPOP
, with the effect of amplifying problem number 1, i.e. more useless calls to Redis.
So Redis implements commands called BRPOP
and BLPOP
which are versions
of RPOP
and LPOP
able to block if the list is empty: they'll return to
the caller only when a new element is added to the list, or when a user-specified
timeout is reached.
This is an example of a BRPOP
call we could use in the worker:
> RPUSH bikes:repairs bike:1 bike:2
(integer) 2
> BRPOP bikes:repairs 1
1) "bikes:repairs"
2) "bike:2"
> BRPOP bikes:repairs 1
1) "bikes:repairs"
2) "bike:1"
> BRPOP bikes:repairs 1
(nil)
(2.01s)
"""
Code samples for List doc pages:
https://redis.io/docs/latest/develop/data-types/lists/
"""
import redis
r = redis.Redis(decode_responses=True)
res1 = r.lpush("bikes:repairs", "bike:1")
print(res1) # >>> 1
res2 = r.lpush("bikes:repairs", "bike:2")
print(res2) # >>> 2
res3 = r.rpop("bikes:repairs")
print(res3) # >>> bike:1
res4 = r.rpop("bikes:repairs")
print(res4) # >>> bike:2
res5 = r.lpush("bikes:repairs", "bike:1")
print(res5) # >>> 1
res6 = r.lpush("bikes:repairs", "bike:2")
print(res6) # >>> 2
res7 = r.lpop("bikes:repairs")
print(res7) # >>> bike:2
res8 = r.lpop("bikes:repairs")
print(res8) # >>> bike:1
res9 = r.llen("bikes:repairs")
print(res9) # >>> 0
res10 = r.lpush("bikes:repairs", "bike:1")
print(res10) # >>> 1
res11 = r.lpush("bikes:repairs", "bike:2")
print(res11) # >>> 2
res12 = r.lmove("bikes:repairs", "bikes:finished", "LEFT", "LEFT")
print(res12) # >>> 'bike:2'
res13 = r.lrange("bikes:repairs", 0, -1)
print(res13) # >>> ['bike:1']
res14 = r.lrange("bikes:finished", 0, -1)
print(res14) # >>> ['bike:2']
res15 = r.rpush("bikes:repairs", "bike:1")
print(res15) # >>> 1
res16 = r.rpush("bikes:repairs", "bike:2")
print(res16) # >>> 2
res17 = r.lpush("bikes:repairs", "bike:important_bike")
print(res17) # >>> 3
res18 = r.lrange("bikes:repairs", 0, -1)
print(res18) # >>> ['bike:important_bike', 'bike:1', 'bike:2']
res19 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res19) # >>> 3
res20 = r.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike")
print(res20) # >>> 5
res21 = r.lrange("bikes:repairs", 0, -1)
print(
res21
) # >>> ['bike:very_important_bike', 'bike:important_bike', 'bike:1', ...
res22 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res22) # >>> 3
res23 = r.rpop("bikes:repairs")
print(res23) # >>> 'bike:3'
res24 = r.lpop("bikes:repairs")
print(res24) # >>> 'bike:1'
res25 = r.rpop("bikes:repairs")
print(res25) # >>> 'bike:2'
res26 = r.rpop("bikes:repairs")
print(res26) # >>> None
res27 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res27) # >>> 5
res28 = r.ltrim("bikes:repairs", 0, 2)
print(res28) # >>> True
res29 = r.lrange("bikes:repairs", 0, -1)
print(res29) # >>> ['bike:5', 'bike:4', 'bike:3']
res27 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res27) # >>> 5
res28 = r.ltrim("bikes:repairs", -3, -1)
print(res28) # >>> True
res29 = r.lrange("bikes:repairs", 0, -1)
print(res29) # >>> ['bike:3', 'bike:4', 'bike:5']
res31 = r.rpush("bikes:repairs", "bike:1", "bike:2")
print(res31) # >>> 2
res32 = r.brpop("bikes:repairs", timeout=1)
print(res32) # >>> ('bikes:repairs', 'bike:2')
res33 = r.brpop("bikes:repairs", timeout=1)
print(res33) # >>> ('bikes:repairs', 'bike:1')
res34 = r.brpop("bikes:repairs", timeout=1)
print(res34) # >>> None
res35 = r.delete("new_bikes")
print(res35) # >>> 0
res36 = r.lpush("new_bikes", "bike:1", "bike:2", "bike:3")
print(res36) # >>> 3
res37 = r.set("new_bikes", "bike:1")
print(res37) # >>> True
res38 = r.type("new_bikes")
print(res38) # >>> 'string'
try:
res39 = r.lpush("new_bikes", "bike:2", "bike:3")
# >>> redis.exceptions.ResponseError:
# >>> WRONGTYPE Operation against a key holding the wrong kind of value
except redis.exceptions.ResponseError as e:
print(e)
r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res36) # >>> 3
res40 = r.exists("bikes:repairs")
print(res40) # >>> 1
res41 = r.lpop("bikes:repairs")
print(res41) # >>> 'bike:3'
res42 = r.lpop("bikes:repairs")
print(res42) # >>> 'bike:2'
res43 = r.lpop("bikes:repairs")
print(res43) # >>> 'bike:1'
res44 = r.exists("bikes:repairs")
print(res44) # >>> False
res45 = r.delete("bikes:repairs")
print(res45) # >>> 0
res46 = r.llen("bikes:repairs")
print(res46) # >>> 0
res47 = r.lpop("bikes:repairs")
print(res47) # >>> None
res48 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res48) # >>> 5
res49 = r.ltrim("bikes:repairs", 0, 2)
print(res49) # >>> True
res50 = r.lrange("bikes:repairs", 0, -1)
print(res50) # >>> ['bike:5', 'bike:4', 'bike:3']
import assert from 'assert';
import { createClient } from 'redis';
const client = await createClient();
await client.connect();
const res1 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res1); // 1
const res2 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res2); // 2
const res3 = await client.rPop('bikes:repairs');
console.log(res3); // bike:1
const res4 = await client.rPop('bikes:repairs');
console.log(res4); // bike:2
const res5 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res5); // 1
const res6 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res6); // 2
const res7 = await client.lPop('bikes:repairs');
console.log(res7); // bike:2
const res8 = await client.lPop('bikes:repairs');
console.log(res8); // bike:1
const res9 = await client.lLen('bikes:repairs');
console.log(res9); // 0
const res10 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res10); // 1
const res11 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res11); // 2
const res12 = await client.lMove('bikes:repairs', 'bikes:finished', 'LEFT', 'LEFT');
console.log(res12); // 'bike:2'
const res13 = await client.lRange('bikes:repairs', 0, -1);
console.log(res13); // ['bike:1']
const res14 = await client.lRange('bikes:finished', 0, -1);
console.log(res14); // ['bike:2']
const res15 = await client.rPush('bikes:repairs', 'bike:1');
console.log(res15); // 1
const res16 = await client.rPush('bikes:repairs', 'bike:2');
console.log(res16); // 2
const res17 = await client.lPush('bikes:repairs', 'bike:important_bike');
console.log(res17); // 3
const res18 = await client.lRange('bikes:repairs', 0, -1);
console.log(res18); // ['bike:important_bike', 'bike:1', 'bike:2']
const res19 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res19); // 3
const res20 = await client.lPush(
'bikes:repairs', ['bike:important_bike', 'bike:very_important_bike']
);
console.log(res20); // 5
const res21 = await client.lRange('bikes:repairs', 0, -1);
console.log(res21); // ['bike:very_important_bike', 'bike:important_bike', 'bike:1', 'bike:2', 'bike:3']
const res22 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res22); // 3
const res23 = await client.rPop('bikes:repairs');
console.log(res23); // 'bike:3'
const res24 = await client.lPop('bikes:repairs');
console.log(res24); // 'bike:1'
const res25 = await client.rPop('bikes:repairs');
console.log(res25); // 'bike:2'
const res26 = await client.rPop('bikes:repairs');
console.log(res26); // None
const res27 = await client.lPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res27); // 5
const res28 = await client.lTrim('bikes:repairs', 0, 2);
console.log(res28); // true
const res29 = await client.lRange('bikes:repairs', 0, -1);
console.log(res29); // ['bike:5', 'bike:4', 'bike:3']
const res27eol = await client.rPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res27eol); // 5
const res28eol = await client.lTrim('bikes:repairs', -3, -1);
console.log(res28eol); // 'OK'
const res29eol = await client.lRange('bikes:repairs', 0, -1);
console.log(res29eol); // ['bike:3', 'bike:4', 'bike:5']
const res31 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2']);
console.log(res31); // 2
const res32 = await client.brPop('bikes:repairs', 1);
console.log(res32); // { key: 'bikes:repairs', element: 'bike:2' }
const res33 = await client.brPop('bikes:repairs', 1);
console.log(res33); // { key: 'bikes:repairs', element: 'bike:1' }
const res34 = await client.brPop('bikes:repairs', 1);
console.log(res34); // null
const res35 = await client.del('new_bikes');
console.log(res35); // 0
const res36 = await client.lPush('new_bikes', ['bike:1', 'bike:2', 'bike:3']);
console.log(res36); // 3
const res37 = await client.set('new_bikes', 'bike:1');
console.log(res37); // 'OK'
const res38 = await client.type('new_bikes');
console.log(res38); // 'string'
try {
const res39 = await client.lPush('new_bikes', 'bike:2', 'bike:3');
// redis.exceptions.ResponseError:
// [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value]
}
catch(e){
console.log(e);
}
await client.lPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res36); // 3
const res40 = await client.exists('bikes:repairs')
console.log(res40); // true
const res41 = await client.lPop('bikes:repairs');
console.log(res41); // 'bike:3'
const res42 = await client.lPop('bikes:repairs');
console.log(res42); // 'bike:2'
const res43 = await client.lPop('bikes:repairs');
console.log(res43); // 'bike:1'
const res44 = await client.exists('bikes:repairs');
console.log(res44); // 0
const res45 = await client.del('bikes:repairs');
console.log(res45); // 0
const res46 = await client.lLen('bikes:repairs');
console.log(res46); // 0
const res47 = await client.lPop('bikes:repairs');
console.log(res47); // null
const res48 = await client.lPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res48); // 5
const res49 = await client.lTrim('bikes:repairs', 0, 2);
console.log(res49); // 'OK'
const res50 = await client.lRange('bikes:repairs', 0, -1);
console.log(res50); // ['bike:5', 'bike:4', 'bike:3']
package io.redis.examples;
import org.junit.Test;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.args.ListDirection;
import java.util.List;
import static org.junit.Assert.*;
public class ListExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
long res1 = jedis.lpush("bikes:repairs", "bike:1");
System.out.println(res1); // >>> 1
long res2 = jedis.lpush("bikes:repairs", "bike:2");
System.out.println(res2); // >>> 2
String res3 = jedis.rpop("bikes:repairs");
System.out.println(res3); // >>> bike:1
String res4 = jedis.rpop("bikes:repairs");
System.out.println(res4); // >>> bike:2
long res5 = jedis.lpush("bikes:repairs", "bike:1");
System.out.println(res5); // >>> 1
long res6 = jedis.lpush("bikes:repairs", "bike:2");
System.out.println(res6); // >>> 2
String res7 = jedis.lpop("bikes:repairs");
System.out.println(res7); // >>> bike:2
String res8 = jedis.lpop("bikes:repairs");
System.out.println(res8); // >>> bike:1
long res9 = jedis.llen("bikes:repairs");
System.out.println(res9); // >>> 0
long res10 = jedis.lpush("bikes:repairs", "bike:1");
System.out.println(res10); // >>> 1
long res11 = jedis.lpush("bikes:repairs", "bike:2");
System.out.println(res11); // >>> 2
String res12 = jedis.lmove("bikes:repairs", "bikes:finished", ListDirection.LEFT, ListDirection.LEFT);
System.out.println(res12); // >>> bike:2
List<String> res13 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res13); // >>> [bike:1]
List<String> res14 = jedis.lrange("bikes:finished", 0, -1);
System.out.println(res14); // >>> [bike:2]
long res15 = jedis.rpush("bikes:repairs", "bike:1");
System.out.println(res15); // >>> 1
long res16 = jedis.rpush("bikes:repairs", "bike:2");
System.out.println(res16); // >>> 2
long res17 = jedis.lpush("bikes:repairs", "bike:important_bike");
System.out.println(res17); // >>> 3
List<String> res18 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res18); // >>> [bike:important_bike, bike:1, bike:2]
long res19 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3");
System.out.println(res19); // >>> 3
long res20 = jedis.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike");
System.out.println(res20); // >>> 5
List<String> res21 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res21); // >>> [bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3]
long res22 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3");
System.out.println(res22); // >>> 3
String res23 = jedis.rpop("bikes:repairs");
System.out.println(res23); // >>> bike:3
String res24 = jedis.lpop("bikes:repairs");
System.out.println(res24); // >>> bike:1
String res25 = jedis.rpop("bikes:repairs");
System.out.println(res25); // >>> bike:2
String res26 = jedis.rpop("bikes:repairs");
System.out.println(res26); // >>> null
long res27 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
System.out.println(res27); // >>> 5
String res28 = jedis.ltrim("bikes:repairs", 0, 2);
System.out.println(res28); // >>> OK
List<String> res29 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res29); // >>> [bike:5, bike:4, bike:3]
res27 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
System.out.println(res27); // >>> 5
res28 = jedis.ltrim("bikes:repairs", -3, -1);
System.out.println(res2); // >>> OK
res29 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res29); // >>> [bike:3, bike:4, bike:5]
long res31 = jedis.rpush("bikes:repairs", "bike:1", "bike:2");
System.out.println(res31); // >>> 2
List<String> res32 = jedis.brpop(1, "bikes:repairs");
System.out.println(res32); // >>> (bikes:repairs, bike:2)
List<String> res33 = jedis.brpop(1,"bikes:repairs");
System.out.println(res33); // >>> (bikes:repairs, bike:1)
List<String> res34 = jedis.brpop(1,"bikes:repairs");
System.out.println(res34); // >>> null
long res35 = jedis.del("new_bikes");
System.out.println(res35); // >>> 0
long res36 = jedis.lpush("new_bikes", "bike:1", "bike:2", "bike:3");
System.out.println(res36); // >>> 3
String res37 = jedis.set("new_bikes", "bike:1");
System.out.println(res37); // >>> OK
String res38 = jedis.type("new_bikes");
System.out.println(res38); // >>> string
try {
long res39 = jedis.lpush("new_bikes", "bike:2", "bike:3");
} catch (Exception e) {
e.printStackTrace();
// >>> redis.clients.jedis.exceptions.JedisDataException:
// >>> WRONGTYPE Operation against a key holding the wrong kind of value
}
jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3");
System.out.println(res36); // >>> 3
boolean res40 = jedis.exists("bikes:repairs");
System.out.println(res40); // >>> true
String res41 = jedis.lpop("bikes:repairs");
System.out.println(res41); // >>> bike:3
String res42 = jedis.lpop("bikes:repairs");
System.out.println(res42); // >>> bike:2
String res43 = jedis.lpop("bikes:repairs");
System.out.println(res43); // >>> bike:1
boolean res44 = jedis.exists("bikes:repairs");
System.out.println(res44); // >>> false
long res45 = jedis.del("bikes:repairs");
System.out.println(res45); // >>> 0
long res46 = jedis.llen("bikes:repairs");
System.out.println(res46); // >>> 0
String res47 = jedis.lpop("bikes:repairs");
System.out.println(res47); // >>> null
long res48 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
System.out.println(res48); // >>> 5
String res49 = jedis.ltrim("bikes:repairs", 0, 2);
System.out.println(res49); // >>> OK
List<String> res50 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res50); // >>> [bike:5, bike:4, bike:3]
}
}
package example_commands_test
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
func ExampleClient_queue() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res1, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res1) // >>> 1
res2, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res2) // >>> 2
res3, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res3) // >>> bike:1
res4, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res4) // >>> bike:2
}
func ExampleClient_stack() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res5, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res5) // >>> 1
res6, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res6) // >>> 2
res7, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res7) // >>> bike:2
res8, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res8) // >>> bike:1
}
func ExampleClient_llen() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res9, err := rdb.LLen(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res9) // >>> 0
}
func ExampleClient_lmove_lrange() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res10, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res10) // >>> 1
res11, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res11) // >>> 2
res12, err := rdb.LMove(ctx, "bikes:repairs", "bikes:finished", "LEFT", "LEFT").Result()
if err != nil {
panic(err)
}
fmt.Println(res12) // >>> bike:2
res13, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res13) // >>> [bike:1]
res14, err := rdb.LRange(ctx, "bikes:finished", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res14) // >>> [bike:2]
}
func ExampleClient_lpush_rpush() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res15, err := rdb.RPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res15) // >>> 1
res16, err := rdb.RPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res16) // >>> 2
res17, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike").Result()
if err != nil {
panic(err)
}
fmt.Println(res17) // >>> 3
res18, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res18) // >>> [bike:important_bike bike:1 bike:2]
}
func ExampleClient_variadic() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res19, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res19) // >>> 3
res20, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike", "bike:very_important_bike").Result()
if err != nil {
panic(err)
}
fmt.Println(res20) // >>> 5
res21, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res21) // >>> [bike:very_important_bike bike:important_bike bike:1 bike:2 bike:3]
}
func ExampleClient_lpop_rpop() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res22, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res22) // >>> 3
res23, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res23) // >>> bike:3
res24, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res24) // >>> bike:1
res25, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res25) // >>> bike:2
res26, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
fmt.Println(err) // >>> redis: nil
}
fmt.Println(res26) // >>> <empty string>
}
func ExampleClient_ltrim() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res27, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
fmt.Println(res27) // >>> 5
res28, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result()
if err != nil {
panic(err)
}
fmt.Println(res28) // >>> OK
res29, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res29) // >>> [bike:5 bike:4 bike:3]
}
func ExampleClient_ltrim_end_of_list() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res30, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
fmt.Println(res30) // >>> 5
res31, err := rdb.LTrim(ctx, "bikes:repairs", -3, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res31) // >>> OK
res32, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res32) // >>> [bike:3 bike:4 bike:5]
}
func ExampleClient_brpop() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res33, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res33) // >>> 2
res34, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res34) // >>> [bikes:repairs bike:2]
res35, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res35) // >>> [bikes:repairs bike:1]
res36, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
if err != nil {
fmt.Println(err) // >>> redis: nil
}
fmt.Println(res36) // >>> []
}
func ExampleClient_rule1() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res37, err := rdb.Del(ctx, "new_bikes").Result()
if err != nil {
panic(err)
}
fmt.Println(res37) // >>> 0
res38, err := rdb.LPush(ctx, "new_bikes", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res38) // >>> 3
}
func ExampleClient_rule11() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res39, err := rdb.Set(ctx, "new_bikes", "bike:1", 0).Result()
if err != nil {
panic(err)
}
fmt.Println(res39) // >>> OK
res40, err := rdb.Type(ctx, "new_bikes").Result()
if err != nil {
panic(err)
}
fmt.Println(res40) // >>> string
res41, err := rdb.LPush(ctx, "new_bikes", "bike:2", "bike:3").Result()
if err != nil {
fmt.Println(err)
// >>> WRONGTYPE Operation against a key holding the wrong kind of value
}
fmt.Println(res41)
}
func ExampleClient_rule2() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res42, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res42) // >>> 3
res43, err := rdb.Exists(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res43) // >>> 1
res44, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res44) // >>> bike:3
res45, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res45) // >>> bike:2
res46, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res46) // >>> bike:1
res47, err := rdb.Exists(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res47) // >>> 0
}
func ExampleClient_rule3() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res48, err := rdb.Del(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res48) // >>> 0
res49, err := rdb.LLen(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res49) // >>> 0
res50, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
fmt.Println(err) // >>> redis: nil
}
fmt.Println(res50) // >>> <empty string>
}
func ExampleClient_ltrim1() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res51, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
fmt.Println(res51) // >>> 5
res52, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result()
if err != nil {
panic(err)
}
fmt.Println(res52) // >>> OK
res53, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res53) // >>> [bike:5 bike:4 bike:3]
}
public class ListExample
{
public void run()
{
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
long res1 = db.ListLeftPush("bikes:repairs", "bike:1");
Console.WriteLine(res1); // >>> 1
long res2 = db.ListLeftPush("bikes:repairs", "bike:2");
Console.WriteLine(res2); // >>> 2
RedisValue res3 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res3); // >>> "bike:1"
RedisValue res4 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res4); // >>> "bike:2"
long res5 = db.ListLeftPush("bikes:repairs", "bike:1");
Console.WriteLine(res5); // >>> 1
long res6 = db.ListLeftPush("bikes:repairs", "bike:2");
Console.WriteLine(res6); // >>> 2
RedisValue res7 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res7); // >>> "bike:2"
RedisValue res8 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res8); // >>> "bike:1"
long res9 = db.ListLength("bikes:repairs");
Console.WriteLine(res9); // >>> 0
long res10 = db.ListLeftPush("{bikes}:repairs", "bike:1");
Console.WriteLine(res10); // >>> 1
long res11 = db.ListLeftPush("{bikes}:repairs", "bike:2");
Console.WriteLine(res11); // >>> 2
RedisValue res12 = db.ListMove("{bikes}:repairs", "{bikes}:finished", ListSide.Left, ListSide.Left);
Console.Write(res12); // >>> "bike:2"
RedisValue[] res13 = db.ListRange("{bikes}:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res13)); // >>> "bike:1"
RedisValue[] res14 = db.ListRange("{bikes}:finished", 0, -1);
Console.WriteLine(string.Join(", ", res14)); // >>> "bike:2"
long res15 = db.ListRightPush("bikes:repairs", "bike:1");
Console.WriteLine(res15); // >>> 1
long res16 = db.ListRightPush("bikes:repairs", "bike:2");
Console.WriteLine(res16); // >>> 2
long res17 = db.ListLeftPush("bikes:repairs", "bike:important_bike");
Console.WriteLine(res17); // >>> 3
RedisValue[] res18 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res18)); // >>> "bike:important_bike, bike:1, bike:2"
long res19 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res19); // >>> 3
long res20 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:important_bike", "bike:very_important_bike" });
Console.WriteLine(res20); // >>> 5
RedisValue[] res21 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res21));
// >>> "bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3"
long res22 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res22); // >>> 3
RedisValue res23 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res23); // >>> "bike:3"
RedisValue res24 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res24); // >>> "bike:1"
RedisValue res25 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res25); // >>> "bike:2"
RedisValue res26 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res26); // >>> <Empty string>
long res27 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
Console.WriteLine(res27); // >>> 5
db.ListTrim("bikes:repairs", 0, 2);
RedisValue[] res28 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res28)); // "bike:5, bike:4, bike:3"
long res29 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
Console.WriteLine(res29); // >>> 5
db.ListTrim("bikes:repairs", -3, -1);
RedisValue[] res30 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res30)); // >>> "bike:3, bike:4, bike:5"
long res31 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2" });
Console.WriteLine(res31); // >>> 2
Tuple<RedisKey, RedisValue>? res32 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1);
if (res32 != null)
Console.WriteLine($"{res32.Item1} -> {res32.Item2}"); // >>> "bikes:repairs -> bike:2"
Tuple<RedisKey, RedisValue>? res33 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1);
if (res33 != null)
Console.WriteLine($"{res33.Item1} -> {res33.Item2}"); // >>> "bikes:repairs -> bike:1"
Tuple<RedisKey, RedisValue>? res34 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1);
Console.WriteLine(res34); // >>> "Null"
bool res35 = db.KeyDelete("new_bikes");
Console.WriteLine(res35); // >>> False
long res36 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res36); // >>> 3
bool res37 = db.StringSet("new_bikes", "bike:1");
Console.WriteLine(res37); // >>> True
RedisType res38 = db.KeyType("new_bikes");
Console.WriteLine(res38); // >>> RedisType.String
try
{
long res39 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:2", "bike:3" });
}
catch (Exception e)
{
Console.WriteLine(e);
}
long res40 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res40); // >>> 3
bool res41 = db.KeyExists("bikes:repairs");
Console.WriteLine(res41); // >>> True
RedisValue res42 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res42); // >>> "bike:3"
RedisValue res43 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res43); // >>> "bike:2"
RedisValue res44 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res44); // >>> "bike:1"
bool res45 = db.KeyExists("bikes:repairs");
Console.WriteLine(res45); // >>> False
bool res46 = db.KeyDelete("bikes:repairs");
Console.WriteLine(res46); // >>> False
long res47 = db.ListLength("bikes:repairs");
Console.WriteLine(res47); // >>> 0
RedisValue res48 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res48); // >>> Null
long res49 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
Console.WriteLine(res49); // >>> 5
db.ListTrim("bikes:repairs", 0, 2);
RedisValue[] res50 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res50)); // >>> "bike:5, bike:4, bike:3"
}
}
It means: "wait for elements in the list bikes:repairs
, but return if after 1 second
no element is available".
Note that you can use 0 as timeout to wait for elements forever, and you can also specify multiple lists and not just one, in order to wait on multiple lists at the same time, and get notified when the first list receives an element.
A few things to note about BRPOP
:
- Clients are served in an ordered way: the first client that blocked waiting for a list, is served first when an element is pushed by some other client, and so forth.
- The return value is different compared to
RPOP
: it is a two-element array since it also includes the name of the key, becauseBRPOP
andBLPOP
are able to block waiting for elements from multiple lists. - If the timeout is reached, NULL is returned.
There are more things you should know about lists and blocking ops. We suggest that you read more on the following:
- It is possible to build safer queues or rotating queues using
LMOVE
. - There is also a blocking variant of the command, called
BLMOVE
.
Automatic creation and removal of keys
So far in our examples we never had to create empty lists before pushing
elements, or removing empty lists when they no longer have elements inside.
It is Redis' responsibility to delete keys when lists are left empty, or to create
an empty list if the key does not exist and we are trying to add elements
to it, for example, with LPUSH
.
This is not specific to lists, it applies to all the Redis data types composed of multiple elements -- Streams, Sets, Sorted Sets and Hashes.
Basically we can summarize the behavior with three rules:
- When we add an element to an aggregate data type, if the target key does not exist, an empty aggregate data type is created before adding the element.
- When we remove elements from an aggregate data type, if the value remains empty, the key is automatically destroyed. The Stream data type is the only exception to this rule.
- Calling a read-only command such as
LLEN
(which returns the length of the list), or a write command removing elements, with an empty key, always produces the same result as if the key is holding an empty aggregate type of the type the command expects to find.
Examples of rule 1:
> DEL new_bikes
(integer) 0
> LPUSH new_bikes bike:1 bike:2 bike:3
(integer) 3
"""
Code samples for List doc pages:
https://redis.io/docs/latest/develop/data-types/lists/
"""
import redis
r = redis.Redis(decode_responses=True)
res1 = r.lpush("bikes:repairs", "bike:1")
print(res1) # >>> 1
res2 = r.lpush("bikes:repairs", "bike:2")
print(res2) # >>> 2
res3 = r.rpop("bikes:repairs")
print(res3) # >>> bike:1
res4 = r.rpop("bikes:repairs")
print(res4) # >>> bike:2
res5 = r.lpush("bikes:repairs", "bike:1")
print(res5) # >>> 1
res6 = r.lpush("bikes:repairs", "bike:2")
print(res6) # >>> 2
res7 = r.lpop("bikes:repairs")
print(res7) # >>> bike:2
res8 = r.lpop("bikes:repairs")
print(res8) # >>> bike:1
res9 = r.llen("bikes:repairs")
print(res9) # >>> 0
res10 = r.lpush("bikes:repairs", "bike:1")
print(res10) # >>> 1
res11 = r.lpush("bikes:repairs", "bike:2")
print(res11) # >>> 2
res12 = r.lmove("bikes:repairs", "bikes:finished", "LEFT", "LEFT")
print(res12) # >>> 'bike:2'
res13 = r.lrange("bikes:repairs", 0, -1)
print(res13) # >>> ['bike:1']
res14 = r.lrange("bikes:finished", 0, -1)
print(res14) # >>> ['bike:2']
res15 = r.rpush("bikes:repairs", "bike:1")
print(res15) # >>> 1
res16 = r.rpush("bikes:repairs", "bike:2")
print(res16) # >>> 2
res17 = r.lpush("bikes:repairs", "bike:important_bike")
print(res17) # >>> 3
res18 = r.lrange("bikes:repairs", 0, -1)
print(res18) # >>> ['bike:important_bike', 'bike:1', 'bike:2']
res19 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res19) # >>> 3
res20 = r.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike")
print(res20) # >>> 5
res21 = r.lrange("bikes:repairs", 0, -1)
print(
res21
) # >>> ['bike:very_important_bike', 'bike:important_bike', 'bike:1', ...
res22 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res22) # >>> 3
res23 = r.rpop("bikes:repairs")
print(res23) # >>> 'bike:3'
res24 = r.lpop("bikes:repairs")
print(res24) # >>> 'bike:1'
res25 = r.rpop("bikes:repairs")
print(res25) # >>> 'bike:2'
res26 = r.rpop("bikes:repairs")
print(res26) # >>> None
res27 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res27) # >>> 5
res28 = r.ltrim("bikes:repairs", 0, 2)
print(res28) # >>> True
res29 = r.lrange("bikes:repairs", 0, -1)
print(res29) # >>> ['bike:5', 'bike:4', 'bike:3']
res27 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res27) # >>> 5
res28 = r.ltrim("bikes:repairs", -3, -1)
print(res28) # >>> True
res29 = r.lrange("bikes:repairs", 0, -1)
print(res29) # >>> ['bike:3', 'bike:4', 'bike:5']
res31 = r.rpush("bikes:repairs", "bike:1", "bike:2")
print(res31) # >>> 2
res32 = r.brpop("bikes:repairs", timeout=1)
print(res32) # >>> ('bikes:repairs', 'bike:2')
res33 = r.brpop("bikes:repairs", timeout=1)
print(res33) # >>> ('bikes:repairs', 'bike:1')
res34 = r.brpop("bikes:repairs", timeout=1)
print(res34) # >>> None
res35 = r.delete("new_bikes")
print(res35) # >>> 0
res36 = r.lpush("new_bikes", "bike:1", "bike:2", "bike:3")
print(res36) # >>> 3
res37 = r.set("new_bikes", "bike:1")
print(res37) # >>> True
res38 = r.type("new_bikes")
print(res38) # >>> 'string'
try:
res39 = r.lpush("new_bikes", "bike:2", "bike:3")
# >>> redis.exceptions.ResponseError:
# >>> WRONGTYPE Operation against a key holding the wrong kind of value
except redis.exceptions.ResponseError as e:
print(e)
r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res36) # >>> 3
res40 = r.exists("bikes:repairs")
print(res40) # >>> 1
res41 = r.lpop("bikes:repairs")
print(res41) # >>> 'bike:3'
res42 = r.lpop("bikes:repairs")
print(res42) # >>> 'bike:2'
res43 = r.lpop("bikes:repairs")
print(res43) # >>> 'bike:1'
res44 = r.exists("bikes:repairs")
print(res44) # >>> False
res45 = r.delete("bikes:repairs")
print(res45) # >>> 0
res46 = r.llen("bikes:repairs")
print(res46) # >>> 0
res47 = r.lpop("bikes:repairs")
print(res47) # >>> None
res48 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res48) # >>> 5
res49 = r.ltrim("bikes:repairs", 0, 2)
print(res49) # >>> True
res50 = r.lrange("bikes:repairs", 0, -1)
print(res50) # >>> ['bike:5', 'bike:4', 'bike:3']
import assert from 'assert';
import { createClient } from 'redis';
const client = await createClient();
await client.connect();
const res1 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res1); // 1
const res2 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res2); // 2
const res3 = await client.rPop('bikes:repairs');
console.log(res3); // bike:1
const res4 = await client.rPop('bikes:repairs');
console.log(res4); // bike:2
const res5 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res5); // 1
const res6 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res6); // 2
const res7 = await client.lPop('bikes:repairs');
console.log(res7); // bike:2
const res8 = await client.lPop('bikes:repairs');
console.log(res8); // bike:1
const res9 = await client.lLen('bikes:repairs');
console.log(res9); // 0
const res10 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res10); // 1
const res11 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res11); // 2
const res12 = await client.lMove('bikes:repairs', 'bikes:finished', 'LEFT', 'LEFT');
console.log(res12); // 'bike:2'
const res13 = await client.lRange('bikes:repairs', 0, -1);
console.log(res13); // ['bike:1']
const res14 = await client.lRange('bikes:finished', 0, -1);
console.log(res14); // ['bike:2']
const res15 = await client.rPush('bikes:repairs', 'bike:1');
console.log(res15); // 1
const res16 = await client.rPush('bikes:repairs', 'bike:2');
console.log(res16); // 2
const res17 = await client.lPush('bikes:repairs', 'bike:important_bike');
console.log(res17); // 3
const res18 = await client.lRange('bikes:repairs', 0, -1);
console.log(res18); // ['bike:important_bike', 'bike:1', 'bike:2']
const res19 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res19); // 3
const res20 = await client.lPush(
'bikes:repairs', ['bike:important_bike', 'bike:very_important_bike']
);
console.log(res20); // 5
const res21 = await client.lRange('bikes:repairs', 0, -1);
console.log(res21); // ['bike:very_important_bike', 'bike:important_bike', 'bike:1', 'bike:2', 'bike:3']
const res22 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res22); // 3
const res23 = await client.rPop('bikes:repairs');
console.log(res23); // 'bike:3'
const res24 = await client.lPop('bikes:repairs');
console.log(res24); // 'bike:1'
const res25 = await client.rPop('bikes:repairs');
console.log(res25); // 'bike:2'
const res26 = await client.rPop('bikes:repairs');
console.log(res26); // None
const res27 = await client.lPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res27); // 5
const res28 = await client.lTrim('bikes:repairs', 0, 2);
console.log(res28); // true
const res29 = await client.lRange('bikes:repairs', 0, -1);
console.log(res29); // ['bike:5', 'bike:4', 'bike:3']
const res27eol = await client.rPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res27eol); // 5
const res28eol = await client.lTrim('bikes:repairs', -3, -1);
console.log(res28eol); // 'OK'
const res29eol = await client.lRange('bikes:repairs', 0, -1);
console.log(res29eol); // ['bike:3', 'bike:4', 'bike:5']
const res31 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2']);
console.log(res31); // 2
const res32 = await client.brPop('bikes:repairs', 1);
console.log(res32); // { key: 'bikes:repairs', element: 'bike:2' }
const res33 = await client.brPop('bikes:repairs', 1);
console.log(res33); // { key: 'bikes:repairs', element: 'bike:1' }
const res34 = await client.brPop('bikes:repairs', 1);
console.log(res34); // null
const res35 = await client.del('new_bikes');
console.log(res35); // 0
const res36 = await client.lPush('new_bikes', ['bike:1', 'bike:2', 'bike:3']);
console.log(res36); // 3
const res37 = await client.set('new_bikes', 'bike:1');
console.log(res37); // 'OK'
const res38 = await client.type('new_bikes');
console.log(res38); // 'string'
try {
const res39 = await client.lPush('new_bikes', 'bike:2', 'bike:3');
// redis.exceptions.ResponseError:
// [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value]
}
catch(e){
console.log(e);
}
await client.lPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res36); // 3
const res40 = await client.exists('bikes:repairs')
console.log(res40); // true
const res41 = await client.lPop('bikes:repairs');
console.log(res41); // 'bike:3'
const res42 = await client.lPop('bikes:repairs');
console.log(res42); // 'bike:2'
const res43 = await client.lPop('bikes:repairs');
console.log(res43); // 'bike:1'
const res44 = await client.exists('bikes:repairs');
console.log(res44); // 0
const res45 = await client.del('bikes:repairs');
console.log(res45); // 0
const res46 = await client.lLen('bikes:repairs');
console.log(res46); // 0
const res47 = await client.lPop('bikes:repairs');
console.log(res47); // null
const res48 = await client.lPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res48); // 5
const res49 = await client.lTrim('bikes:repairs', 0, 2);
console.log(res49); // 'OK'
const res50 = await client.lRange('bikes:repairs', 0, -1);
console.log(res50); // ['bike:5', 'bike:4', 'bike:3']
package io.redis.examples;
import org.junit.Test;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.args.ListDirection;
import java.util.List;
import static org.junit.Assert.*;
public class ListExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
long res1 = jedis.lpush("bikes:repairs", "bike:1");
System.out.println(res1); // >>> 1
long res2 = jedis.lpush("bikes:repairs", "bike:2");
System.out.println(res2); // >>> 2
String res3 = jedis.rpop("bikes:repairs");
System.out.println(res3); // >>> bike:1
String res4 = jedis.rpop("bikes:repairs");
System.out.println(res4); // >>> bike:2
long res5 = jedis.lpush("bikes:repairs", "bike:1");
System.out.println(res5); // >>> 1
long res6 = jedis.lpush("bikes:repairs", "bike:2");
System.out.println(res6); // >>> 2
String res7 = jedis.lpop("bikes:repairs");
System.out.println(res7); // >>> bike:2
String res8 = jedis.lpop("bikes:repairs");
System.out.println(res8); // >>> bike:1
long res9 = jedis.llen("bikes:repairs");
System.out.println(res9); // >>> 0
long res10 = jedis.lpush("bikes:repairs", "bike:1");
System.out.println(res10); // >>> 1
long res11 = jedis.lpush("bikes:repairs", "bike:2");
System.out.println(res11); // >>> 2
String res12 = jedis.lmove("bikes:repairs", "bikes:finished", ListDirection.LEFT, ListDirection.LEFT);
System.out.println(res12); // >>> bike:2
List<String> res13 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res13); // >>> [bike:1]
List<String> res14 = jedis.lrange("bikes:finished", 0, -1);
System.out.println(res14); // >>> [bike:2]
long res15 = jedis.rpush("bikes:repairs", "bike:1");
System.out.println(res15); // >>> 1
long res16 = jedis.rpush("bikes:repairs", "bike:2");
System.out.println(res16); // >>> 2
long res17 = jedis.lpush("bikes:repairs", "bike:important_bike");
System.out.println(res17); // >>> 3
List<String> res18 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res18); // >>> [bike:important_bike, bike:1, bike:2]
long res19 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3");
System.out.println(res19); // >>> 3
long res20 = jedis.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike");
System.out.println(res20); // >>> 5
List<String> res21 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res21); // >>> [bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3]
long res22 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3");
System.out.println(res22); // >>> 3
String res23 = jedis.rpop("bikes:repairs");
System.out.println(res23); // >>> bike:3
String res24 = jedis.lpop("bikes:repairs");
System.out.println(res24); // >>> bike:1
String res25 = jedis.rpop("bikes:repairs");
System.out.println(res25); // >>> bike:2
String res26 = jedis.rpop("bikes:repairs");
System.out.println(res26); // >>> null
long res27 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
System.out.println(res27); // >>> 5
String res28 = jedis.ltrim("bikes:repairs", 0, 2);
System.out.println(res28); // >>> OK
List<String> res29 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res29); // >>> [bike:5, bike:4, bike:3]
res27 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
System.out.println(res27); // >>> 5
res28 = jedis.ltrim("bikes:repairs", -3, -1);
System.out.println(res2); // >>> OK
res29 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res29); // >>> [bike:3, bike:4, bike:5]
long res31 = jedis.rpush("bikes:repairs", "bike:1", "bike:2");
System.out.println(res31); // >>> 2
List<String> res32 = jedis.brpop(1, "bikes:repairs");
System.out.println(res32); // >>> (bikes:repairs, bike:2)
List<String> res33 = jedis.brpop(1,"bikes:repairs");
System.out.println(res33); // >>> (bikes:repairs, bike:1)
List<String> res34 = jedis.brpop(1,"bikes:repairs");
System.out.println(res34); // >>> null
long res35 = jedis.del("new_bikes");
System.out.println(res35); // >>> 0
long res36 = jedis.lpush("new_bikes", "bike:1", "bike:2", "bike:3");
System.out.println(res36); // >>> 3
String res37 = jedis.set("new_bikes", "bike:1");
System.out.println(res37); // >>> OK
String res38 = jedis.type("new_bikes");
System.out.println(res38); // >>> string
try {
long res39 = jedis.lpush("new_bikes", "bike:2", "bike:3");
} catch (Exception e) {
e.printStackTrace();
// >>> redis.clients.jedis.exceptions.JedisDataException:
// >>> WRONGTYPE Operation against a key holding the wrong kind of value
}
jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3");
System.out.println(res36); // >>> 3
boolean res40 = jedis.exists("bikes:repairs");
System.out.println(res40); // >>> true
String res41 = jedis.lpop("bikes:repairs");
System.out.println(res41); // >>> bike:3
String res42 = jedis.lpop("bikes:repairs");
System.out.println(res42); // >>> bike:2
String res43 = jedis.lpop("bikes:repairs");
System.out.println(res43); // >>> bike:1
boolean res44 = jedis.exists("bikes:repairs");
System.out.println(res44); // >>> false
long res45 = jedis.del("bikes:repairs");
System.out.println(res45); // >>> 0
long res46 = jedis.llen("bikes:repairs");
System.out.println(res46); // >>> 0
String res47 = jedis.lpop("bikes:repairs");
System.out.println(res47); // >>> null
long res48 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
System.out.println(res48); // >>> 5
String res49 = jedis.ltrim("bikes:repairs", 0, 2);
System.out.println(res49); // >>> OK
List<String> res50 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res50); // >>> [bike:5, bike:4, bike:3]
}
}
package example_commands_test
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
func ExampleClient_queue() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res1, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res1) // >>> 1
res2, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res2) // >>> 2
res3, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res3) // >>> bike:1
res4, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res4) // >>> bike:2
}
func ExampleClient_stack() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res5, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res5) // >>> 1
res6, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res6) // >>> 2
res7, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res7) // >>> bike:2
res8, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res8) // >>> bike:1
}
func ExampleClient_llen() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res9, err := rdb.LLen(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res9) // >>> 0
}
func ExampleClient_lmove_lrange() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res10, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res10) // >>> 1
res11, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res11) // >>> 2
res12, err := rdb.LMove(ctx, "bikes:repairs", "bikes:finished", "LEFT", "LEFT").Result()
if err != nil {
panic(err)
}
fmt.Println(res12) // >>> bike:2
res13, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res13) // >>> [bike:1]
res14, err := rdb.LRange(ctx, "bikes:finished", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res14) // >>> [bike:2]
}
func ExampleClient_lpush_rpush() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res15, err := rdb.RPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res15) // >>> 1
res16, err := rdb.RPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res16) // >>> 2
res17, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike").Result()
if err != nil {
panic(err)
}
fmt.Println(res17) // >>> 3
res18, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res18) // >>> [bike:important_bike bike:1 bike:2]
}
func ExampleClient_variadic() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res19, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res19) // >>> 3
res20, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike", "bike:very_important_bike").Result()
if err != nil {
panic(err)
}
fmt.Println(res20) // >>> 5
res21, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res21) // >>> [bike:very_important_bike bike:important_bike bike:1 bike:2 bike:3]
}
func ExampleClient_lpop_rpop() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res22, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res22) // >>> 3
res23, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res23) // >>> bike:3
res24, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res24) // >>> bike:1
res25, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res25) // >>> bike:2
res26, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
fmt.Println(err) // >>> redis: nil
}
fmt.Println(res26) // >>> <empty string>
}
func ExampleClient_ltrim() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res27, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
fmt.Println(res27) // >>> 5
res28, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result()
if err != nil {
panic(err)
}
fmt.Println(res28) // >>> OK
res29, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res29) // >>> [bike:5 bike:4 bike:3]
}
func ExampleClient_ltrim_end_of_list() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res30, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
fmt.Println(res30) // >>> 5
res31, err := rdb.LTrim(ctx, "bikes:repairs", -3, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res31) // >>> OK
res32, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res32) // >>> [bike:3 bike:4 bike:5]
}
func ExampleClient_brpop() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res33, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res33) // >>> 2
res34, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res34) // >>> [bikes:repairs bike:2]
res35, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res35) // >>> [bikes:repairs bike:1]
res36, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
if err != nil {
fmt.Println(err) // >>> redis: nil
}
fmt.Println(res36) // >>> []
}
func ExampleClient_rule1() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res37, err := rdb.Del(ctx, "new_bikes").Result()
if err != nil {
panic(err)
}
fmt.Println(res37) // >>> 0
res38, err := rdb.LPush(ctx, "new_bikes", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res38) // >>> 3
}
func ExampleClient_rule11() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res39, err := rdb.Set(ctx, "new_bikes", "bike:1", 0).Result()
if err != nil {
panic(err)
}
fmt.Println(res39) // >>> OK
res40, err := rdb.Type(ctx, "new_bikes").Result()
if err != nil {
panic(err)
}
fmt.Println(res40) // >>> string
res41, err := rdb.LPush(ctx, "new_bikes", "bike:2", "bike:3").Result()
if err != nil {
fmt.Println(err)
// >>> WRONGTYPE Operation against a key holding the wrong kind of value
}
fmt.Println(res41)
}
func ExampleClient_rule2() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res42, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res42) // >>> 3
res43, err := rdb.Exists(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res43) // >>> 1
res44, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res44) // >>> bike:3
res45, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res45) // >>> bike:2
res46, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res46) // >>> bike:1
res47, err := rdb.Exists(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res47) // >>> 0
}
func ExampleClient_rule3() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res48, err := rdb.Del(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res48) // >>> 0
res49, err := rdb.LLen(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res49) // >>> 0
res50, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
fmt.Println(err) // >>> redis: nil
}
fmt.Println(res50) // >>> <empty string>
}
func ExampleClient_ltrim1() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res51, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
fmt.Println(res51) // >>> 5
res52, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result()
if err != nil {
panic(err)
}
fmt.Println(res52) // >>> OK
res53, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res53) // >>> [bike:5 bike:4 bike:3]
}
public class ListExample
{
public void run()
{
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
long res1 = db.ListLeftPush("bikes:repairs", "bike:1");
Console.WriteLine(res1); // >>> 1
long res2 = db.ListLeftPush("bikes:repairs", "bike:2");
Console.WriteLine(res2); // >>> 2
RedisValue res3 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res3); // >>> "bike:1"
RedisValue res4 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res4); // >>> "bike:2"
long res5 = db.ListLeftPush("bikes:repairs", "bike:1");
Console.WriteLine(res5); // >>> 1
long res6 = db.ListLeftPush("bikes:repairs", "bike:2");
Console.WriteLine(res6); // >>> 2
RedisValue res7 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res7); // >>> "bike:2"
RedisValue res8 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res8); // >>> "bike:1"
long res9 = db.ListLength("bikes:repairs");
Console.WriteLine(res9); // >>> 0
long res10 = db.ListLeftPush("{bikes}:repairs", "bike:1");
Console.WriteLine(res10); // >>> 1
long res11 = db.ListLeftPush("{bikes}:repairs", "bike:2");
Console.WriteLine(res11); // >>> 2
RedisValue res12 = db.ListMove("{bikes}:repairs", "{bikes}:finished", ListSide.Left, ListSide.Left);
Console.Write(res12); // >>> "bike:2"
RedisValue[] res13 = db.ListRange("{bikes}:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res13)); // >>> "bike:1"
RedisValue[] res14 = db.ListRange("{bikes}:finished", 0, -1);
Console.WriteLine(string.Join(", ", res14)); // >>> "bike:2"
long res15 = db.ListRightPush("bikes:repairs", "bike:1");
Console.WriteLine(res15); // >>> 1
long res16 = db.ListRightPush("bikes:repairs", "bike:2");
Console.WriteLine(res16); // >>> 2
long res17 = db.ListLeftPush("bikes:repairs", "bike:important_bike");
Console.WriteLine(res17); // >>> 3
RedisValue[] res18 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res18)); // >>> "bike:important_bike, bike:1, bike:2"
long res19 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res19); // >>> 3
long res20 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:important_bike", "bike:very_important_bike" });
Console.WriteLine(res20); // >>> 5
RedisValue[] res21 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res21));
// >>> "bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3"
long res22 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res22); // >>> 3
RedisValue res23 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res23); // >>> "bike:3"
RedisValue res24 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res24); // >>> "bike:1"
RedisValue res25 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res25); // >>> "bike:2"
RedisValue res26 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res26); // >>> <Empty string>
long res27 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
Console.WriteLine(res27); // >>> 5
db.ListTrim("bikes:repairs", 0, 2);
RedisValue[] res28 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res28)); // "bike:5, bike:4, bike:3"
long res29 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
Console.WriteLine(res29); // >>> 5
db.ListTrim("bikes:repairs", -3, -1);
RedisValue[] res30 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res30)); // >>> "bike:3, bike:4, bike:5"
long res31 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2" });
Console.WriteLine(res31); // >>> 2
Tuple<RedisKey, RedisValue>? res32 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1);
if (res32 != null)
Console.WriteLine($"{res32.Item1} -> {res32.Item2}"); // >>> "bikes:repairs -> bike:2"
Tuple<RedisKey, RedisValue>? res33 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1);
if (res33 != null)
Console.WriteLine($"{res33.Item1} -> {res33.Item2}"); // >>> "bikes:repairs -> bike:1"
Tuple<RedisKey, RedisValue>? res34 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1);
Console.WriteLine(res34); // >>> "Null"
bool res35 = db.KeyDelete("new_bikes");
Console.WriteLine(res35); // >>> False
long res36 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res36); // >>> 3
bool res37 = db.StringSet("new_bikes", "bike:1");
Console.WriteLine(res37); // >>> True
RedisType res38 = db.KeyType("new_bikes");
Console.WriteLine(res38); // >>> RedisType.String
try
{
long res39 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:2", "bike:3" });
}
catch (Exception e)
{
Console.WriteLine(e);
}
long res40 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res40); // >>> 3
bool res41 = db.KeyExists("bikes:repairs");
Console.WriteLine(res41); // >>> True
RedisValue res42 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res42); // >>> "bike:3"
RedisValue res43 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res43); // >>> "bike:2"
RedisValue res44 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res44); // >>> "bike:1"
bool res45 = db.KeyExists("bikes:repairs");
Console.WriteLine(res45); // >>> False
bool res46 = db.KeyDelete("bikes:repairs");
Console.WriteLine(res46); // >>> False
long res47 = db.ListLength("bikes:repairs");
Console.WriteLine(res47); // >>> 0
RedisValue res48 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res48); // >>> Null
long res49 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
Console.WriteLine(res49); // >>> 5
db.ListTrim("bikes:repairs", 0, 2);
RedisValue[] res50 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res50)); // >>> "bike:5, bike:4, bike:3"
}
}
However we can't perform operations against the wrong type if the key exists:
> SET new_bikes bike:1
OK
> TYPE new_bikes
string
> LPUSH new_bikes bike:2 bike:3
(error) WRONGTYPE Operation against a key holding the wrong kind of value
"""
Code samples for List doc pages:
https://redis.io/docs/latest/develop/data-types/lists/
"""
import redis
r = redis.Redis(decode_responses=True)
res1 = r.lpush("bikes:repairs", "bike:1")
print(res1) # >>> 1
res2 = r.lpush("bikes:repairs", "bike:2")
print(res2) # >>> 2
res3 = r.rpop("bikes:repairs")
print(res3) # >>> bike:1
res4 = r.rpop("bikes:repairs")
print(res4) # >>> bike:2
res5 = r.lpush("bikes:repairs", "bike:1")
print(res5) # >>> 1
res6 = r.lpush("bikes:repairs", "bike:2")
print(res6) # >>> 2
res7 = r.lpop("bikes:repairs")
print(res7) # >>> bike:2
res8 = r.lpop("bikes:repairs")
print(res8) # >>> bike:1
res9 = r.llen("bikes:repairs")
print(res9) # >>> 0
res10 = r.lpush("bikes:repairs", "bike:1")
print(res10) # >>> 1
res11 = r.lpush("bikes:repairs", "bike:2")
print(res11) # >>> 2
res12 = r.lmove("bikes:repairs", "bikes:finished", "LEFT", "LEFT")
print(res12) # >>> 'bike:2'
res13 = r.lrange("bikes:repairs", 0, -1)
print(res13) # >>> ['bike:1']
res14 = r.lrange("bikes:finished", 0, -1)
print(res14) # >>> ['bike:2']
res15 = r.rpush("bikes:repairs", "bike:1")
print(res15) # >>> 1
res16 = r.rpush("bikes:repairs", "bike:2")
print(res16) # >>> 2
res17 = r.lpush("bikes:repairs", "bike:important_bike")
print(res17) # >>> 3
res18 = r.lrange("bikes:repairs", 0, -1)
print(res18) # >>> ['bike:important_bike', 'bike:1', 'bike:2']
res19 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res19) # >>> 3
res20 = r.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike")
print(res20) # >>> 5
res21 = r.lrange("bikes:repairs", 0, -1)
print(
res21
) # >>> ['bike:very_important_bike', 'bike:important_bike', 'bike:1', ...
res22 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res22) # >>> 3
res23 = r.rpop("bikes:repairs")
print(res23) # >>> 'bike:3'
res24 = r.lpop("bikes:repairs")
print(res24) # >>> 'bike:1'
res25 = r.rpop("bikes:repairs")
print(res25) # >>> 'bike:2'
res26 = r.rpop("bikes:repairs")
print(res26) # >>> None
res27 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res27) # >>> 5
res28 = r.ltrim("bikes:repairs", 0, 2)
print(res28) # >>> True
res29 = r.lrange("bikes:repairs", 0, -1)
print(res29) # >>> ['bike:5', 'bike:4', 'bike:3']
res27 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res27) # >>> 5
res28 = r.ltrim("bikes:repairs", -3, -1)
print(res28) # >>> True
res29 = r.lrange("bikes:repairs", 0, -1)
print(res29) # >>> ['bike:3', 'bike:4', 'bike:5']
res31 = r.rpush("bikes:repairs", "bike:1", "bike:2")
print(res31) # >>> 2
res32 = r.brpop("bikes:repairs", timeout=1)
print(res32) # >>> ('bikes:repairs', 'bike:2')
res33 = r.brpop("bikes:repairs", timeout=1)
print(res33) # >>> ('bikes:repairs', 'bike:1')
res34 = r.brpop("bikes:repairs", timeout=1)
print(res34) # >>> None
res35 = r.delete("new_bikes")
print(res35) # >>> 0
res36 = r.lpush("new_bikes", "bike:1", "bike:2", "bike:3")
print(res36) # >>> 3
res37 = r.set("new_bikes", "bike:1")
print(res37) # >>> True
res38 = r.type("new_bikes")
print(res38) # >>> 'string'
try:
res39 = r.lpush("new_bikes", "bike:2", "bike:3")
# >>> redis.exceptions.ResponseError:
# >>> WRONGTYPE Operation against a key holding the wrong kind of value
except redis.exceptions.ResponseError as e:
print(e)
r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res36) # >>> 3
res40 = r.exists("bikes:repairs")
print(res40) # >>> 1
res41 = r.lpop("bikes:repairs")
print(res41) # >>> 'bike:3'
res42 = r.lpop("bikes:repairs")
print(res42) # >>> 'bike:2'
res43 = r.lpop("bikes:repairs")
print(res43) # >>> 'bike:1'
res44 = r.exists("bikes:repairs")
print(res44) # >>> False
res45 = r.delete("bikes:repairs")
print(res45) # >>> 0
res46 = r.llen("bikes:repairs")
print(res46) # >>> 0
res47 = r.lpop("bikes:repairs")
print(res47) # >>> None
res48 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res48) # >>> 5
res49 = r.ltrim("bikes:repairs", 0, 2)
print(res49) # >>> True
res50 = r.lrange("bikes:repairs", 0, -1)
print(res50) # >>> ['bike:5', 'bike:4', 'bike:3']
import assert from 'assert';
import { createClient } from 'redis';
const client = await createClient();
await client.connect();
const res1 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res1); // 1
const res2 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res2); // 2
const res3 = await client.rPop('bikes:repairs');
console.log(res3); // bike:1
const res4 = await client.rPop('bikes:repairs');
console.log(res4); // bike:2
const res5 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res5); // 1
const res6 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res6); // 2
const res7 = await client.lPop('bikes:repairs');
console.log(res7); // bike:2
const res8 = await client.lPop('bikes:repairs');
console.log(res8); // bike:1
const res9 = await client.lLen('bikes:repairs');
console.log(res9); // 0
const res10 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res10); // 1
const res11 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res11); // 2
const res12 = await client.lMove('bikes:repairs', 'bikes:finished', 'LEFT', 'LEFT');
console.log(res12); // 'bike:2'
const res13 = await client.lRange('bikes:repairs', 0, -1);
console.log(res13); // ['bike:1']
const res14 = await client.lRange('bikes:finished', 0, -1);
console.log(res14); // ['bike:2']
const res15 = await client.rPush('bikes:repairs', 'bike:1');
console.log(res15); // 1
const res16 = await client.rPush('bikes:repairs', 'bike:2');
console.log(res16); // 2
const res17 = await client.lPush('bikes:repairs', 'bike:important_bike');
console.log(res17); // 3
const res18 = await client.lRange('bikes:repairs', 0, -1);
console.log(res18); // ['bike:important_bike', 'bike:1', 'bike:2']
const res19 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res19); // 3
const res20 = await client.lPush(
'bikes:repairs', ['bike:important_bike', 'bike:very_important_bike']
);
console.log(res20); // 5
const res21 = await client.lRange('bikes:repairs', 0, -1);
console.log(res21); // ['bike:very_important_bike', 'bike:important_bike', 'bike:1', 'bike:2', 'bike:3']
const res22 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res22); // 3
const res23 = await client.rPop('bikes:repairs');
console.log(res23); // 'bike:3'
const res24 = await client.lPop('bikes:repairs');
console.log(res24); // 'bike:1'
const res25 = await client.rPop('bikes:repairs');
console.log(res25); // 'bike:2'
const res26 = await client.rPop('bikes:repairs');
console.log(res26); // None
const res27 = await client.lPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res27); // 5
const res28 = await client.lTrim('bikes:repairs', 0, 2);
console.log(res28); // true
const res29 = await client.lRange('bikes:repairs', 0, -1);
console.log(res29); // ['bike:5', 'bike:4', 'bike:3']
const res27eol = await client.rPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res27eol); // 5
const res28eol = await client.lTrim('bikes:repairs', -3, -1);
console.log(res28eol); // 'OK'
const res29eol = await client.lRange('bikes:repairs', 0, -1);
console.log(res29eol); // ['bike:3', 'bike:4', 'bike:5']
const res31 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2']);
console.log(res31); // 2
const res32 = await client.brPop('bikes:repairs', 1);
console.log(res32); // { key: 'bikes:repairs', element: 'bike:2' }
const res33 = await client.brPop('bikes:repairs', 1);
console.log(res33); // { key: 'bikes:repairs', element: 'bike:1' }
const res34 = await client.brPop('bikes:repairs', 1);
console.log(res34); // null
const res35 = await client.del('new_bikes');
console.log(res35); // 0
const res36 = await client.lPush('new_bikes', ['bike:1', 'bike:2', 'bike:3']);
console.log(res36); // 3
const res37 = await client.set('new_bikes', 'bike:1');
console.log(res37); // 'OK'
const res38 = await client.type('new_bikes');
console.log(res38); // 'string'
try {
const res39 = await client.lPush('new_bikes', 'bike:2', 'bike:3');
// redis.exceptions.ResponseError:
// [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value]
}
catch(e){
console.log(e);
}
await client.lPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res36); // 3
const res40 = await client.exists('bikes:repairs')
console.log(res40); // true
const res41 = await client.lPop('bikes:repairs');
console.log(res41); // 'bike:3'
const res42 = await client.lPop('bikes:repairs');
console.log(res42); // 'bike:2'
const res43 = await client.lPop('bikes:repairs');
console.log(res43); // 'bike:1'
const res44 = await client.exists('bikes:repairs');
console.log(res44); // 0
const res45 = await client.del('bikes:repairs');
console.log(res45); // 0
const res46 = await client.lLen('bikes:repairs');
console.log(res46); // 0
const res47 = await client.lPop('bikes:repairs');
console.log(res47); // null
const res48 = await client.lPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res48); // 5
const res49 = await client.lTrim('bikes:repairs', 0, 2);
console.log(res49); // 'OK'
const res50 = await client.lRange('bikes:repairs', 0, -1);
console.log(res50); // ['bike:5', 'bike:4', 'bike:3']
package io.redis.examples;
import org.junit.Test;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.args.ListDirection;
import java.util.List;
import static org.junit.Assert.*;
public class ListExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
long res1 = jedis.lpush("bikes:repairs", "bike:1");
System.out.println(res1); // >>> 1
long res2 = jedis.lpush("bikes:repairs", "bike:2");
System.out.println(res2); // >>> 2
String res3 = jedis.rpop("bikes:repairs");
System.out.println(res3); // >>> bike:1
String res4 = jedis.rpop("bikes:repairs");
System.out.println(res4); // >>> bike:2
long res5 = jedis.lpush("bikes:repairs", "bike:1");
System.out.println(res5); // >>> 1
long res6 = jedis.lpush("bikes:repairs", "bike:2");
System.out.println(res6); // >>> 2
String res7 = jedis.lpop("bikes:repairs");
System.out.println(res7); // >>> bike:2
String res8 = jedis.lpop("bikes:repairs");
System.out.println(res8); // >>> bike:1
long res9 = jedis.llen("bikes:repairs");
System.out.println(res9); // >>> 0
long res10 = jedis.lpush("bikes:repairs", "bike:1");
System.out.println(res10); // >>> 1
long res11 = jedis.lpush("bikes:repairs", "bike:2");
System.out.println(res11); // >>> 2
String res12 = jedis.lmove("bikes:repairs", "bikes:finished", ListDirection.LEFT, ListDirection.LEFT);
System.out.println(res12); // >>> bike:2
List<String> res13 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res13); // >>> [bike:1]
List<String> res14 = jedis.lrange("bikes:finished", 0, -1);
System.out.println(res14); // >>> [bike:2]
long res15 = jedis.rpush("bikes:repairs", "bike:1");
System.out.println(res15); // >>> 1
long res16 = jedis.rpush("bikes:repairs", "bike:2");
System.out.println(res16); // >>> 2
long res17 = jedis.lpush("bikes:repairs", "bike:important_bike");
System.out.println(res17); // >>> 3
List<String> res18 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res18); // >>> [bike:important_bike, bike:1, bike:2]
long res19 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3");
System.out.println(res19); // >>> 3
long res20 = jedis.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike");
System.out.println(res20); // >>> 5
List<String> res21 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res21); // >>> [bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3]
long res22 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3");
System.out.println(res22); // >>> 3
String res23 = jedis.rpop("bikes:repairs");
System.out.println(res23); // >>> bike:3
String res24 = jedis.lpop("bikes:repairs");
System.out.println(res24); // >>> bike:1
String res25 = jedis.rpop("bikes:repairs");
System.out.println(res25); // >>> bike:2
String res26 = jedis.rpop("bikes:repairs");
System.out.println(res26); // >>> null
long res27 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
System.out.println(res27); // >>> 5
String res28 = jedis.ltrim("bikes:repairs", 0, 2);
System.out.println(res28); // >>> OK
List<String> res29 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res29); // >>> [bike:5, bike:4, bike:3]
res27 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
System.out.println(res27); // >>> 5
res28 = jedis.ltrim("bikes:repairs", -3, -1);
System.out.println(res2); // >>> OK
res29 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res29); // >>> [bike:3, bike:4, bike:5]
long res31 = jedis.rpush("bikes:repairs", "bike:1", "bike:2");
System.out.println(res31); // >>> 2
List<String> res32 = jedis.brpop(1, "bikes:repairs");
System.out.println(res32); // >>> (bikes:repairs, bike:2)
List<String> res33 = jedis.brpop(1,"bikes:repairs");
System.out.println(res33); // >>> (bikes:repairs, bike:1)
List<String> res34 = jedis.brpop(1,"bikes:repairs");
System.out.println(res34); // >>> null
long res35 = jedis.del("new_bikes");
System.out.println(res35); // >>> 0
long res36 = jedis.lpush("new_bikes", "bike:1", "bike:2", "bike:3");
System.out.println(res36); // >>> 3
String res37 = jedis.set("new_bikes", "bike:1");
System.out.println(res37); // >>> OK
String res38 = jedis.type("new_bikes");
System.out.println(res38); // >>> string
try {
long res39 = jedis.lpush("new_bikes", "bike:2", "bike:3");
} catch (Exception e) {
e.printStackTrace();
// >>> redis.clients.jedis.exceptions.JedisDataException:
// >>> WRONGTYPE Operation against a key holding the wrong kind of value
}
jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3");
System.out.println(res36); // >>> 3
boolean res40 = jedis.exists("bikes:repairs");
System.out.println(res40); // >>> true
String res41 = jedis.lpop("bikes:repairs");
System.out.println(res41); // >>> bike:3
String res42 = jedis.lpop("bikes:repairs");
System.out.println(res42); // >>> bike:2
String res43 = jedis.lpop("bikes:repairs");
System.out.println(res43); // >>> bike:1
boolean res44 = jedis.exists("bikes:repairs");
System.out.println(res44); // >>> false
long res45 = jedis.del("bikes:repairs");
System.out.println(res45); // >>> 0
long res46 = jedis.llen("bikes:repairs");
System.out.println(res46); // >>> 0
String res47 = jedis.lpop("bikes:repairs");
System.out.println(res47); // >>> null
long res48 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
System.out.println(res48); // >>> 5
String res49 = jedis.ltrim("bikes:repairs", 0, 2);
System.out.println(res49); // >>> OK
List<String> res50 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res50); // >>> [bike:5, bike:4, bike:3]
}
}
package example_commands_test
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
func ExampleClient_queue() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res1, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res1) // >>> 1
res2, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res2) // >>> 2
res3, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res3) // >>> bike:1
res4, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res4) // >>> bike:2
}
func ExampleClient_stack() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res5, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res5) // >>> 1
res6, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res6) // >>> 2
res7, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res7) // >>> bike:2
res8, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res8) // >>> bike:1
}
func ExampleClient_llen() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res9, err := rdb.LLen(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res9) // >>> 0
}
func ExampleClient_lmove_lrange() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res10, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res10) // >>> 1
res11, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res11) // >>> 2
res12, err := rdb.LMove(ctx, "bikes:repairs", "bikes:finished", "LEFT", "LEFT").Result()
if err != nil {
panic(err)
}
fmt.Println(res12) // >>> bike:2
res13, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res13) // >>> [bike:1]
res14, err := rdb.LRange(ctx, "bikes:finished", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res14) // >>> [bike:2]
}
func ExampleClient_lpush_rpush() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res15, err := rdb.RPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res15) // >>> 1
res16, err := rdb.RPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res16) // >>> 2
res17, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike").Result()
if err != nil {
panic(err)
}
fmt.Println(res17) // >>> 3
res18, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res18) // >>> [bike:important_bike bike:1 bike:2]
}
func ExampleClient_variadic() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res19, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res19) // >>> 3
res20, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike", "bike:very_important_bike").Result()
if err != nil {
panic(err)
}
fmt.Println(res20) // >>> 5
res21, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res21) // >>> [bike:very_important_bike bike:important_bike bike:1 bike:2 bike:3]
}
func ExampleClient_lpop_rpop() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res22, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res22) // >>> 3
res23, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res23) // >>> bike:3
res24, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res24) // >>> bike:1
res25, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res25) // >>> bike:2
res26, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
fmt.Println(err) // >>> redis: nil
}
fmt.Println(res26) // >>> <empty string>
}
func ExampleClient_ltrim() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res27, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
fmt.Println(res27) // >>> 5
res28, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result()
if err != nil {
panic(err)
}
fmt.Println(res28) // >>> OK
res29, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res29) // >>> [bike:5 bike:4 bike:3]
}
func ExampleClient_ltrim_end_of_list() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res30, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
fmt.Println(res30) // >>> 5
res31, err := rdb.LTrim(ctx, "bikes:repairs", -3, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res31) // >>> OK
res32, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res32) // >>> [bike:3 bike:4 bike:5]
}
func ExampleClient_brpop() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res33, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res33) // >>> 2
res34, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res34) // >>> [bikes:repairs bike:2]
res35, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res35) // >>> [bikes:repairs bike:1]
res36, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
if err != nil {
fmt.Println(err) // >>> redis: nil
}
fmt.Println(res36) // >>> []
}
func ExampleClient_rule1() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res37, err := rdb.Del(ctx, "new_bikes").Result()
if err != nil {
panic(err)
}
fmt.Println(res37) // >>> 0
res38, err := rdb.LPush(ctx, "new_bikes", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res38) // >>> 3
}
func ExampleClient_rule11() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res39, err := rdb.Set(ctx, "new_bikes", "bike:1", 0).Result()
if err != nil {
panic(err)
}
fmt.Println(res39) // >>> OK
res40, err := rdb.Type(ctx, "new_bikes").Result()
if err != nil {
panic(err)
}
fmt.Println(res40) // >>> string
res41, err := rdb.LPush(ctx, "new_bikes", "bike:2", "bike:3").Result()
if err != nil {
fmt.Println(err)
// >>> WRONGTYPE Operation against a key holding the wrong kind of value
}
fmt.Println(res41)
}
func ExampleClient_rule2() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res42, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res42) // >>> 3
res43, err := rdb.Exists(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res43) // >>> 1
res44, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res44) // >>> bike:3
res45, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res45) // >>> bike:2
res46, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res46) // >>> bike:1
res47, err := rdb.Exists(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res47) // >>> 0
}
func ExampleClient_rule3() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res48, err := rdb.Del(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res48) // >>> 0
res49, err := rdb.LLen(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res49) // >>> 0
res50, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
fmt.Println(err) // >>> redis: nil
}
fmt.Println(res50) // >>> <empty string>
}
func ExampleClient_ltrim1() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res51, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
fmt.Println(res51) // >>> 5
res52, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result()
if err != nil {
panic(err)
}
fmt.Println(res52) // >>> OK
res53, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res53) // >>> [bike:5 bike:4 bike:3]
}
public class ListExample
{
public void run()
{
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
long res1 = db.ListLeftPush("bikes:repairs", "bike:1");
Console.WriteLine(res1); // >>> 1
long res2 = db.ListLeftPush("bikes:repairs", "bike:2");
Console.WriteLine(res2); // >>> 2
RedisValue res3 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res3); // >>> "bike:1"
RedisValue res4 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res4); // >>> "bike:2"
long res5 = db.ListLeftPush("bikes:repairs", "bike:1");
Console.WriteLine(res5); // >>> 1
long res6 = db.ListLeftPush("bikes:repairs", "bike:2");
Console.WriteLine(res6); // >>> 2
RedisValue res7 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res7); // >>> "bike:2"
RedisValue res8 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res8); // >>> "bike:1"
long res9 = db.ListLength("bikes:repairs");
Console.WriteLine(res9); // >>> 0
long res10 = db.ListLeftPush("{bikes}:repairs", "bike:1");
Console.WriteLine(res10); // >>> 1
long res11 = db.ListLeftPush("{bikes}:repairs", "bike:2");
Console.WriteLine(res11); // >>> 2
RedisValue res12 = db.ListMove("{bikes}:repairs", "{bikes}:finished", ListSide.Left, ListSide.Left);
Console.Write(res12); // >>> "bike:2"
RedisValue[] res13 = db.ListRange("{bikes}:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res13)); // >>> "bike:1"
RedisValue[] res14 = db.ListRange("{bikes}:finished", 0, -1);
Console.WriteLine(string.Join(", ", res14)); // >>> "bike:2"
long res15 = db.ListRightPush("bikes:repairs", "bike:1");
Console.WriteLine(res15); // >>> 1
long res16 = db.ListRightPush("bikes:repairs", "bike:2");
Console.WriteLine(res16); // >>> 2
long res17 = db.ListLeftPush("bikes:repairs", "bike:important_bike");
Console.WriteLine(res17); // >>> 3
RedisValue[] res18 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res18)); // >>> "bike:important_bike, bike:1, bike:2"
long res19 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res19); // >>> 3
long res20 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:important_bike", "bike:very_important_bike" });
Console.WriteLine(res20); // >>> 5
RedisValue[] res21 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res21));
// >>> "bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3"
long res22 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res22); // >>> 3
RedisValue res23 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res23); // >>> "bike:3"
RedisValue res24 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res24); // >>> "bike:1"
RedisValue res25 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res25); // >>> "bike:2"
RedisValue res26 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res26); // >>> <Empty string>
long res27 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
Console.WriteLine(res27); // >>> 5
db.ListTrim("bikes:repairs", 0, 2);
RedisValue[] res28 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res28)); // "bike:5, bike:4, bike:3"
long res29 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
Console.WriteLine(res29); // >>> 5
db.ListTrim("bikes:repairs", -3, -1);
RedisValue[] res30 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res30)); // >>> "bike:3, bike:4, bike:5"
long res31 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2" });
Console.WriteLine(res31); // >>> 2
Tuple<RedisKey, RedisValue>? res32 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1);
if (res32 != null)
Console.WriteLine($"{res32.Item1} -> {res32.Item2}"); // >>> "bikes:repairs -> bike:2"
Tuple<RedisKey, RedisValue>? res33 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1);
if (res33 != null)
Console.WriteLine($"{res33.Item1} -> {res33.Item2}"); // >>> "bikes:repairs -> bike:1"
Tuple<RedisKey, RedisValue>? res34 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1);
Console.WriteLine(res34); // >>> "Null"
bool res35 = db.KeyDelete("new_bikes");
Console.WriteLine(res35); // >>> False
long res36 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res36); // >>> 3
bool res37 = db.StringSet("new_bikes", "bike:1");
Console.WriteLine(res37); // >>> True
RedisType res38 = db.KeyType("new_bikes");
Console.WriteLine(res38); // >>> RedisType.String
try
{
long res39 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:2", "bike:3" });
}
catch (Exception e)
{
Console.WriteLine(e);
}
long res40 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res40); // >>> 3
bool res41 = db.KeyExists("bikes:repairs");
Console.WriteLine(res41); // >>> True
RedisValue res42 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res42); // >>> "bike:3"
RedisValue res43 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res43); // >>> "bike:2"
RedisValue res44 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res44); // >>> "bike:1"
bool res45 = db.KeyExists("bikes:repairs");
Console.WriteLine(res45); // >>> False
bool res46 = db.KeyDelete("bikes:repairs");
Console.WriteLine(res46); // >>> False
long res47 = db.ListLength("bikes:repairs");
Console.WriteLine(res47); // >>> 0
RedisValue res48 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res48); // >>> Null
long res49 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
Console.WriteLine(res49); // >>> 5
db.ListTrim("bikes:repairs", 0, 2);
RedisValue[] res50 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res50)); // >>> "bike:5, bike:4, bike:3"
}
}
Example of rule 2:
> RPUSH bikes:repairs bike:1 bike:2 bike:3
(integer) 3
> EXISTS bikes:repairs
(integer) 1
> LPOP bikes:repairs
"bike:3"
> LPOP bikes:repairs
"bike:2"
> LPOP bikes:repairs
"bike:1"
> EXISTS bikes:repairs
(integer) 0
"""
Code samples for List doc pages:
https://redis.io/docs/latest/develop/data-types/lists/
"""
import redis
r = redis.Redis(decode_responses=True)
res1 = r.lpush("bikes:repairs", "bike:1")
print(res1) # >>> 1
res2 = r.lpush("bikes:repairs", "bike:2")
print(res2) # >>> 2
res3 = r.rpop("bikes:repairs")
print(res3) # >>> bike:1
res4 = r.rpop("bikes:repairs")
print(res4) # >>> bike:2
res5 = r.lpush("bikes:repairs", "bike:1")
print(res5) # >>> 1
res6 = r.lpush("bikes:repairs", "bike:2")
print(res6) # >>> 2
res7 = r.lpop("bikes:repairs")
print(res7) # >>> bike:2
res8 = r.lpop("bikes:repairs")
print(res8) # >>> bike:1
res9 = r.llen("bikes:repairs")
print(res9) # >>> 0
res10 = r.lpush("bikes:repairs", "bike:1")
print(res10) # >>> 1
res11 = r.lpush("bikes:repairs", "bike:2")
print(res11) # >>> 2
res12 = r.lmove("bikes:repairs", "bikes:finished", "LEFT", "LEFT")
print(res12) # >>> 'bike:2'
res13 = r.lrange("bikes:repairs", 0, -1)
print(res13) # >>> ['bike:1']
res14 = r.lrange("bikes:finished", 0, -1)
print(res14) # >>> ['bike:2']
res15 = r.rpush("bikes:repairs", "bike:1")
print(res15) # >>> 1
res16 = r.rpush("bikes:repairs", "bike:2")
print(res16) # >>> 2
res17 = r.lpush("bikes:repairs", "bike:important_bike")
print(res17) # >>> 3
res18 = r.lrange("bikes:repairs", 0, -1)
print(res18) # >>> ['bike:important_bike', 'bike:1', 'bike:2']
res19 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res19) # >>> 3
res20 = r.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike")
print(res20) # >>> 5
res21 = r.lrange("bikes:repairs", 0, -1)
print(
res21
) # >>> ['bike:very_important_bike', 'bike:important_bike', 'bike:1', ...
res22 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res22) # >>> 3
res23 = r.rpop("bikes:repairs")
print(res23) # >>> 'bike:3'
res24 = r.lpop("bikes:repairs")
print(res24) # >>> 'bike:1'
res25 = r.rpop("bikes:repairs")
print(res25) # >>> 'bike:2'
res26 = r.rpop("bikes:repairs")
print(res26) # >>> None
res27 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res27) # >>> 5
res28 = r.ltrim("bikes:repairs", 0, 2)
print(res28) # >>> True
res29 = r.lrange("bikes:repairs", 0, -1)
print(res29) # >>> ['bike:5', 'bike:4', 'bike:3']
res27 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res27) # >>> 5
res28 = r.ltrim("bikes:repairs", -3, -1)
print(res28) # >>> True
res29 = r.lrange("bikes:repairs", 0, -1)
print(res29) # >>> ['bike:3', 'bike:4', 'bike:5']
res31 = r.rpush("bikes:repairs", "bike:1", "bike:2")
print(res31) # >>> 2
res32 = r.brpop("bikes:repairs", timeout=1)
print(res32) # >>> ('bikes:repairs', 'bike:2')
res33 = r.brpop("bikes:repairs", timeout=1)
print(res33) # >>> ('bikes:repairs', 'bike:1')
res34 = r.brpop("bikes:repairs", timeout=1)
print(res34) # >>> None
res35 = r.delete("new_bikes")
print(res35) # >>> 0
res36 = r.lpush("new_bikes", "bike:1", "bike:2", "bike:3")
print(res36) # >>> 3
res37 = r.set("new_bikes", "bike:1")
print(res37) # >>> True
res38 = r.type("new_bikes")
print(res38) # >>> 'string'
try:
res39 = r.lpush("new_bikes", "bike:2", "bike:3")
# >>> redis.exceptions.ResponseError:
# >>> WRONGTYPE Operation against a key holding the wrong kind of value
except redis.exceptions.ResponseError as e:
print(e)
r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res36) # >>> 3
res40 = r.exists("bikes:repairs")
print(res40) # >>> 1
res41 = r.lpop("bikes:repairs")
print(res41) # >>> 'bike:3'
res42 = r.lpop("bikes:repairs")
print(res42) # >>> 'bike:2'
res43 = r.lpop("bikes:repairs")
print(res43) # >>> 'bike:1'
res44 = r.exists("bikes:repairs")
print(res44) # >>> False
res45 = r.delete("bikes:repairs")
print(res45) # >>> 0
res46 = r.llen("bikes:repairs")
print(res46) # >>> 0
res47 = r.lpop("bikes:repairs")
print(res47) # >>> None
res48 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res48) # >>> 5
res49 = r.ltrim("bikes:repairs", 0, 2)
print(res49) # >>> True
res50 = r.lrange("bikes:repairs", 0, -1)
print(res50) # >>> ['bike:5', 'bike:4', 'bike:3']
import assert from 'assert';
import { createClient } from 'redis';
const client = await createClient();
await client.connect();
const res1 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res1); // 1
const res2 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res2); // 2
const res3 = await client.rPop('bikes:repairs');
console.log(res3); // bike:1
const res4 = await client.rPop('bikes:repairs');
console.log(res4); // bike:2
const res5 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res5); // 1
const res6 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res6); // 2
const res7 = await client.lPop('bikes:repairs');
console.log(res7); // bike:2
const res8 = await client.lPop('bikes:repairs');
console.log(res8); // bike:1
const res9 = await client.lLen('bikes:repairs');
console.log(res9); // 0
const res10 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res10); // 1
const res11 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res11); // 2
const res12 = await client.lMove('bikes:repairs', 'bikes:finished', 'LEFT', 'LEFT');
console.log(res12); // 'bike:2'
const res13 = await client.lRange('bikes:repairs', 0, -1);
console.log(res13); // ['bike:1']
const res14 = await client.lRange('bikes:finished', 0, -1);
console.log(res14); // ['bike:2']
const res15 = await client.rPush('bikes:repairs', 'bike:1');
console.log(res15); // 1
const res16 = await client.rPush('bikes:repairs', 'bike:2');
console.log(res16); // 2
const res17 = await client.lPush('bikes:repairs', 'bike:important_bike');
console.log(res17); // 3
const res18 = await client.lRange('bikes:repairs', 0, -1);
console.log(res18); // ['bike:important_bike', 'bike:1', 'bike:2']
const res19 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res19); // 3
const res20 = await client.lPush(
'bikes:repairs', ['bike:important_bike', 'bike:very_important_bike']
);
console.log(res20); // 5
const res21 = await client.lRange('bikes:repairs', 0, -1);
console.log(res21); // ['bike:very_important_bike', 'bike:important_bike', 'bike:1', 'bike:2', 'bike:3']
const res22 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res22); // 3
const res23 = await client.rPop('bikes:repairs');
console.log(res23); // 'bike:3'
const res24 = await client.lPop('bikes:repairs');
console.log(res24); // 'bike:1'
const res25 = await client.rPop('bikes:repairs');
console.log(res25); // 'bike:2'
const res26 = await client.rPop('bikes:repairs');
console.log(res26); // None
const res27 = await client.lPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res27); // 5
const res28 = await client.lTrim('bikes:repairs', 0, 2);
console.log(res28); // true
const res29 = await client.lRange('bikes:repairs', 0, -1);
console.log(res29); // ['bike:5', 'bike:4', 'bike:3']
const res27eol = await client.rPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res27eol); // 5
const res28eol = await client.lTrim('bikes:repairs', -3, -1);
console.log(res28eol); // 'OK'
const res29eol = await client.lRange('bikes:repairs', 0, -1);
console.log(res29eol); // ['bike:3', 'bike:4', 'bike:5']
const res31 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2']);
console.log(res31); // 2
const res32 = await client.brPop('bikes:repairs', 1);
console.log(res32); // { key: 'bikes:repairs', element: 'bike:2' }
const res33 = await client.brPop('bikes:repairs', 1);
console.log(res33); // { key: 'bikes:repairs', element: 'bike:1' }
const res34 = await client.brPop('bikes:repairs', 1);
console.log(res34); // null
const res35 = await client.del('new_bikes');
console.log(res35); // 0
const res36 = await client.lPush('new_bikes', ['bike:1', 'bike:2', 'bike:3']);
console.log(res36); // 3
const res37 = await client.set('new_bikes', 'bike:1');
console.log(res37); // 'OK'
const res38 = await client.type('new_bikes');
console.log(res38); // 'string'
try {
const res39 = await client.lPush('new_bikes', 'bike:2', 'bike:3');
// redis.exceptions.ResponseError:
// [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value]
}
catch(e){
console.log(e);
}
await client.lPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res36); // 3
const res40 = await client.exists('bikes:repairs')
console.log(res40); // true
const res41 = await client.lPop('bikes:repairs');
console.log(res41); // 'bike:3'
const res42 = await client.lPop('bikes:repairs');
console.log(res42); // 'bike:2'
const res43 = await client.lPop('bikes:repairs');
console.log(res43); // 'bike:1'
const res44 = await client.exists('bikes:repairs');
console.log(res44); // 0
const res45 = await client.del('bikes:repairs');
console.log(res45); // 0
const res46 = await client.lLen('bikes:repairs');
console.log(res46); // 0
const res47 = await client.lPop('bikes:repairs');
console.log(res47); // null
const res48 = await client.lPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res48); // 5
const res49 = await client.lTrim('bikes:repairs', 0, 2);
console.log(res49); // 'OK'
const res50 = await client.lRange('bikes:repairs', 0, -1);
console.log(res50); // ['bike:5', 'bike:4', 'bike:3']
package io.redis.examples;
import org.junit.Test;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.args.ListDirection;
import java.util.List;
import static org.junit.Assert.*;
public class ListExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
long res1 = jedis.lpush("bikes:repairs", "bike:1");
System.out.println(res1); // >>> 1
long res2 = jedis.lpush("bikes:repairs", "bike:2");
System.out.println(res2); // >>> 2
String res3 = jedis.rpop("bikes:repairs");
System.out.println(res3); // >>> bike:1
String res4 = jedis.rpop("bikes:repairs");
System.out.println(res4); // >>> bike:2
long res5 = jedis.lpush("bikes:repairs", "bike:1");
System.out.println(res5); // >>> 1
long res6 = jedis.lpush("bikes:repairs", "bike:2");
System.out.println(res6); // >>> 2
String res7 = jedis.lpop("bikes:repairs");
System.out.println(res7); // >>> bike:2
String res8 = jedis.lpop("bikes:repairs");
System.out.println(res8); // >>> bike:1
long res9 = jedis.llen("bikes:repairs");
System.out.println(res9); // >>> 0
long res10 = jedis.lpush("bikes:repairs", "bike:1");
System.out.println(res10); // >>> 1
long res11 = jedis.lpush("bikes:repairs", "bike:2");
System.out.println(res11); // >>> 2
String res12 = jedis.lmove("bikes:repairs", "bikes:finished", ListDirection.LEFT, ListDirection.LEFT);
System.out.println(res12); // >>> bike:2
List<String> res13 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res13); // >>> [bike:1]
List<String> res14 = jedis.lrange("bikes:finished", 0, -1);
System.out.println(res14); // >>> [bike:2]
long res15 = jedis.rpush("bikes:repairs", "bike:1");
System.out.println(res15); // >>> 1
long res16 = jedis.rpush("bikes:repairs", "bike:2");
System.out.println(res16); // >>> 2
long res17 = jedis.lpush("bikes:repairs", "bike:important_bike");
System.out.println(res17); // >>> 3
List<String> res18 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res18); // >>> [bike:important_bike, bike:1, bike:2]
long res19 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3");
System.out.println(res19); // >>> 3
long res20 = jedis.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike");
System.out.println(res20); // >>> 5
List<String> res21 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res21); // >>> [bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3]
long res22 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3");
System.out.println(res22); // >>> 3
String res23 = jedis.rpop("bikes:repairs");
System.out.println(res23); // >>> bike:3
String res24 = jedis.lpop("bikes:repairs");
System.out.println(res24); // >>> bike:1
String res25 = jedis.rpop("bikes:repairs");
System.out.println(res25); // >>> bike:2
String res26 = jedis.rpop("bikes:repairs");
System.out.println(res26); // >>> null
long res27 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
System.out.println(res27); // >>> 5
String res28 = jedis.ltrim("bikes:repairs", 0, 2);
System.out.println(res28); // >>> OK
List<String> res29 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res29); // >>> [bike:5, bike:4, bike:3]
res27 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
System.out.println(res27); // >>> 5
res28 = jedis.ltrim("bikes:repairs", -3, -1);
System.out.println(res2); // >>> OK
res29 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res29); // >>> [bike:3, bike:4, bike:5]
long res31 = jedis.rpush("bikes:repairs", "bike:1", "bike:2");
System.out.println(res31); // >>> 2
List<String> res32 = jedis.brpop(1, "bikes:repairs");
System.out.println(res32); // >>> (bikes:repairs, bike:2)
List<String> res33 = jedis.brpop(1,"bikes:repairs");
System.out.println(res33); // >>> (bikes:repairs, bike:1)
List<String> res34 = jedis.brpop(1,"bikes:repairs");
System.out.println(res34); // >>> null
long res35 = jedis.del("new_bikes");
System.out.println(res35); // >>> 0
long res36 = jedis.lpush("new_bikes", "bike:1", "bike:2", "bike:3");
System.out.println(res36); // >>> 3
String res37 = jedis.set("new_bikes", "bike:1");
System.out.println(res37); // >>> OK
String res38 = jedis.type("new_bikes");
System.out.println(res38); // >>> string
try {
long res39 = jedis.lpush("new_bikes", "bike:2", "bike:3");
} catch (Exception e) {
e.printStackTrace();
// >>> redis.clients.jedis.exceptions.JedisDataException:
// >>> WRONGTYPE Operation against a key holding the wrong kind of value
}
jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3");
System.out.println(res36); // >>> 3
boolean res40 = jedis.exists("bikes:repairs");
System.out.println(res40); // >>> true
String res41 = jedis.lpop("bikes:repairs");
System.out.println(res41); // >>> bike:3
String res42 = jedis.lpop("bikes:repairs");
System.out.println(res42); // >>> bike:2
String res43 = jedis.lpop("bikes:repairs");
System.out.println(res43); // >>> bike:1
boolean res44 = jedis.exists("bikes:repairs");
System.out.println(res44); // >>> false
long res45 = jedis.del("bikes:repairs");
System.out.println(res45); // >>> 0
long res46 = jedis.llen("bikes:repairs");
System.out.println(res46); // >>> 0
String res47 = jedis.lpop("bikes:repairs");
System.out.println(res47); // >>> null
long res48 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
System.out.println(res48); // >>> 5
String res49 = jedis.ltrim("bikes:repairs", 0, 2);
System.out.println(res49); // >>> OK
List<String> res50 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res50); // >>> [bike:5, bike:4, bike:3]
}
}
package example_commands_test
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
func ExampleClient_queue() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res1, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res1) // >>> 1
res2, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res2) // >>> 2
res3, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res3) // >>> bike:1
res4, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res4) // >>> bike:2
}
func ExampleClient_stack() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res5, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res5) // >>> 1
res6, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res6) // >>> 2
res7, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res7) // >>> bike:2
res8, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res8) // >>> bike:1
}
func ExampleClient_llen() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res9, err := rdb.LLen(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res9) // >>> 0
}
func ExampleClient_lmove_lrange() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res10, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res10) // >>> 1
res11, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res11) // >>> 2
res12, err := rdb.LMove(ctx, "bikes:repairs", "bikes:finished", "LEFT", "LEFT").Result()
if err != nil {
panic(err)
}
fmt.Println(res12) // >>> bike:2
res13, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res13) // >>> [bike:1]
res14, err := rdb.LRange(ctx, "bikes:finished", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res14) // >>> [bike:2]
}
func ExampleClient_lpush_rpush() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res15, err := rdb.RPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res15) // >>> 1
res16, err := rdb.RPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res16) // >>> 2
res17, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike").Result()
if err != nil {
panic(err)
}
fmt.Println(res17) // >>> 3
res18, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res18) // >>> [bike:important_bike bike:1 bike:2]
}
func ExampleClient_variadic() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res19, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res19) // >>> 3
res20, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike", "bike:very_important_bike").Result()
if err != nil {
panic(err)
}
fmt.Println(res20) // >>> 5
res21, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res21) // >>> [bike:very_important_bike bike:important_bike bike:1 bike:2 bike:3]
}
func ExampleClient_lpop_rpop() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res22, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res22) // >>> 3
res23, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res23) // >>> bike:3
res24, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res24) // >>> bike:1
res25, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res25) // >>> bike:2
res26, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
fmt.Println(err) // >>> redis: nil
}
fmt.Println(res26) // >>> <empty string>
}
func ExampleClient_ltrim() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res27, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
fmt.Println(res27) // >>> 5
res28, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result()
if err != nil {
panic(err)
}
fmt.Println(res28) // >>> OK
res29, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res29) // >>> [bike:5 bike:4 bike:3]
}
func ExampleClient_ltrim_end_of_list() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res30, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
fmt.Println(res30) // >>> 5
res31, err := rdb.LTrim(ctx, "bikes:repairs", -3, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res31) // >>> OK
res32, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res32) // >>> [bike:3 bike:4 bike:5]
}
func ExampleClient_brpop() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res33, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res33) // >>> 2
res34, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res34) // >>> [bikes:repairs bike:2]
res35, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res35) // >>> [bikes:repairs bike:1]
res36, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
if err != nil {
fmt.Println(err) // >>> redis: nil
}
fmt.Println(res36) // >>> []
}
func ExampleClient_rule1() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res37, err := rdb.Del(ctx, "new_bikes").Result()
if err != nil {
panic(err)
}
fmt.Println(res37) // >>> 0
res38, err := rdb.LPush(ctx, "new_bikes", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res38) // >>> 3
}
func ExampleClient_rule11() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res39, err := rdb.Set(ctx, "new_bikes", "bike:1", 0).Result()
if err != nil {
panic(err)
}
fmt.Println(res39) // >>> OK
res40, err := rdb.Type(ctx, "new_bikes").Result()
if err != nil {
panic(err)
}
fmt.Println(res40) // >>> string
res41, err := rdb.LPush(ctx, "new_bikes", "bike:2", "bike:3").Result()
if err != nil {
fmt.Println(err)
// >>> WRONGTYPE Operation against a key holding the wrong kind of value
}
fmt.Println(res41)
}
func ExampleClient_rule2() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res42, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res42) // >>> 3
res43, err := rdb.Exists(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res43) // >>> 1
res44, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res44) // >>> bike:3
res45, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res45) // >>> bike:2
res46, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res46) // >>> bike:1
res47, err := rdb.Exists(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res47) // >>> 0
}
func ExampleClient_rule3() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res48, err := rdb.Del(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res48) // >>> 0
res49, err := rdb.LLen(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res49) // >>> 0
res50, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
fmt.Println(err) // >>> redis: nil
}
fmt.Println(res50) // >>> <empty string>
}
func ExampleClient_ltrim1() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res51, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
fmt.Println(res51) // >>> 5
res52, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result()
if err != nil {
panic(err)
}
fmt.Println(res52) // >>> OK
res53, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res53) // >>> [bike:5 bike:4 bike:3]
}
public class ListExample
{
public void run()
{
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
long res1 = db.ListLeftPush("bikes:repairs", "bike:1");
Console.WriteLine(res1); // >>> 1
long res2 = db.ListLeftPush("bikes:repairs", "bike:2");
Console.WriteLine(res2); // >>> 2
RedisValue res3 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res3); // >>> "bike:1"
RedisValue res4 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res4); // >>> "bike:2"
long res5 = db.ListLeftPush("bikes:repairs", "bike:1");
Console.WriteLine(res5); // >>> 1
long res6 = db.ListLeftPush("bikes:repairs", "bike:2");
Console.WriteLine(res6); // >>> 2
RedisValue res7 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res7); // >>> "bike:2"
RedisValue res8 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res8); // >>> "bike:1"
long res9 = db.ListLength("bikes:repairs");
Console.WriteLine(res9); // >>> 0
long res10 = db.ListLeftPush("{bikes}:repairs", "bike:1");
Console.WriteLine(res10); // >>> 1
long res11 = db.ListLeftPush("{bikes}:repairs", "bike:2");
Console.WriteLine(res11); // >>> 2
RedisValue res12 = db.ListMove("{bikes}:repairs", "{bikes}:finished", ListSide.Left, ListSide.Left);
Console.Write(res12); // >>> "bike:2"
RedisValue[] res13 = db.ListRange("{bikes}:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res13)); // >>> "bike:1"
RedisValue[] res14 = db.ListRange("{bikes}:finished", 0, -1);
Console.WriteLine(string.Join(", ", res14)); // >>> "bike:2"
long res15 = db.ListRightPush("bikes:repairs", "bike:1");
Console.WriteLine(res15); // >>> 1
long res16 = db.ListRightPush("bikes:repairs", "bike:2");
Console.WriteLine(res16); // >>> 2
long res17 = db.ListLeftPush("bikes:repairs", "bike:important_bike");
Console.WriteLine(res17); // >>> 3
RedisValue[] res18 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res18)); // >>> "bike:important_bike, bike:1, bike:2"
long res19 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res19); // >>> 3
long res20 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:important_bike", "bike:very_important_bike" });
Console.WriteLine(res20); // >>> 5
RedisValue[] res21 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res21));
// >>> "bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3"
long res22 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res22); // >>> 3
RedisValue res23 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res23); // >>> "bike:3"
RedisValue res24 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res24); // >>> "bike:1"
RedisValue res25 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res25); // >>> "bike:2"
RedisValue res26 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res26); // >>> <Empty string>
long res27 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
Console.WriteLine(res27); // >>> 5
db.ListTrim("bikes:repairs", 0, 2);
RedisValue[] res28 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res28)); // "bike:5, bike:4, bike:3"
long res29 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
Console.WriteLine(res29); // >>> 5
db.ListTrim("bikes:repairs", -3, -1);
RedisValue[] res30 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res30)); // >>> "bike:3, bike:4, bike:5"
long res31 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2" });
Console.WriteLine(res31); // >>> 2
Tuple<RedisKey, RedisValue>? res32 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1);
if (res32 != null)
Console.WriteLine($"{res32.Item1} -> {res32.Item2}"); // >>> "bikes:repairs -> bike:2"
Tuple<RedisKey, RedisValue>? res33 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1);
if (res33 != null)
Console.WriteLine($"{res33.Item1} -> {res33.Item2}"); // >>> "bikes:repairs -> bike:1"
Tuple<RedisKey, RedisValue>? res34 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1);
Console.WriteLine(res34); // >>> "Null"
bool res35 = db.KeyDelete("new_bikes");
Console.WriteLine(res35); // >>> False
long res36 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res36); // >>> 3
bool res37 = db.StringSet("new_bikes", "bike:1");
Console.WriteLine(res37); // >>> True
RedisType res38 = db.KeyType("new_bikes");
Console.WriteLine(res38); // >>> RedisType.String
try
{
long res39 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:2", "bike:3" });
}
catch (Exception e)
{
Console.WriteLine(e);
}
long res40 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res40); // >>> 3
bool res41 = db.KeyExists("bikes:repairs");
Console.WriteLine(res41); // >>> True
RedisValue res42 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res42); // >>> "bike:3"
RedisValue res43 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res43); // >>> "bike:2"
RedisValue res44 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res44); // >>> "bike:1"
bool res45 = db.KeyExists("bikes:repairs");
Console.WriteLine(res45); // >>> False
bool res46 = db.KeyDelete("bikes:repairs");
Console.WriteLine(res46); // >>> False
long res47 = db.ListLength("bikes:repairs");
Console.WriteLine(res47); // >>> 0
RedisValue res48 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res48); // >>> Null
long res49 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
Console.WriteLine(res49); // >>> 5
db.ListTrim("bikes:repairs", 0, 2);
RedisValue[] res50 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res50)); // >>> "bike:5, bike:4, bike:3"
}
}
The key no longer exists after all the elements are popped.
Example of rule 3:
> DEL bikes:repairs
(integer) 0
> LLEN bikes:repairs
(integer) 0
> LPOP bikes:repairs
(nil)
"""
Code samples for List doc pages:
https://redis.io/docs/latest/develop/data-types/lists/
"""
import redis
r = redis.Redis(decode_responses=True)
res1 = r.lpush("bikes:repairs", "bike:1")
print(res1) # >>> 1
res2 = r.lpush("bikes:repairs", "bike:2")
print(res2) # >>> 2
res3 = r.rpop("bikes:repairs")
print(res3) # >>> bike:1
res4 = r.rpop("bikes:repairs")
print(res4) # >>> bike:2
res5 = r.lpush("bikes:repairs", "bike:1")
print(res5) # >>> 1
res6 = r.lpush("bikes:repairs", "bike:2")
print(res6) # >>> 2
res7 = r.lpop("bikes:repairs")
print(res7) # >>> bike:2
res8 = r.lpop("bikes:repairs")
print(res8) # >>> bike:1
res9 = r.llen("bikes:repairs")
print(res9) # >>> 0
res10 = r.lpush("bikes:repairs", "bike:1")
print(res10) # >>> 1
res11 = r.lpush("bikes:repairs", "bike:2")
print(res11) # >>> 2
res12 = r.lmove("bikes:repairs", "bikes:finished", "LEFT", "LEFT")
print(res12) # >>> 'bike:2'
res13 = r.lrange("bikes:repairs", 0, -1)
print(res13) # >>> ['bike:1']
res14 = r.lrange("bikes:finished", 0, -1)
print(res14) # >>> ['bike:2']
res15 = r.rpush("bikes:repairs", "bike:1")
print(res15) # >>> 1
res16 = r.rpush("bikes:repairs", "bike:2")
print(res16) # >>> 2
res17 = r.lpush("bikes:repairs", "bike:important_bike")
print(res17) # >>> 3
res18 = r.lrange("bikes:repairs", 0, -1)
print(res18) # >>> ['bike:important_bike', 'bike:1', 'bike:2']
res19 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res19) # >>> 3
res20 = r.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike")
print(res20) # >>> 5
res21 = r.lrange("bikes:repairs", 0, -1)
print(
res21
) # >>> ['bike:very_important_bike', 'bike:important_bike', 'bike:1', ...
res22 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res22) # >>> 3
res23 = r.rpop("bikes:repairs")
print(res23) # >>> 'bike:3'
res24 = r.lpop("bikes:repairs")
print(res24) # >>> 'bike:1'
res25 = r.rpop("bikes:repairs")
print(res25) # >>> 'bike:2'
res26 = r.rpop("bikes:repairs")
print(res26) # >>> None
res27 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res27) # >>> 5
res28 = r.ltrim("bikes:repairs", 0, 2)
print(res28) # >>> True
res29 = r.lrange("bikes:repairs", 0, -1)
print(res29) # >>> ['bike:5', 'bike:4', 'bike:3']
res27 = r.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res27) # >>> 5
res28 = r.ltrim("bikes:repairs", -3, -1)
print(res28) # >>> True
res29 = r.lrange("bikes:repairs", 0, -1)
print(res29) # >>> ['bike:3', 'bike:4', 'bike:5']
res31 = r.rpush("bikes:repairs", "bike:1", "bike:2")
print(res31) # >>> 2
res32 = r.brpop("bikes:repairs", timeout=1)
print(res32) # >>> ('bikes:repairs', 'bike:2')
res33 = r.brpop("bikes:repairs", timeout=1)
print(res33) # >>> ('bikes:repairs', 'bike:1')
res34 = r.brpop("bikes:repairs", timeout=1)
print(res34) # >>> None
res35 = r.delete("new_bikes")
print(res35) # >>> 0
res36 = r.lpush("new_bikes", "bike:1", "bike:2", "bike:3")
print(res36) # >>> 3
res37 = r.set("new_bikes", "bike:1")
print(res37) # >>> True
res38 = r.type("new_bikes")
print(res38) # >>> 'string'
try:
res39 = r.lpush("new_bikes", "bike:2", "bike:3")
# >>> redis.exceptions.ResponseError:
# >>> WRONGTYPE Operation against a key holding the wrong kind of value
except redis.exceptions.ResponseError as e:
print(e)
r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3")
print(res36) # >>> 3
res40 = r.exists("bikes:repairs")
print(res40) # >>> 1
res41 = r.lpop("bikes:repairs")
print(res41) # >>> 'bike:3'
res42 = r.lpop("bikes:repairs")
print(res42) # >>> 'bike:2'
res43 = r.lpop("bikes:repairs")
print(res43) # >>> 'bike:1'
res44 = r.exists("bikes:repairs")
print(res44) # >>> False
res45 = r.delete("bikes:repairs")
print(res45) # >>> 0
res46 = r.llen("bikes:repairs")
print(res46) # >>> 0
res47 = r.lpop("bikes:repairs")
print(res47) # >>> None
res48 = r.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5")
print(res48) # >>> 5
res49 = r.ltrim("bikes:repairs", 0, 2)
print(res49) # >>> True
res50 = r.lrange("bikes:repairs", 0, -1)
print(res50) # >>> ['bike:5', 'bike:4', 'bike:3']
import assert from 'assert';
import { createClient } from 'redis';
const client = await createClient();
await client.connect();
const res1 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res1); // 1
const res2 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res2); // 2
const res3 = await client.rPop('bikes:repairs');
console.log(res3); // bike:1
const res4 = await client.rPop('bikes:repairs');
console.log(res4); // bike:2
const res5 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res5); // 1
const res6 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res6); // 2
const res7 = await client.lPop('bikes:repairs');
console.log(res7); // bike:2
const res8 = await client.lPop('bikes:repairs');
console.log(res8); // bike:1
const res9 = await client.lLen('bikes:repairs');
console.log(res9); // 0
const res10 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res10); // 1
const res11 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res11); // 2
const res12 = await client.lMove('bikes:repairs', 'bikes:finished', 'LEFT', 'LEFT');
console.log(res12); // 'bike:2'
const res13 = await client.lRange('bikes:repairs', 0, -1);
console.log(res13); // ['bike:1']
const res14 = await client.lRange('bikes:finished', 0, -1);
console.log(res14); // ['bike:2']
const res15 = await client.rPush('bikes:repairs', 'bike:1');
console.log(res15); // 1
const res16 = await client.rPush('bikes:repairs', 'bike:2');
console.log(res16); // 2
const res17 = await client.lPush('bikes:repairs', 'bike:important_bike');
console.log(res17); // 3
const res18 = await client.lRange('bikes:repairs', 0, -1);
console.log(res18); // ['bike:important_bike', 'bike:1', 'bike:2']
const res19 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res19); // 3
const res20 = await client.lPush(
'bikes:repairs', ['bike:important_bike', 'bike:very_important_bike']
);
console.log(res20); // 5
const res21 = await client.lRange('bikes:repairs', 0, -1);
console.log(res21); // ['bike:very_important_bike', 'bike:important_bike', 'bike:1', 'bike:2', 'bike:3']
const res22 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res22); // 3
const res23 = await client.rPop('bikes:repairs');
console.log(res23); // 'bike:3'
const res24 = await client.lPop('bikes:repairs');
console.log(res24); // 'bike:1'
const res25 = await client.rPop('bikes:repairs');
console.log(res25); // 'bike:2'
const res26 = await client.rPop('bikes:repairs');
console.log(res26); // None
const res27 = await client.lPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res27); // 5
const res28 = await client.lTrim('bikes:repairs', 0, 2);
console.log(res28); // true
const res29 = await client.lRange('bikes:repairs', 0, -1);
console.log(res29); // ['bike:5', 'bike:4', 'bike:3']
const res27eol = await client.rPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res27eol); // 5
const res28eol = await client.lTrim('bikes:repairs', -3, -1);
console.log(res28eol); // 'OK'
const res29eol = await client.lRange('bikes:repairs', 0, -1);
console.log(res29eol); // ['bike:3', 'bike:4', 'bike:5']
const res31 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2']);
console.log(res31); // 2
const res32 = await client.brPop('bikes:repairs', 1);
console.log(res32); // { key: 'bikes:repairs', element: 'bike:2' }
const res33 = await client.brPop('bikes:repairs', 1);
console.log(res33); // { key: 'bikes:repairs', element: 'bike:1' }
const res34 = await client.brPop('bikes:repairs', 1);
console.log(res34); // null
const res35 = await client.del('new_bikes');
console.log(res35); // 0
const res36 = await client.lPush('new_bikes', ['bike:1', 'bike:2', 'bike:3']);
console.log(res36); // 3
const res37 = await client.set('new_bikes', 'bike:1');
console.log(res37); // 'OK'
const res38 = await client.type('new_bikes');
console.log(res38); // 'string'
try {
const res39 = await client.lPush('new_bikes', 'bike:2', 'bike:3');
// redis.exceptions.ResponseError:
// [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value]
}
catch(e){
console.log(e);
}
await client.lPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res36); // 3
const res40 = await client.exists('bikes:repairs')
console.log(res40); // true
const res41 = await client.lPop('bikes:repairs');
console.log(res41); // 'bike:3'
const res42 = await client.lPop('bikes:repairs');
console.log(res42); // 'bike:2'
const res43 = await client.lPop('bikes:repairs');
console.log(res43); // 'bike:1'
const res44 = await client.exists('bikes:repairs');
console.log(res44); // 0
const res45 = await client.del('bikes:repairs');
console.log(res45); // 0
const res46 = await client.lLen('bikes:repairs');
console.log(res46); // 0
const res47 = await client.lPop('bikes:repairs');
console.log(res47); // null
const res48 = await client.lPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res48); // 5
const res49 = await client.lTrim('bikes:repairs', 0, 2);
console.log(res49); // 'OK'
const res50 = await client.lRange('bikes:repairs', 0, -1);
console.log(res50); // ['bike:5', 'bike:4', 'bike:3']
package io.redis.examples;
import org.junit.Test;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.args.ListDirection;
import java.util.List;
import static org.junit.Assert.*;
public class ListExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
long res1 = jedis.lpush("bikes:repairs", "bike:1");
System.out.println(res1); // >>> 1
long res2 = jedis.lpush("bikes:repairs", "bike:2");
System.out.println(res2); // >>> 2
String res3 = jedis.rpop("bikes:repairs");
System.out.println(res3); // >>> bike:1
String res4 = jedis.rpop("bikes:repairs");
System.out.println(res4); // >>> bike:2
long res5 = jedis.lpush("bikes:repairs", "bike:1");
System.out.println(res5); // >>> 1
long res6 = jedis.lpush("bikes:repairs", "bike:2");
System.out.println(res6); // >>> 2
String res7 = jedis.lpop("bikes:repairs");
System.out.println(res7); // >>> bike:2
String res8 = jedis.lpop("bikes:repairs");
System.out.println(res8); // >>> bike:1
long res9 = jedis.llen("bikes:repairs");
System.out.println(res9); // >>> 0
long res10 = jedis.lpush("bikes:repairs", "bike:1");
System.out.println(res10); // >>> 1
long res11 = jedis.lpush("bikes:repairs", "bike:2");
System.out.println(res11); // >>> 2
String res12 = jedis.lmove("bikes:repairs", "bikes:finished", ListDirection.LEFT, ListDirection.LEFT);
System.out.println(res12); // >>> bike:2
List<String> res13 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res13); // >>> [bike:1]
List<String> res14 = jedis.lrange("bikes:finished", 0, -1);
System.out.println(res14); // >>> [bike:2]
long res15 = jedis.rpush("bikes:repairs", "bike:1");
System.out.println(res15); // >>> 1
long res16 = jedis.rpush("bikes:repairs", "bike:2");
System.out.println(res16); // >>> 2
long res17 = jedis.lpush("bikes:repairs", "bike:important_bike");
System.out.println(res17); // >>> 3
List<String> res18 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res18); // >>> [bike:important_bike, bike:1, bike:2]
long res19 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3");
System.out.println(res19); // >>> 3
long res20 = jedis.lpush("bikes:repairs", "bike:important_bike", "bike:very_important_bike");
System.out.println(res20); // >>> 5
List<String> res21 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res21); // >>> [bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3]
long res22 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3");
System.out.println(res22); // >>> 3
String res23 = jedis.rpop("bikes:repairs");
System.out.println(res23); // >>> bike:3
String res24 = jedis.lpop("bikes:repairs");
System.out.println(res24); // >>> bike:1
String res25 = jedis.rpop("bikes:repairs");
System.out.println(res25); // >>> bike:2
String res26 = jedis.rpop("bikes:repairs");
System.out.println(res26); // >>> null
long res27 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
System.out.println(res27); // >>> 5
String res28 = jedis.ltrim("bikes:repairs", 0, 2);
System.out.println(res28); // >>> OK
List<String> res29 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res29); // >>> [bike:5, bike:4, bike:3]
res27 = jedis.rpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
System.out.println(res27); // >>> 5
res28 = jedis.ltrim("bikes:repairs", -3, -1);
System.out.println(res2); // >>> OK
res29 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res29); // >>> [bike:3, bike:4, bike:5]
long res31 = jedis.rpush("bikes:repairs", "bike:1", "bike:2");
System.out.println(res31); // >>> 2
List<String> res32 = jedis.brpop(1, "bikes:repairs");
System.out.println(res32); // >>> (bikes:repairs, bike:2)
List<String> res33 = jedis.brpop(1,"bikes:repairs");
System.out.println(res33); // >>> (bikes:repairs, bike:1)
List<String> res34 = jedis.brpop(1,"bikes:repairs");
System.out.println(res34); // >>> null
long res35 = jedis.del("new_bikes");
System.out.println(res35); // >>> 0
long res36 = jedis.lpush("new_bikes", "bike:1", "bike:2", "bike:3");
System.out.println(res36); // >>> 3
String res37 = jedis.set("new_bikes", "bike:1");
System.out.println(res37); // >>> OK
String res38 = jedis.type("new_bikes");
System.out.println(res38); // >>> string
try {
long res39 = jedis.lpush("new_bikes", "bike:2", "bike:3");
} catch (Exception e) {
e.printStackTrace();
// >>> redis.clients.jedis.exceptions.JedisDataException:
// >>> WRONGTYPE Operation against a key holding the wrong kind of value
}
jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3");
System.out.println(res36); // >>> 3
boolean res40 = jedis.exists("bikes:repairs");
System.out.println(res40); // >>> true
String res41 = jedis.lpop("bikes:repairs");
System.out.println(res41); // >>> bike:3
String res42 = jedis.lpop("bikes:repairs");
System.out.println(res42); // >>> bike:2
String res43 = jedis.lpop("bikes:repairs");
System.out.println(res43); // >>> bike:1
boolean res44 = jedis.exists("bikes:repairs");
System.out.println(res44); // >>> false
long res45 = jedis.del("bikes:repairs");
System.out.println(res45); // >>> 0
long res46 = jedis.llen("bikes:repairs");
System.out.println(res46); // >>> 0
String res47 = jedis.lpop("bikes:repairs");
System.out.println(res47); // >>> null
long res48 = jedis.lpush("bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5");
System.out.println(res48); // >>> 5
String res49 = jedis.ltrim("bikes:repairs", 0, 2);
System.out.println(res49); // >>> OK
List<String> res50 = jedis.lrange("bikes:repairs", 0, -1);
System.out.println(res50); // >>> [bike:5, bike:4, bike:3]
}
}
package example_commands_test
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
func ExampleClient_queue() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res1, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res1) // >>> 1
res2, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res2) // >>> 2
res3, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res3) // >>> bike:1
res4, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res4) // >>> bike:2
}
func ExampleClient_stack() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res5, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res5) // >>> 1
res6, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res6) // >>> 2
res7, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res7) // >>> bike:2
res8, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res8) // >>> bike:1
}
func ExampleClient_llen() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res9, err := rdb.LLen(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res9) // >>> 0
}
func ExampleClient_lmove_lrange() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res10, err := rdb.LPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res10) // >>> 1
res11, err := rdb.LPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res11) // >>> 2
res12, err := rdb.LMove(ctx, "bikes:repairs", "bikes:finished", "LEFT", "LEFT").Result()
if err != nil {
panic(err)
}
fmt.Println(res12) // >>> bike:2
res13, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res13) // >>> [bike:1]
res14, err := rdb.LRange(ctx, "bikes:finished", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res14) // >>> [bike:2]
}
func ExampleClient_lpush_rpush() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res15, err := rdb.RPush(ctx, "bikes:repairs", "bike:1").Result()
if err != nil {
panic(err)
}
fmt.Println(res15) // >>> 1
res16, err := rdb.RPush(ctx, "bikes:repairs", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res16) // >>> 2
res17, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike").Result()
if err != nil {
panic(err)
}
fmt.Println(res17) // >>> 3
res18, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res18) // >>> [bike:important_bike bike:1 bike:2]
}
func ExampleClient_variadic() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res19, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res19) // >>> 3
res20, err := rdb.LPush(ctx, "bikes:repairs", "bike:important_bike", "bike:very_important_bike").Result()
if err != nil {
panic(err)
}
fmt.Println(res20) // >>> 5
res21, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res21) // >>> [bike:very_important_bike bike:important_bike bike:1 bike:2 bike:3]
}
func ExampleClient_lpop_rpop() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res22, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res22) // >>> 3
res23, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res23) // >>> bike:3
res24, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res24) // >>> bike:1
res25, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res25) // >>> bike:2
res26, err := rdb.RPop(ctx, "bikes:repairs").Result()
if err != nil {
fmt.Println(err) // >>> redis: nil
}
fmt.Println(res26) // >>> <empty string>
}
func ExampleClient_ltrim() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res27, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
fmt.Println(res27) // >>> 5
res28, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result()
if err != nil {
panic(err)
}
fmt.Println(res28) // >>> OK
res29, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res29) // >>> [bike:5 bike:4 bike:3]
}
func ExampleClient_ltrim_end_of_list() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res30, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
fmt.Println(res30) // >>> 5
res31, err := rdb.LTrim(ctx, "bikes:repairs", -3, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res31) // >>> OK
res32, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res32) // >>> [bike:3 bike:4 bike:5]
}
func ExampleClient_brpop() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res33, err := rdb.RPush(ctx, "bikes:repairs", "bike:1", "bike:2").Result()
if err != nil {
panic(err)
}
fmt.Println(res33) // >>> 2
res34, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res34) // >>> [bikes:repairs bike:2]
res35, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res35) // >>> [bikes:repairs bike:1]
res36, err := rdb.BRPop(ctx, 1, "bikes:repairs").Result()
if err != nil {
fmt.Println(err) // >>> redis: nil
}
fmt.Println(res36) // >>> []
}
func ExampleClient_rule1() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res37, err := rdb.Del(ctx, "new_bikes").Result()
if err != nil {
panic(err)
}
fmt.Println(res37) // >>> 0
res38, err := rdb.LPush(ctx, "new_bikes", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res38) // >>> 3
}
func ExampleClient_rule11() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res39, err := rdb.Set(ctx, "new_bikes", "bike:1", 0).Result()
if err != nil {
panic(err)
}
fmt.Println(res39) // >>> OK
res40, err := rdb.Type(ctx, "new_bikes").Result()
if err != nil {
panic(err)
}
fmt.Println(res40) // >>> string
res41, err := rdb.LPush(ctx, "new_bikes", "bike:2", "bike:3").Result()
if err != nil {
fmt.Println(err)
// >>> WRONGTYPE Operation against a key holding the wrong kind of value
}
fmt.Println(res41)
}
func ExampleClient_rule2() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res42, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3").Result()
if err != nil {
panic(err)
}
fmt.Println(res42) // >>> 3
res43, err := rdb.Exists(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res43) // >>> 1
res44, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res44) // >>> bike:3
res45, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res45) // >>> bike:2
res46, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res46) // >>> bike:1
res47, err := rdb.Exists(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res47) // >>> 0
}
func ExampleClient_rule3() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res48, err := rdb.Del(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res48) // >>> 0
res49, err := rdb.LLen(ctx, "bikes:repairs").Result()
if err != nil {
panic(err)
}
fmt.Println(res49) // >>> 0
res50, err := rdb.LPop(ctx, "bikes:repairs").Result()
if err != nil {
fmt.Println(err) // >>> redis: nil
}
fmt.Println(res50) // >>> <empty string>
}
func ExampleClient_ltrim1() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res51, err := rdb.LPush(ctx, "bikes:repairs", "bike:1", "bike:2", "bike:3", "bike:4", "bike:5").Result()
if err != nil {
panic(err)
}
fmt.Println(res51) // >>> 5
res52, err := rdb.LTrim(ctx, "bikes:repairs", 0, 2).Result()
if err != nil {
panic(err)
}
fmt.Println(res52) // >>> OK
res53, err := rdb.LRange(ctx, "bikes:repairs", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(res53) // >>> [bike:5 bike:4 bike:3]
}
public class ListExample
{
public void run()
{
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
long res1 = db.ListLeftPush("bikes:repairs", "bike:1");
Console.WriteLine(res1); // >>> 1
long res2 = db.ListLeftPush("bikes:repairs", "bike:2");
Console.WriteLine(res2); // >>> 2
RedisValue res3 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res3); // >>> "bike:1"
RedisValue res4 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res4); // >>> "bike:2"
long res5 = db.ListLeftPush("bikes:repairs", "bike:1");
Console.WriteLine(res5); // >>> 1
long res6 = db.ListLeftPush("bikes:repairs", "bike:2");
Console.WriteLine(res6); // >>> 2
RedisValue res7 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res7); // >>> "bike:2"
RedisValue res8 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res8); // >>> "bike:1"
long res9 = db.ListLength("bikes:repairs");
Console.WriteLine(res9); // >>> 0
long res10 = db.ListLeftPush("{bikes}:repairs", "bike:1");
Console.WriteLine(res10); // >>> 1
long res11 = db.ListLeftPush("{bikes}:repairs", "bike:2");
Console.WriteLine(res11); // >>> 2
RedisValue res12 = db.ListMove("{bikes}:repairs", "{bikes}:finished", ListSide.Left, ListSide.Left);
Console.Write(res12); // >>> "bike:2"
RedisValue[] res13 = db.ListRange("{bikes}:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res13)); // >>> "bike:1"
RedisValue[] res14 = db.ListRange("{bikes}:finished", 0, -1);
Console.WriteLine(string.Join(", ", res14)); // >>> "bike:2"
long res15 = db.ListRightPush("bikes:repairs", "bike:1");
Console.WriteLine(res15); // >>> 1
long res16 = db.ListRightPush("bikes:repairs", "bike:2");
Console.WriteLine(res16); // >>> 2
long res17 = db.ListLeftPush("bikes:repairs", "bike:important_bike");
Console.WriteLine(res17); // >>> 3
RedisValue[] res18 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res18)); // >>> "bike:important_bike, bike:1, bike:2"
long res19 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res19); // >>> 3
long res20 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:important_bike", "bike:very_important_bike" });
Console.WriteLine(res20); // >>> 5
RedisValue[] res21 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res21));
// >>> "bike:very_important_bike, bike:important_bike, bike:1, bike:2, bike:3"
long res22 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res22); // >>> 3
RedisValue res23 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res23); // >>> "bike:3"
RedisValue res24 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res24); // >>> "bike:1"
RedisValue res25 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res25); // >>> "bike:2"
RedisValue res26 = db.ListRightPop("bikes:repairs");
Console.WriteLine(res26); // >>> <Empty string>
long res27 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
Console.WriteLine(res27); // >>> 5
db.ListTrim("bikes:repairs", 0, 2);
RedisValue[] res28 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res28)); // "bike:5, bike:4, bike:3"
long res29 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
Console.WriteLine(res29); // >>> 5
db.ListTrim("bikes:repairs", -3, -1);
RedisValue[] res30 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res30)); // >>> "bike:3, bike:4, bike:5"
long res31 = db.ListRightPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2" });
Console.WriteLine(res31); // >>> 2
Tuple<RedisKey, RedisValue>? res32 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1);
if (res32 != null)
Console.WriteLine($"{res32.Item1} -> {res32.Item2}"); // >>> "bikes:repairs -> bike:2"
Tuple<RedisKey, RedisValue>? res33 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1);
if (res33 != null)
Console.WriteLine($"{res33.Item1} -> {res33.Item2}"); // >>> "bikes:repairs -> bike:1"
Tuple<RedisKey, RedisValue>? res34 = db.BRPop(new RedisKey[] { "bikes:repairs" }, 1);
Console.WriteLine(res34); // >>> "Null"
bool res35 = db.KeyDelete("new_bikes");
Console.WriteLine(res35); // >>> False
long res36 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res36); // >>> 3
bool res37 = db.StringSet("new_bikes", "bike:1");
Console.WriteLine(res37); // >>> True
RedisType res38 = db.KeyType("new_bikes");
Console.WriteLine(res38); // >>> RedisType.String
try
{
long res39 = db.ListRightPush("new_bikes", new RedisValue[] { "bike:2", "bike:3" });
}
catch (Exception e)
{
Console.WriteLine(e);
}
long res40 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3" });
Console.WriteLine(res40); // >>> 3
bool res41 = db.KeyExists("bikes:repairs");
Console.WriteLine(res41); // >>> True
RedisValue res42 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res42); // >>> "bike:3"
RedisValue res43 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res43); // >>> "bike:2"
RedisValue res44 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res44); // >>> "bike:1"
bool res45 = db.KeyExists("bikes:repairs");
Console.WriteLine(res45); // >>> False
bool res46 = db.KeyDelete("bikes:repairs");
Console.WriteLine(res46); // >>> False
long res47 = db.ListLength("bikes:repairs");
Console.WriteLine(res47); // >>> 0
RedisValue res48 = db.ListLeftPop("bikes:repairs");
Console.WriteLine(res48); // >>> Null
long res49 = db.ListLeftPush("bikes:repairs", new RedisValue[] { "bike:1", "bike:2", "bike:3", "bike:4", "bike:5" });
Console.WriteLine(res49); // >>> 5
db.ListTrim("bikes:repairs", 0, 2);
RedisValue[] res50 = db.ListRange("bikes:repairs", 0, -1);
Console.WriteLine(string.Join(", ", res50)); // >>> "bike:5, bike:4, bike:3"
}
}
Limits
The max length of a Redis list is 2^32 - 1 (4,294,967,295) elements.
Performance
List operations that access its head or tail are O(1), which means they're highly efficient.
However, commands that manipulate elements within a list are usually O(n).
Examples of these include LINDEX
, LINSERT
, and LSET
.
Exercise caution when running these commands, mainly when operating on large lists.
Alternatives
Consider Redis streams as an alternative to lists when you need to store and process an indeterminate series of events.
Learn more
- Redis Lists Explained is a short, comprehensive video explainer on Redis lists.
- Redis University's RU101 covers Redis lists in detail.