/
inbox_memcache.go
105 lines (88 loc) · 2.49 KB
/
inbox_memcache.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
package storage
import (
"sync"
"github.com/adamwalz/keybase-client/go/chat/types"
"github.com/adamwalz/keybase-client/go/libkb"
"github.com/adamwalz/keybase-client/go/protocol/chat1"
"github.com/adamwalz/keybase-client/go/protocol/gregor1"
)
type inboxMemCacheImpl struct {
sync.Mutex
versMap map[string]*inboxDiskVersions
indexMap map[string]*inboxDiskIndex
convMap map[string]types.RemoteConversation
}
func newInboxMemCacheImpl() *inboxMemCacheImpl {
return &inboxMemCacheImpl{
versMap: make(map[string]*inboxDiskVersions),
indexMap: make(map[string]*inboxDiskIndex),
convMap: make(map[string]types.RemoteConversation),
}
}
func (i *inboxMemCacheImpl) GetVersions(uid gregor1.UID) *inboxDiskVersions {
i.Lock()
defer i.Unlock()
if ibox, ok := i.versMap[uid.String()]; ok {
return ibox
}
return nil
}
func (i *inboxMemCacheImpl) PutVersions(uid gregor1.UID, ibox *inboxDiskVersions) {
i.Lock()
defer i.Unlock()
i.versMap[uid.String()] = ibox
}
func (i *inboxMemCacheImpl) GetIndex(uid gregor1.UID) *inboxDiskIndex {
i.Lock()
defer i.Unlock()
if ibox, ok := i.indexMap[uid.String()]; ok {
ret := ibox.DeepCopy()
return &ret
}
return nil
}
func (i *inboxMemCacheImpl) PutIndex(uid gregor1.UID, ibox *inboxDiskIndex) {
i.Lock()
defer i.Unlock()
i.indexMap[uid.String()] = ibox
}
func (i *inboxMemCacheImpl) convKey(uid gregor1.UID, convID chat1.ConversationID) string {
return uid.String() + convID.String()
}
func (i *inboxMemCacheImpl) GetConv(uid gregor1.UID, convID chat1.ConversationID) *types.RemoteConversation {
i.Lock()
defer i.Unlock()
if conv, ok := i.convMap[i.convKey(uid, convID)]; ok {
ret := conv.DeepCopy()
return &ret
}
return nil
}
func (i *inboxMemCacheImpl) PutConv(uid gregor1.UID, conv types.RemoteConversation) {
i.Lock()
defer i.Unlock()
i.convMap[i.convKey(uid, conv.GetConvID())] = conv
}
func (i *inboxMemCacheImpl) Clear(uid gregor1.UID) {
i.Lock()
defer i.Unlock()
delete(i.versMap, uid.String())
delete(i.indexMap, uid.String())
i.convMap = make(map[string]types.RemoteConversation)
}
func (i *inboxMemCacheImpl) clearCache() {
i.Lock()
defer i.Unlock()
i.versMap = make(map[string]*inboxDiskVersions)
i.indexMap = make(map[string]*inboxDiskIndex)
i.convMap = make(map[string]types.RemoteConversation)
}
func (i *inboxMemCacheImpl) OnLogout(m libkb.MetaContext) error {
i.clearCache()
return nil
}
func (i *inboxMemCacheImpl) OnDbNuke(m libkb.MetaContext) error {
i.clearCache()
return nil
}
var inboxMemCache = newInboxMemCacheImpl()