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?
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?
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.