Skip to content

bugfix command set does not work #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 44 additions & 37 deletions src/main/scala/com/redis/Operations/Operations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,77 @@ package com.redis.operations
import com.redis.Connection

/**
* Redis operations
*
*/
* Redis operations
*
*/

trait Operations {

trait Operations{

def getConnection(key: String): Connection

// SET (key, value)
// SET (key, value, expiry)
// sets the key with the specified value, and with an optional expiry.
def set(key: String, value: String) = setKey(key, value)
def set(key: String, value: String, expiry: Int) = { setKey(key, value) && expire(key, expiry) }


def set(key: String, value: String, expiry: Int) = {
setKey(key, value) && expire(key, expiry)
}

// SET KEY (key, value)
// sets the key with the specified value.
def setKey(key: String, value: String): Boolean = {
val connection = getConnection(key)
connection.write("SET "+key+" "+value.length+"\r\n"+value+"\r\n")
connection.write("SET " + key + " " + value + "\r\n")
connection.readBoolean
}

// EXPIRE (key, expiry)
// sets the expire time (in sec.) for the specified key.
def expire(key: String, expiry: Int): Boolean = {
val connection = getConnection(key)
connection.write("EXPIRE "+key+" "+expiry+"\r\n")
connection.write("EXPIRE " + key + " " + expiry + "\r\n")
connection.readBoolean
}

// GET (key)
// gets the value for the specified key.
def get(key: String): Option[String] = {
val connection = getConnection(key)
val a = connection.write("GET "+key+"\r\n")
val a = connection.write("GET " + key + "\r\n")
connection.readResponse match {
case Some(r:String) => Some(r)
case Some(r: String) => Some(r)
case _ => None
}
}

// GETSET (key, value)
// is an atomic set this value and return the old value command.
def getSet(key: String, value: String): Option[String] = {
val connection = getConnection(key)
val a = connection.write("GETSET "+key+" "+value.length+"\r\n"+value+"\r\n")
val a = connection.write("GETSET " + key + " " + value + "\r\n")
connection.readResponse match {
case Some(r: String) => Some(r)
case _ => None
}
}

// SETNX (key, value)
// sets the value for the specified key, only if the key is not there.
def setUnlessExists(key: String, value: String): Boolean = {
val connection = getConnection(key)
connection.write("SETNX "+key+" "+value.length+"\r\n"+value+"\r\n")
connection.write("SETNX " + key + " " + value + "\r\n")
connection.readBoolean
}

// DELETE (key)
// deletes the specified key.
def delete(key: String): Boolean = {
val connection = getConnection(key)
connection.write("DEL "+key+"\r\n")
connection.write("DEL " + key + "\r\n")
connection.readBoolean
}

// INCR (key)
// INCR (key, increment)
// increments the specified key, optional the increment value.
Expand All @@ -79,69 +82,73 @@ trait Operations{
case (key: String) => incrOne(key)
case _ => None
}

def incrBy(key: String, increment: Int): Option[Int] = {
val connection = getConnection(key)
connection.write("INCRBY "+key+" "+increment+"\r\n")
connection.write("INCRBY " + key + " " + increment + "\r\n")
connection.readInt
}

def incrOne(key: String): Option[Int] = {
val connection = getConnection(key)
connection.write("INCR "+key+"\r\n")
connection.write("INCR " + key + "\r\n")
connection.readInt
}

// DECR (key)
// DECRBY (key, decrement)
// decrements the specified key, optional the decrement value.
def decr(key: String, decrement: Int) = decrBy(key, decrement)

def decr(key: String) = decrOne(key)

def decrBy(key: String, decrement: Int): Option[Int] = {
val connection = getConnection(key)
connection.write("DECRBY "+key+" "+decrement+"\r\n")
connection.write("DECRBY " + key + " " + decrement + "\r\n")
connection.readInt
}

def decrOne(key: String): Option[Int] = {
val connection = getConnection(key)
connection.write("DECR "+key+"\r\n")
connection.write("DECR " + key + "\r\n")
connection.readInt
}

// EXISTS (key)
// test if the specified key exists.
def exists(key: String): Boolean = {
val connection = getConnection(key)
connection.write("EXISTS "+key+"\r\n")
connection.write("EXISTS " + key + "\r\n")
connection.readBoolean
}

// TYPE (key)
// return the type of the value stored at key in form of a string.
def getType(key: String): Any = {
val connection = getConnection(key)
connection.write("TYPE "+key+"\r\n")
connection.write("TYPE " + key + "\r\n")
connection.readResponse
}

// TTL (key)
// returns the time to live for the key.
def ttl(key: String): Option[Int] = {
val connection = getConnection(key)
connection.write("TTL "+key+"\r\n")
connection.write("TTL " + key + "\r\n")
connection.readInt
}

// MSET
// Set the the respective keys to the respective values.
def mSet(keyValues: Map[String, String]): Boolean = {
def mSet(keyValues: Map[String, String]): Boolean = {
val connection = getConnection(keyValues.toArray.apply(0)._1)
connection.writeMultiBulk(keyValues.size * 2, "MSET", keyValues)
connection.readBoolean
}

// MSETNX
// Set the the respective keys to the respective values.
def mSetnx(keyValues: Map[String, String]): Boolean = {
def mSetnx(keyValues: Map[String, String]): Boolean = {
val connection = getConnection(keyValues.toArray.apply(0)._1)
connection.writeMultiBulk(keyValues.size * 2, "MSETNX", keyValues)
connection.readBoolean
Expand Down
5 changes: 1 addition & 4 deletions src/main/scala/com/redis/RedisClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@ import com.redis.operations._
*
*/

class Redis(val host: String, val port: Int) extends Operations
class Redis(val host: String = "localhost", val port: Int = 6379) extends Operations
with ListOperations
with SetOperations
with NodeOperations
with KeySpaceOperations
with SortOperations
with SortedSetOperations {

// auxiliary constructor
def this() = this("localhost", 6379)

// Points to the connection to a server instance
val connection = Connection(host, port)
var db: Int = 0
Expand Down