-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathredissingleton.go
More file actions
42 lines (34 loc) · 876 Bytes
/
redissingleton.go
File metadata and controls
42 lines (34 loc) · 876 Bytes
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
package redis
import "sync"
// SingletonSession handles connection pool for Redis
type SingletonSession struct {
Session *RedisSession
Err error
server string
initMutex sync.Mutex
}
// Create a new Singleton
func Singleton(server string) *SingletonSession {
return &SingletonSession{
server: server,
}
}
// Connect connects to Redis and holds the Session and Err object
// in the SingletonSession struct
func (r *SingletonSession) Connect() (*RedisSession, error) {
r.initMutex.Lock()
defer r.initMutex.Unlock()
if r.Session != nil && r.Err == nil {
return r.Session, nil
}
r.Session, r.Err = NewRedisSession(&RedisConf{Server: r.server})
return r.Session, r.Err
}
// Close clears the connection to redis
func (r *SingletonSession) Close() {
r.initMutex.Lock()
defer r.initMutex.Unlock()
r.Session.Close()
r.Session = nil
r.Err = nil
}