-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
189 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package api | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"go.uber.org/zap" | ||
|
||
"github.com/iotexproject/iotex-core/blockchain/block" | ||
"github.com/iotexproject/iotex-core/pkg/log" | ||
) | ||
|
||
type ( | ||
// Bin stores []byte | ||
Bin struct { | ||
lastUpdate time.Time | ||
bin map[string][]byte | ||
} | ||
|
||
// ReadCache stores bins | ||
ReadCache struct { | ||
total, hit int | ||
lock sync.RWMutex | ||
timeout time.Duration | ||
bins map[string]*Bin | ||
} | ||
) | ||
|
||
// NewBin returns a new read cache | ||
func NewBin() *Bin { | ||
return &Bin{ | ||
lastUpdate: time.Now(), | ||
bin: make(map[string][]byte), | ||
} | ||
} | ||
|
||
func (b *Bin) expire(timeout time.Duration) bool { | ||
return b.lastUpdate.Add(timeout).Before(time.Now()) | ||
} | ||
|
||
func (b *Bin) get(key string) ([]byte, bool) { | ||
s, ok := b.bin[key] | ||
return s, ok | ||
} | ||
|
||
func (b *Bin) put(key string, value []byte) { | ||
b.bin[key] = value | ||
b.lastUpdate = time.Now() | ||
} | ||
|
||
// NewReadCache returns a new read cache | ||
func NewReadCache(timeout time.Duration) *ReadCache { | ||
return &ReadCache{ | ||
timeout: timeout, | ||
bins: make(map[string]*Bin), | ||
} | ||
} | ||
|
||
// Get reads according to key | ||
func (rc *ReadCache) Get(ns, key string) ([]byte, bool) { | ||
rc.lock.RLock() | ||
defer rc.lock.RUnlock() | ||
|
||
rc.total++ | ||
bin, ok := rc.bins[ns] | ||
if !ok { | ||
return nil, false | ||
} | ||
d, ok := bin.get(key) | ||
if ok { | ||
rc.hit++ | ||
log.L().Info("API cache hit", zap.Int("total", rc.total), zap.Int("hit", rc.hit)) | ||
} | ||
return d, ok | ||
} | ||
|
||
// Put writes according to key | ||
func (rc *ReadCache) Put(ns, key string, value []byte) { | ||
rc.lock.Lock() | ||
defer rc.lock.Unlock() | ||
|
||
bin, ok := rc.bins[ns] | ||
if !ok { | ||
bin = NewBin() | ||
rc.bins[ns] = bin | ||
} | ||
bin.put(key, value) | ||
} | ||
|
||
// Clear clears the cache | ||
func (rc *ReadCache) Clear() { | ||
rc.bins = nil | ||
rc.bins = make(map[string]*Bin) | ||
} | ||
|
||
// Respond implements the Responder interface | ||
func (rc *ReadCache) Respond(*block.Block) error { | ||
// for now, we just invalidate the entire cache | ||
rc.Clear() | ||
return nil | ||
} | ||
|
||
// Exit implements the Responder interface | ||
func (rc *ReadCache) Exit() { | ||
rc.Clear() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package api | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestReadCache(t *testing.T) { | ||
r := require.New(t) | ||
|
||
c := NewReadCache(200 * time.Millisecond) | ||
rcTests := []struct { | ||
ns, k string | ||
v []byte | ||
}{ | ||
{"a", "1", []byte{1}}, | ||
{"a", "2", []byte{2}}, | ||
{"b", "1", []byte{1}}, | ||
{"b", "2", []byte{2}}, | ||
} | ||
for _, v := range rcTests { | ||
d, ok := c.Get(v.ns, v.k) | ||
r.False(ok) | ||
r.Nil(d) | ||
c.Put(v.ns, v.k, v.v) | ||
} | ||
|
||
for _, v := range rcTests { | ||
d, ok := c.Get(v.ns, v.k) | ||
r.True(ok) | ||
r.Equal(v.v, d) | ||
} | ||
|
||
c.Clear() | ||
for _, v := range rcTests { | ||
d, ok := c.Get(v.ns, v.k) | ||
r.False(ok) | ||
r.Nil(d) | ||
c.Put(v.ns, v.k, v.v) | ||
} | ||
} |