-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRedis.imba
More file actions
106 lines (72 loc) · 2.31 KB
/
Copy pathRedis.imba
File metadata and controls
106 lines (72 loc) · 2.31 KB
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
import * as redis from 'redis'
const settings = { instances: {}, config: {}, running: [] }
const socketProperties = [
'port',
'host',
'family',
'path',
'connectTimeout'
'noDelay',
'keepAlive',
'tls',
'reconnectStrategy'
]
export default class Redis
def constructor database\string = 'default'
if settings.instances[database]
return settings.instances[database]
let connection = settings.config.get("database.redis.{database}")
const socket = {}
for own key, value of connection
if key in socketProperties
socket[key] = value
delete connection[key]
if Object.keys(socket).length > 0
connection.socket = socket
# merge redis options to redis connection.
connection = Object.assign(
connection,
settings.config.get('database.redis.options', {})
)
# prepend database name to connection prefix.
if connection.prefix
connection.prefix += "{database}_"
# remove null values.
for own key, value of connection
if value == null || value == 'null'
delete connection[key]
const db = connection.database
if db !== undefined || db !== null
delete connection.database
connection.db = db
settings.instances[database] = redis.createClient(connection)
settings.instances[database].on 'error', do(error)
throw error
static def connection database\string = 'default'
let instance = settings.instances[database]
if instance == undefined || instance == null
new Redis(database, config)
instance = settings.instances[database]
if settings.running.indexOf(database) == -1
await instance.connect!
settings.running.push(database)
settings.instances[database] = instance
instance
static def configure config
settings.config = config
static def closeAll
for own database, instance of settings.instances
settings.running.splice(settings.running.indexOf(database), 1)
instance.quit!
static def set key\string, value\string, options\any = null
const i = await self.connection!
await i.set key, value, options
static def get key\string
const i = await self.connection!
await i.get key
static def del key\string
const i = await self.connection!
await i.del key
static def command command\string, key\string, value\string|null = null, nx\any = null
const i = await self.connection!
await i.sendCommand command, key, value, nx