]> git.saurik.com Git - redis.git/blob - client-libraries/scala/src/main/scala/com/redis/Operations/Operations.scala
fb5c23d6a6550922cc2eefaef031db251286225f
[redis.git] / client-libraries / scala / src / main / scala / com / redis / Operations / Operations.scala
1 package com.redis.operations
2
3 /**
4 * Redis operations
5 *
6 */
7
8 trait Operations{
9
10 def getConnection(key: String): Connection
11
12 // SET (key, value)
13 // SET (key, value, expiry)
14 // sets the key with the specified value, and with an optional expiry.
15 def set(key: String, value: String) = setKey(key, value)
16 def set(key: String, value: String, expiry: Int) = { setKey(key, value) && expire(key, expiry) }
17
18 // SET KEY (key, value)
19 // sets the key with the specified value.
20 def setKey(key: String, value: String): Boolean = {
21 val connection = getConnection(key)
22 connection.write("SET "+key+" "+value.length+"\r\n"+value+"\r\n")
23 connection.readBoolean
24 }
25
26 // EXPIRE (key, expiry)
27 // sets the expire time (in sec.) for the specified key.
28 def expire(key: String, expiry: Int): Boolean = {
29 val connection = getConnection(key)
30 connection.write("EXPIRE "+key+" "+expiry+"\r\n")
31 connection.readBoolean
32 }
33
34 // GET (key)
35 // gets the value for the specified key.
36 def get(key: String): String = {
37 val connection = getConnection(key)
38 val a = connection.write("GET "+key+"\r\n")
39 connection.readResponse match {
40 case r: String => r.toString
41 case _ => null
42 }
43 }
44
45 // GETSET (key, value)
46 // is an atomic set this value and return the old value command.
47 def getSet(key: String, value: String): String = {
48 val connection = getConnection(key)
49 val a = connection.write("GETSET "+key+" "+value.length+"\r\n"+value+"\r\n")
50 connection.readResponse match {
51 case r: String => r.toString
52 case _ => null
53 }
54 }
55
56 // SETNX (key, value)
57 // sets the value for the specified key, only if the key is not there.
58 def setUnlessExists(key: String, value: String): Boolean = {
59 val connection = getConnection(key)
60 connection.write("SETNX "+key+" "+value.length+"\r\n"+value+"\r\n")
61 connection.readBoolean
62 }
63
64 // DELETE (key)
65 // deletes the specified key.
66 def delete(key: String): Boolean = {
67 val connection = getConnection(key)
68 connection.write("DEL "+key+"\r\n")
69 connection.readBoolean
70 }
71
72 // INCR (key)
73 // INCR (key, increment)
74 // increments the specified key, optional the increment value.
75 def incr(x: Any): Int = x match {
76 case (key: String, increment: Int) => incrBy(key, increment)
77 case (key: String) => incrOne(key)
78 case _ => 0
79 }
80 def incrBy(key: String, increment: Int): Int = {
81 val connection = getConnection(key)
82 connection.write("INCRBY "+key+" "+increment+"\r\n")
83 connection.readInt
84 }
85 def incrOne(key: String): Int = {
86 val connection = getConnection(key)
87 connection.write("INCR "+key+"\r\n")
88 connection.readInt
89 }
90
91 // DECR (key)
92 // DECRBY (key, decrement)
93 // decrements the specified key, optional the decrement value.
94 def decr(key: String, decrement: Int) = decrBy(key, decrement)
95 def decr(key: String) = decrOne(key)
96
97 def decrBy(key: String, decrement: Int): Int = {
98 val connection = getConnection(key)
99 connection.write("DECRBY "+key+" "+decrement+"\r\n")
100 connection.readInt
101 }
102 def decrOne(key: String): Int = {
103 val connection = getConnection(key)
104 connection.write("DECR "+key+"\r\n")
105 connection.readInt
106 }
107
108 // EXISTS (key)
109 // test if the specified key exists.
110 def exists(key: String): Boolean = {
111 val connection = getConnection(key)
112 connection.write("EXISTS "+key+"\r\n")
113 connection.readBoolean
114 }
115
116 // TYPE (key)
117 // return the type of the value stored at key in form of a string.
118 def getType(key: String): Any = {
119 val connection = getConnection(key)
120 connection.write("TYPE "+key+"\r\n")
121 connection.readResponse
122 }
123
124 }