1 package com.redis.operations
10 def getConnection(key: String): Connection
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) }
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
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
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
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
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
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
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)
80 def incrBy(key: String, increment: Int): Int = {
81 val connection = getConnection(key)
82 connection.write("INCRBY "+key+" "+increment+"\r\n")
85 def incrOne(key: String): Int = {
86 val connection = getConnection(key)
87 connection.write("INCR "+key+"\r\n")
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)
97 def decrBy(key: String, decrement: Int): Int = {
98 val connection = getConnection(key)
99 connection.write("DECRBY "+key+" "+decrement+"\r\n")
102 def decrOne(key: String): Int = {
103 val connection = getConnection(key)
104 connection.write("DECR "+key+"\r\n")
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
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