]> git.saurik.com Git - redis.git/blob - client-libraries/scala/src/main/scala/com/redis/Operations/ListOperations.scala
6538311b409951a987a70ce17df43c4f1cf646f8
[redis.git] / client-libraries / scala / src / main / scala / com / redis / Operations / ListOperations.scala
1 package com.redis.operations
2
3 /**
4 * Redis list operations
5 *
6 */
7
8 trait ListOperations{
9
10 def getConnection(key: String): Connection
11
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.
14 // LPUSH
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
19 }
20
21 // RPUSH
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
26 }
27
28 // LPOP
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")
33 connection.readString
34 }
35
36 // RPOP
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")
41 connection.readString
42 }
43
44 // LINDEX
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")
50 connection.readString
51 }
52
53 // LSET
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
59 }
60
61 // LLEN
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")
67 connection.readInt
68 }
69
70 // LRANGE
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")
76 connection.readList
77 }
78
79 // LTRIM
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
85 }
86
87 // LREM
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
93 }
94 }