-
Notifications
You must be signed in to change notification settings - Fork 15
/
chat_repository.go
43 lines (35 loc) · 980 Bytes
/
chat_repository.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
package margelet
import (
"gopkg.in/redis.v3"
"strconv"
"strings"
)
// ChatRepository - repository for started chats
type ChatRepository struct {
key string
redis *redis.Client
}
func newChatRepository(prefix string, redis *redis.Client) *ChatRepository {
key := strings.Join([]string{prefix, "margelet_chats"}, "-")
return &ChatRepository{key, redis}
}
func (chat *ChatRepository) Add(id int64) {
chat.redis.SAdd(chat.key, strconv.FormatInt(id, 10))
}
func (chat *ChatRepository) Exist(id int64) (res bool) {
res, _ = chat.redis.SIsMember(chat.key, strconv.FormatInt(id, 10)).Result()
return
}
func (chat *ChatRepository) Remove(id int64) {
chat.redis.SRem(chat.key, strconv.FormatInt(id, 10))
}
func (chat *ChatRepository) All() []int64 {
var result []int64
strings, _ := chat.redis.SMembers(chat.key).Result()
for _, str := range strings {
if c, err := strconv.ParseInt(str, 10, 64); err == nil {
result = append(result, c)
}
}
return result
}