-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag_set.go
115 lines (92 loc) · 2.02 KB
/
tag_set.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
package tagcache
import (
"fmt"
"math"
"sort"
"strconv"
"strings"
"time"
)
type TagSet struct {
store CacheStore
names []string
}
func NewTagSet(store CacheStore, names []string) *TagSet {
t := &TagSet{store, names}
t.SetNames(names)
return t
}
func (this *TagSet) SetNames(names []string) {
this.names = names
}
func (this *TagSet) AddNames(names []string) {
names = append(this.names, names...)
m := make(map[string]struct{})
for i, l := 0, len(names); i < l; i++ {
m[names[i]] = struct{}{}
}
filterNames := make([]string, len(m))
i := 0
for k, _ := range m {
filterNames[i] = k
i++
}
this.names = filterNames
}
// 刷新所有 tag key
func (this *TagSet) Reset() error {
for _, name := range this.names {
this.ResetTag(name)
}
return nil
}
// 取tag id
func (this *TagSet) TagId(name string) string {
id := this.store.Get(this.TagKey(name))
if len(id) == 0 {
return this.ResetTag(name)
}
return id
}
// 取所有的tagid
func (this *TagSet) TagIds() []string {
l := len(this.names)
if l == 0 {
return []string{}
}
// 排序
sort.Strings(this.names)
ids := make([]string, l)
for i, name := range this.names {
ids[i] = this.TagId(name)
}
return ids
}
// 取命名空间
func (this *TagSet) GetNamespace() string {
ids := this.TagIds()
if len(ids) == 0 {
return ""
}
return strings.Join(ids, "|")
}
// 重置tagID 版本+1
func (this *TagSet) ResetTag(name string) string {
seq, err := this.store.Incr(this.TagKey(name))
if name == "getFollowUidCacheTag:uid1385217469" {
fmt.Printf("%v ResetTag name %s, seq %d, redis:%s, err %v ", time.Now(), name, seq, this.store.Info(), err)
}
// redis 起过最大值不会归零
if seq > (math.MaxInt64 - 10) {
seq = 0
this.store.Set(this.TagKey(name), "0", 0)
}
return strconv.FormatInt(seq, 10)
}
// Tag key
func (this *TagSet) TagKey(name string) string {
if name == "getFollowUidCacheTag:uid1385217469" {
fmt.Printf("%v ResetTag TagKey redis:%s, ", time.Now(), this.store.Info())
}
return fmt.Sprintf("tag:%s:key", name)
}