-
Notifications
You must be signed in to change notification settings - Fork 132
/
cache.go
74 lines (59 loc) · 1.92 KB
/
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
package deployment
import (
"context"
)
// Cache is meant as an asynchronous KV memcache to reduce
// constant keeper queries. It is NOT threadsafe, it requires
// manual bootstrapping by loading data from the keeper once
// during initialisation and needs to be kept in sync
// with added and removed keeper data manually.
//
// If this works well, I'd like to build this into a v2
// that functions basically like a keeper wrapper, agnostic
// of the underlaying data and keeping in sync with added
// and removed items automatically.
type Cache struct {
data map[string]map[uint64]uint64
bootstrap func(context.Context, *Cache)
initialised bool
}
func NewCache(bootstrap func(context.Context, *Cache)) *Cache {
return &Cache{
data: make(map[string]map[uint64]uint64),
bootstrap: bootstrap,
}
}
func (c *Cache) Add(ctx context.Context, chainReferenceID string, smartContractID uint64, msgIDs ...uint64) {
c.assertExists(ctx, chainReferenceID)
for _, v := range msgIDs {
c.data[chainReferenceID][v] = smartContractID
}
}
func (c *Cache) Delete(ctx context.Context, chainReferenceID string, msgIDs ...uint64) {
c.assertExists(ctx, chainReferenceID)
for _, v := range msgIDs {
delete(c.data[chainReferenceID], v)
}
}
func (c *Cache) Has(ctx context.Context, chainReferenceID string, msgID uint64) bool {
c.assertExists(ctx, chainReferenceID)
_, found := c.data[chainReferenceID][msgID]
return found
}
func (c *Cache) Get(ctx context.Context, chainReferenceID string, msgID uint64) uint64 {
c.assertExists(ctx, chainReferenceID)
return c.data[chainReferenceID][msgID]
}
func (c *Cache) assertExists(ctx context.Context, chainReferenceID string) {
c.assertInitialised(ctx)
if _, found := c.data[chainReferenceID]; !found {
c.data[chainReferenceID] = make(map[uint64]uint64)
}
}
func (c *Cache) assertInitialised(ctx context.Context) {
if c.initialised {
return
}
c.initialised = true
c.bootstrap(ctx, c)
}