-
Notifications
You must be signed in to change notification settings - Fork 179
/
caches.go
95 lines (83 loc) · 2.69 KB
/
caches.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
package approvals
import (
"sync"
"github.com/onflow/flow-go/model/flow"
)
// ApprovalsCache encapsulates a map for storing result approvals indexed by approval partial ID
// to provide concurrent access.
type ApprovalsCache struct {
cache map[flow.Identifier]*flow.ResultApproval
lock sync.RWMutex
}
func NewApprovalsCache(sizeHint uint) *ApprovalsCache {
return &ApprovalsCache{
cache: make(map[flow.Identifier]*flow.ResultApproval, sizeHint),
lock: sync.RWMutex{},
}
}
// Put saves approval into cache; returns true iff approval was newly added
// the approvalID should be calculated as `approval.Body.PartialID()`
// it is taken as an input so that the caller can optimize by reusing the calculated approvalID.
func (c *ApprovalsCache) Put(approvalID flow.Identifier, approval *flow.ResultApproval) bool {
c.lock.Lock()
defer c.lock.Unlock()
if _, found := c.cache[approvalID]; !found {
c.cache[approvalID] = approval
return true
}
return false
}
// Get returns ResultApproval for the given ID (or nil if none is stored)
func (c *ApprovalsCache) Get(approvalID flow.Identifier) *flow.ResultApproval {
c.lock.RLock()
defer c.lock.RUnlock()
return c.cache[approvalID]
}
// All returns all stored approvals
func (c *ApprovalsCache) All() []*flow.ResultApproval {
c.lock.RLock()
defer c.lock.RUnlock()
all := make([]*flow.ResultApproval, 0, len(c.cache))
for _, approval := range c.cache {
all = append(all, approval)
}
return all
}
// IncorporatedResultsCache encapsulates a map for storing IncorporatedResults
// to provide concurrent access.
type IncorporatedResultsCache struct {
cache map[flow.Identifier]*flow.IncorporatedResult
lock sync.RWMutex
}
func NewIncorporatedResultsCache(sizeHint uint) *IncorporatedResultsCache {
return &IncorporatedResultsCache{
cache: make(map[flow.Identifier]*flow.IncorporatedResult, sizeHint),
lock: sync.RWMutex{},
}
}
// Put saves IncorporatedResult into cache; returns true iff IncorporatedResult was newly added
func (c *IncorporatedResultsCache) Put(key flow.Identifier, incRes *flow.IncorporatedResult) bool {
c.lock.Lock()
defer c.lock.Unlock()
if _, found := c.cache[key]; !found {
c.cache[key] = incRes
return true
}
return false
}
// Get returns IncorporatedResult for the given ID (or nil if none is stored)
func (c *IncorporatedResultsCache) Get(key flow.Identifier) *flow.IncorporatedResult {
c.lock.RLock()
defer c.lock.RUnlock()
return c.cache[key]
}
// All returns all stored approvals
func (c *IncorporatedResultsCache) All() []*flow.IncorporatedResult {
c.lock.RLock()
defer c.lock.RUnlock()
all := make([]*flow.IncorporatedResult, 0, len(c.cache))
for _, approval := range c.cache {
all = append(all, approval)
}
return all
}