/
RedisProvider.scala
184 lines (160 loc) · 4.45 KB
/
RedisProvider.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package com.turdwaffle.redis
import com.google.inject.name.Names
import com.google.inject.{AbstractModule, Guice, Key, Singleton}
import redis.clients.jedis.{Jedis, JedisPool, JedisPoolConfig}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.{Future, Promise}
import scala.util.Try
/**
* Trait containing the default connection pool settings we will use for
* redis instances.
*/
trait RedisPool {
/**
* IP/hostname of the redis server
*/
val hostName: String
/**
* Password for the redis server.
*/
val password: Option[String]
/**
* The redis connection pool to get resources from.
* Lazily instantiated.
* This is cleanly closed on JVM shutdown via sys.addShutdownHook
*/
lazy val jedisPool: JedisPool = password match {
case Some(p) => new JedisPool(jedisPoolConfig, hostName, port, timeOut, p)
case None => new JedisPool(jedisPoolConfig, hostName, port, timeOut)
}
/**
* The port redis is running on. 6379 unless overridden
*/
val port: Int = 6379
/**
* The redis timeout. 2000 unless overridden
*/
val timeOut: Int = 2000
/**
* The redis connection pool. Settings are:
* 25 minimum connections in the pool.
* 250 max connections in the pool.
* Unlimited connections on burst, if needed.
* Test connection on return to the pool.
* Test connections while idle.
* Evict members of the connection pool that have been idle for 1 hour.
*/
val jedisPoolConfig: JedisPoolConfig = new JedisPoolConfig()
jedisPoolConfig.setMinIdle(25)
jedisPoolConfig.setMaxIdle(250)
jedisPoolConfig.setMaxTotal(-1)
jedisPoolConfig.setTestOnReturn(true)
jedisPoolConfig.setTestWhileIdle(true)
jedisPoolConfig.setSoftMinEvictableIdleTimeMillis(3600000)
/**
* Create a way to reduce the boilerplate of calling redis actions
* i.e.
* def quick: Future[String] = redisAction { client =>
* client.hget(key, value)
* }
* Note the future is always successful.
* @param thunk
* @tparam T
* @return
*/
def redisAction[T](thunk: Jedis => T): Future[T] = {
val p = Promise[T]()
Future {
val jedis = jedisPool.getResource
val result = thunk(jedis)
try {
jedis.close()
} finally {
p.success(result)
}
}
p.future
}
/**
* Same as redisAction, but wrapping in a Try()
* Note the future is always successful.
* @param thunk
* @tparam T
* @return
*/
def redisTryAction[T](thunk: Jedis => T): Future[Try[T]] = {
val p = Promise[Try[T]]()
Future {
val jedis = jedisPool.getResource
val result = Try(thunk(jedis))
try {
jedis.close()
} finally {
p.success(result)
}
}
p.future
}
/**
* Add a shutdown hook to close the jedisPool.
*/
sys.addShutdownHook {
jedisPool.close()
}
}
/**
* A connection to a redis server on localhost
*/
@Singleton
class RedisLocalHost extends RedisPool {
override val password: Option[String] = None
override val hostName: String = "localhost"
}
/**
* A connection to a redis server on 127.0.0.1
* This is obviously the same as localhost, but just done as a 'for example'
* of how to add another IP/host
*/
@Singleton
class Redis127 extends RedisPool {
override val hostName: String = "127.0.0.1"
override val password: Option[String] = None
}
/**
* Guice module to configure our various redis configurations
*/
class RedisProvider extends AbstractModule {
override def configure(): Unit = {
// Bind an instance to localhost
bind(classOf[RedisPool])
.annotatedWith(Names.named("localhost"))
.to(classOf[RedisLocalHost])
// Bind an instance to 127.0.0.1
bind(classOf[RedisPool])
.annotatedWith(Names.named("127"))
.to(classOf[Redis127])
}
}
/**
* Helper object to retrieve instances of our various redis configurations
*/
object RedisProvider {
/**
* Guice injector for RedisProvider
*/
lazy val redisInjector = Guice.createInjector(new RedisProvider)
/**
* Connection pool to localhost
* @return
*/
def getRedisLocalHost: RedisLocalHost = redisInjector.getInstance(
Key.get(classOf[RedisPool], Names.named("localhost"))
).asInstanceOf[RedisLocalHost]
/**
* Connection pool to 127.0.0.1
* @return
*/
def getRedis127: Redis127 = redisInjector.getInstance(
Key.get(classOf[RedisPool], Names.named("127"))
).asInstanceOf[Redis127]
}