/
blockengine_memcache.go
47 lines (38 loc) · 1.13 KB
/
blockengine_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
package storage
import (
"fmt"
lru "github.com/hashicorp/golang-lru"
"github.com/keybase/client/go/protocol/chat1"
"github.com/keybase/client/go/protocol/gregor1"
context "golang.org/x/net/context"
)
type blockEngineMemCacheImpl struct {
blockCache *lru.Cache
}
func newBlockEngineMemCache() *blockEngineMemCacheImpl {
c, _ := lru.New(100)
return &blockEngineMemCacheImpl{
blockCache: c,
}
}
func (b *blockEngineMemCacheImpl) key(uid gregor1.UID, convID chat1.ConversationID, id int) string {
return fmt.Sprintf("%s:%s:%d", uid, convID, id)
}
func (b *blockEngineMemCacheImpl) getBlock(ctx context.Context, uid gregor1.UID, convID chat1.ConversationID,
id int) (block, bool) {
key := b.key(uid, convID, id)
if v, ok := b.blockCache.Get(key); ok {
return v.(block), true
}
return block{}, false
}
func (b *blockEngineMemCacheImpl) writeBlock(ctx context.Context, uid gregor1.UID,
convID chat1.ConversationID, bl block) {
key := b.key(uid, convID, bl.BlockID)
b.blockCache.Add(key, bl)
}
func (b *blockEngineMemCacheImpl) OnLogout() error {
b.blockCache.Purge()
return nil
}
var blockEngineMemCache = newBlockEngineMemCache()