forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_bundle_cache.go
82 lines (70 loc) · 3.1 KB
/
key_bundle_cache.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
// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package kbfsmd
import (
"errors"
"github.com/keybase/kbfs/cache"
)
// KeyBundleCache is an interface to a key bundle cache for use with v3 metadata.
type KeyBundleCache interface {
// GetTLFReaderKeyBundle returns the TLFReaderKeyBundleV3 for
// the given TLFReaderKeyBundleID, or nil if there is none.
GetTLFReaderKeyBundle(TLFReaderKeyBundleID) (*TLFReaderKeyBundleV3, error)
// GetTLFWriterKeyBundle returns the TLFWriterKeyBundleV3 for
// the given TLFWriterKeyBundleID, or nil if there is none.
GetTLFWriterKeyBundle(TLFWriterKeyBundleID) (*TLFWriterKeyBundleV3, error)
// PutTLFReaderKeyBundle stores the given TLFReaderKeyBundleV3.
PutTLFReaderKeyBundle(TLFReaderKeyBundleID, TLFReaderKeyBundleV3)
// PutTLFWriterKeyBundle stores the given TLFWriterKeyBundleV3.
PutTLFWriterKeyBundle(TLFWriterKeyBundleID, TLFWriterKeyBundleV3)
}
// KeyBundleCacheStandard is an LRU-based implementation of the KeyBundleCache interface.
type KeyBundleCacheStandard struct {
cache cache.Cache
}
var _ KeyBundleCache = (*KeyBundleCacheStandard)(nil)
// NewKeyBundleCacheLRU constructs a new KeyBundleCacheStandard with LRU
// eviction strategy. The capacity of the cache is set to capacityBytes bytes.
func NewKeyBundleCacheLRU(capacityBytes int) *KeyBundleCacheStandard {
return &KeyBundleCacheStandard{cache.NewLRUEvictedCache(capacityBytes)}
}
// NewKeyBundleCacheRandom constructs a new KeyBundleCacheStandard with random
// eviction strategy. The capacity of the cache is set to capacityBytes bytes.
func NewKeyBundleCacheRandom(capacityBytes int) *KeyBundleCacheStandard {
return &KeyBundleCacheStandard{cache.NewRandomEvictedCache(capacityBytes)}
}
// GetTLFReaderKeyBundle implements the KeyBundleCache interface for KeyBundleCacheStandard.
func (k *KeyBundleCacheStandard) GetTLFReaderKeyBundle(
bundleID TLFReaderKeyBundleID) (*TLFReaderKeyBundleV3, error) {
if entry, ok := k.cache.Get(bundleID); ok {
if rkb, ok := entry.(TLFReaderKeyBundleV3); ok {
return &rkb, nil
}
// Shouldn't be possible.
return nil, errors.New("Invalid key bundle type")
}
return nil, nil
}
// GetTLFWriterKeyBundle implements the KeyBundleCache interface for KeyBundleCacheStandard.
func (k *KeyBundleCacheStandard) GetTLFWriterKeyBundle(
bundleID TLFWriterKeyBundleID) (*TLFWriterKeyBundleV3, error) {
if entry, ok := k.cache.Get(bundleID); ok {
if wkb, ok := entry.(TLFWriterKeyBundleV3); ok {
return &wkb, nil
}
// Shouldn't be possible.
return nil, errors.New("Invalid key bundle type")
}
return nil, nil
}
// PutTLFReaderKeyBundle implements the KeyBundleCache interface for KeyBundleCacheStandard.
func (k *KeyBundleCacheStandard) PutTLFReaderKeyBundle(
bundleID TLFReaderKeyBundleID, rkb TLFReaderKeyBundleV3) {
k.cache.Add(bundleID, rkb)
}
// PutTLFWriterKeyBundle implements the KeyBundleCache interface for KeyBundleCacheStandard.
func (k *KeyBundleCacheStandard) PutTLFWriterKeyBundle(
bundleID TLFWriterKeyBundleID, wkb TLFWriterKeyBundleV3) {
k.cache.Add(bundleID, wkb)
}