How to sadd multiple elements with scala redisClient

now my code like this:

val elems  = List("1","2","3")
for(elem <- elems){
  redis.sadd("elems",elem)
}

is there any method to sadd multiple elements like python's api.

How to use sadd with multiple elements in Redis using Python API?

I use https://github.com/debasishg/scala-redis

Answers


Looking through sources you can see that sadd is defined here https://github.com/debasishg/scala-redis/blob/master/src/main/scala/com/redis/SetOperations.scala as:

def sadd(key: Any, value: Any, values: Any*)(implicit format: Format): Option[Long] =
    send("SADD", List(key, value) ::: values.toList)(asLong)

This is good if you list elements manually i.e.

redis.sadd("elems", "1", "2", "3")

It is done to disallow using empty list. What you can do with this api is

val elems  = List("1","2","3")
redis.sadd("elems", elems.head, elems.tail: _*)

you could as well add a method that suits you to RedisClient class via implicit conversion

implicit class RichRedis(self: RedisClient) {
    def saddseq(key: Any, values: Seq[Any])(implicit format: Format): Option[Long] =
        self.sadd(key, values.head, values.tail: _*)
}

and then you could do:

val elems  = List("1","2","3")
redis.saddseq("elems", elems)

Note that you can't overload sadd it needs a different name.

Remember that you cannot call head on an empty list, so whenever you use this method with empty list you will get exception, which is reasonable considering that you can't add just a key.


Need Your Help

PHP Aborting when creating large .zip file

php zip ziparchive

My php script running on CentOS 5.6 and PHP 5.2.12 using ZipArchive() and successfully creates .zip files over 1.6Gb but not for a larger archive of 2GB or larger - PHP aborts with no apparent error.

Getting a Signature Mismatch Error when Compiling Even though it Matches in VS.NET 2005

visual-studio compiler-construction reference

I changed a reference in my project from pointing to a specific hard-coded DLL to a project reference and now I'm getting an error telling me that the signature for some event handlers don't match ...