]> git.saurik.com Git - redis.git/blob - client-libraries/scala/src/main/scala/com/redis/RedisCluster.scala
0a753719e7a07da187ed2fa1372d9067b3e00eda
[redis.git] / client-libraries / scala / src / main / scala / com / redis / RedisCluster.scala
1 package com.redis
2
3 import com.redis.operations._
4
5 /**
6 * Redis cluster
7 *
8 */
9
10 import scala.collection.mutable.ArrayBuffer
11
12 class RedisCluster(val hosts: String*) extends Operations with ListOperations with SetOperations with HashRing with SortOperations {
13
14 // Get Redis Client connection inside the HashRing.
15 def getConnection(key: String) = {
16 getNode(key).connection
17 }
18
19 // Default value used on MemCache client.
20 private val NUMBER_OF_REPLICAS = 160
21 val replicas = NUMBER_OF_REPLICAS
22
23 // Outputs a formatted representation of the Redis server.
24 override def toString = cluster.mkString(", ")
25
26 // Connect the client and add it to the cluster.
27 def connectClient(host: String): Boolean = {
28 val h = host.split(":")(0)
29 val p = host.split(":")(1)
30 val client = new Redis(h.toString, p.toString.toInt)
31 addNode(client)
32 client.connected
33 }
34
35 // Connect all clients in the cluster.
36 def connect = cluster.map(c => c.connect)
37
38 // Initialize cluster.
39 def initialize_cluster = hosts.map(connectClient(_))
40
41 initialize_cluster
42 }