-
Notifications
You must be signed in to change notification settings - Fork 2
/
database.go
147 lines (125 loc) · 3.19 KB
/
database.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
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
package redis
import (
"fmt"
"github.com/go-redis/redis"
)
// Database keeps a connection to a Redis server.
type Database struct {
app string
sess *redis.Client
}
// Open a new database connection to the remote Redis server.
func Open(hostname, applicationName string) *Database {
return &Database{
app: applicationName,
sess: redis.NewClient(&redis.Options{Addr: hostname}),
}
}
// Close the connection to the remote database.
func (db *Database) Close() error {
return db.sess.Close()
}
func (db *Database) StringsSet(key string) *StringsSet {
return &StringsSet{
db: db,
key: fmt.Sprintf("%s:%s", db.app, key),
}
}
func (db *Database) Int64Set(key string) *Int64Set {
return &Int64Set{
db: db,
key: fmt.Sprintf("%s:%s", db.app, key),
}
}
func (db *Database) StringKV(key string) *StringKV {
return &StringKV{
db: db,
key: fmt.Sprintf("%s:%s", db.app, key),
}
}
func (db *Database) Int32KV(key string) *Int32KV {
return &Int32KV{
db: db,
key: fmt.Sprintf("%s:%s", db.app, key),
}
}
func (db *Database) Int64KV(key string) *Int64KV {
return &Int64KV{
db: db,
key: fmt.Sprintf("%s:%s", db.app, key),
}
}
func (db *Database) ProtoKV(key string) *ProtoKV {
return &ProtoKV{
db: db,
key: fmt.Sprintf("%s:%s", db.app, key),
}
}
func (db *Database) ProtoHash(key string) *ProtoHash {
return &ProtoHash{
db: db,
key: fmt.Sprintf("%s:%s", db.app, key),
}
}
func (db *Database) Counters(key string) *Counters {
return &Counters{
db: db,
key: fmt.Sprintf("%s:%s", db.app, key),
}
}
func (db *Database) BooleanKV(key string) *BooleanKV {
return &BooleanKV{
db: db,
key: fmt.Sprintf("%s:%s", db.app, key),
}
}
func (db *Database) TimeKV(key string) *TimeKV {
return &TimeKV{
db: db,
key: fmt.Sprintf("%s:%s", db.app, key),
}
}
func (db *Database) ProtoList(key string) *ProtoList {
return &ProtoList{
db: db,
key: fmt.Sprintf("%s:%s", db.app, key),
}
}
func (db *Database) StringsList(key string) *StringsList {
return &StringsList{
db: db,
key: fmt.Sprintf("%s:%s", db.app, key),
}
}
// DirectClient returns the underlying client of go-redis to call advanced
// methods not exposed through this library. Please consider to add the functionality
// here after it's tested to improve all the users of the library.
func (db *Database) DirectClient() *redis.Client {
return db.sess
}
// FlushAllKeysFromDatabase is exposed as a simple way for tests to reset the local
// database. It is not intended to be run in production. It will clean up all the keys
// of the whole database and leav an empty canvas to fill again.
func (db *Database) FlushAllKeysFromDatabase() error {
return db.sess.FlushAll().Err()
}
// PubSub returns an entrypoint to a PubSub queue in redisIt can be used to publish
// and receive protobuf messages.
func (db *Database) PubSub(name string) *PubSub {
return &PubSub{
db: db,
name: fmt.Sprintf("%s:%s", db.app, name),
}
}
// Hash returns a hash instance that stores full models as individual hash keys.
func (db *Database) Hash(name string, model Model) *Hash {
props, err := extractModelProps(model)
if err != nil {
panic(err)
}
return &Hash{
db: db,
name: fmt.Sprintf("%s:%s", db.app, name),
props: props,
}
}