-
Notifications
You must be signed in to change notification settings - Fork 48
/
notification.go
55 lines (44 loc) · 1.25 KB
/
notification.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
package agollo
import (
"encoding/json"
"sync"
)
type notification struct {
NamespaceName string `json:"namespaceName,omitempty"`
NotificationID int `json:"notificationId,omitempty"`
}
type notificationRepo struct {
notifications sync.Map
}
func (n *notificationRepo) addNotificationID(namespace string, notificationID int) bool {
_, loaded := n.notifications.LoadOrStore(nomalizeNamespace(namespace), notificationID)
return !loaded
}
func (n *notificationRepo) setNotificationID(namespace string, notificationID int) {
n.notifications.Store(nomalizeNamespace(namespace), notificationID)
}
func (n *notificationRepo) getNotificationID(namespace string) (int, bool) {
if val, ok := n.notifications.Load(nomalizeNamespace(namespace)); ok {
if ret, ok := val.(int); ok {
return ret, true
}
}
return defaultNotificationID, false
}
func (n *notificationRepo) toString() string {
var notifications []*notification
n.notifications.Range(func(key, val interface{}) bool {
k, _ := key.(string)
v, _ := val.(int)
notifications = append(notifications, ¬ification{
NamespaceName: nomalizeNamespace(k),
NotificationID: v,
})
return true
})
bts, err := json.Marshal(¬ifications)
if err != nil {
return ""
}
return string(bts)
}