forked from vireshas/mantle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mantle.go
69 lines (60 loc) · 1.58 KB
/
mantle.go
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
package mantle
import (
"github.com/goibibo/mantle/backends"
)
//only strings are supported
type Mantle interface {
Get(key string) string
Set(key string, value interface{}) bool
Delete(keys ...interface{}) int
Setex(key string, duration int, value interface{}) bool
MGet(keys ...interface{}) []string
MSet(keyValMap map[string]interface{}) bool
Expire(key string, duration int) bool
Execute(cmd string, args ...interface{}) (interface{}, error)
}
//helper func
func redisConns(settings mantle.PoolSettings) *mantle.Redis {
redis := &mantle.Redis{}
redis.Configure(settings)
return redis
}
func memcacheConns(settings mantle.PoolSettings) *mantle.Memcache {
redis := &mantle.Memcache{}
redis.Configure(settings)
return redis
}
//generic pool settings
func getSettings(o *Orm) mantle.PoolSettings {
return mantle.PoolSettings{
HostAndPorts: o.HostAndPorts,
Capacity: o.Capacity,
MaxCapacity: o.Capacity,
Options: o.Options}
}
//this struct is exported
type Orm struct {
//redis|memcache|cassandra
Driver string
//arrays of ip:port,ip:port
HostAndPorts []string
//pool size
Capacity int
//any other options thats needed for creating a connection
Options map[string]string
}
//mantle is a wrapper for many nosql dbs
func (o *Orm) New() Mantle {
settings := getSettings(o)
if o.Driver == "memcache" {
return memcacheConns(settings)
} else {
return redisConns(settings)
}
}
//override mantle and get a redis client
func (o *Orm) GetRedisConn() (*mantle.RedisConn, error) {
settings := getSettings(o)
redisPool := redisConns(settings)
return redisPool.GetClient()
}