1 package com.redis.operations
4 * Redis list operations
10 def getConnection(key: String): Connection
12 // add the string value to the head (LPUSH) or tail (RPUSH) of the list stored at key.
13 // If the key does not exist an empty list is created just before the append operation. If the key exists but is not a List an error is returned.
15 def pushHead(key: String, value: String): Boolean = {
16 val connection = getConnection(key)
17 connection.write("LPUSH "+key+" "+value.length+"\r\n"+value+"\r\n")
18 connection.readBoolean
22 def pushTail(key: String, value: String): Boolean = {
23 val connection = getConnection(key)
24 connection.write("RPUSH "+key+" "+value.length+"\r\n"+value+"\r\n")
25 connection.readBoolean
29 // atomically return and remove the first (LPOP) or last (RPOP) element of the list
30 def popHead(key: String): String = {
31 val connection = getConnection(key)
32 connection.write("LPOP "+key+"\r\n")
37 // atomically return and remove the first (LPOP) or last (RPOP) element of the list
38 def popTail(key: String): String = {
39 val connection = getConnection(key)
40 connection.write("RPOP "+key+"\r\n")
45 // return the especified element of the list stored at the specified key. 0 is the first element, 1 the second and so on.
46 // Negative indexes are supported, for example -1 is the last element, -2 the penultimate and so on.
47 def listIndex(key: String, index: Int): String = {
48 val connection = getConnection(key)
49 connection.write("LINDEX "+key+" "+index+"\r\n")
54 // set the list element at index with the new value. Out of range indexes will generate an error
55 def listSet(key: String, index: Int, value: String): Boolean = {
56 val connection = getConnection(key)
57 connection.write("LSET "+key+" "+index+" "+value.length+"\r\n"+value+"\r\n")
58 connection.readBoolean
62 // return the length of the list stored at the specified key.
63 // If the key does not exist zero is returned (the same behaviour as for empty lists). If the value stored at key is not a list an error is returned.
64 def listLength(key: String): Int = {
65 val connection = getConnection(key)
66 connection.write("LLEN "+key+"\r\n")
71 // return the specified elements of the list stored at the specified key.
72 // Start and end are zero-based indexes. 0 is the first element of the list (the list head), 1 the next element and so on.
73 def listRange(key: String, start: Int, end: Int): List[String] = {
74 val connection = getConnection(key)
75 connection.write("LRANGE "+key+" "+start+" "+end+"\r\n")
80 // Trim an existing list so that it will contain only the specified range of elements specified.
81 def listTrim(key: String, start: Int, end: Int): Boolean = {
82 val connection = getConnection(key)
83 connection.write("LTRIM "+key+" "+start+" "+end+"\r\n")
84 connection.readBoolean
88 // Remove the first count occurrences of the value element from the list.
89 def listRem(key: String, count: Int, value: String): Boolean = {
90 val connection = getConnection(key)
91 connection.write("LREM "+key+" "+count+" "+value.length+"\r\n"+value+"\r\n")
92 connection.readBoolean