-
Notifications
You must be signed in to change notification settings - Fork 6
/
Kedis.kt
51 lines (34 loc) · 1.33 KB
/
Kedis.kt
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
package com.sxtanna.database
import com.sxtanna.database.base.Database
import com.sxtanna.database.config.DatabaseConfigManager
import com.sxtanna.database.config.KedisConfig
import com.sxtanna.database.ext.loadOrSave
import com.sxtanna.database.task.KedisTask
import redis.clients.jedis.Jedis
import redis.clients.jedis.JedisPool
import redis.clients.jedis.JedisPoolConfig
import java.io.File
class Kedis(override val conf : KedisConfig) : Database<Jedis, KedisConfig, KedisTask>() {
override val name : String = "Kedis"
lateinit var pool : JedisPool
private set
override fun load() {
val jedisConfig = JedisPoolConfig().apply {
maxTotal = conf.pool.maxSize
maxIdle = conf.pool.idleSize
testOnBorrow = true
}
pool = JedisPool(jedisConfig, conf.server.address, conf.server.port, conf.pool.timeout, conf.user.auth)
}
override fun poison() = pool.destroy()
override fun poolResource() : Jedis? = pool.resource?.apply { select(conf.user.defaultDB) }
override fun createTask(resource : Jedis) : KedisTask = KedisTask(resource)
companion object : DatabaseConfigManager<KedisConfig, Kedis> {
@JvmStatic
override fun get(file : File) = Kedis(getConfig(file))
@JvmStatic
override fun get(config: KedisConfig) = Kedis(config)
@JvmStatic
override fun getConfig(file : File) = file.loadOrSave(KedisConfig.DEFAULT)
}
}