mirror of
https://github.com/number571/go-peer.git
synced 2026-09-12 19:50:00 +05:00
update
This commit is contained in:
parent
1eac6ea206
commit
68dd52dab5
2
pkg/storage/cache/cache_test.go
vendored
2
pkg/storage/cache/cache_test.go
vendored
@ -39,7 +39,7 @@ func TestLRUCache(t *testing.T) {
|
||||
t.Errorf("failed load %d", i)
|
||||
return
|
||||
}
|
||||
if !bytes.Equal(val, []byte(fmt.Sprintf("_%d_", i))) {
|
||||
if !bytes.Equal(val.([]byte), []byte(fmt.Sprintf("_%d_", i))) {
|
||||
t.Errorf("value is incorrect %d", i)
|
||||
return
|
||||
}
|
||||
|
||||
16
pkg/storage/cache/lru.go
vendored
16
pkg/storage/cache/lru.go
vendored
@ -12,7 +12,7 @@ var (
|
||||
|
||||
type sLRUCache struct {
|
||||
fMutex sync.RWMutex
|
||||
fMap map[string][]byte
|
||||
fMap map[string]interface{}
|
||||
fQueue []string
|
||||
fIndex uint64
|
||||
}
|
||||
@ -20,7 +20,7 @@ type sLRUCache struct {
|
||||
func NewLRUCache(pCapacity uint64) ILRUCache {
|
||||
return &sLRUCache{
|
||||
fQueue: make([]string, pCapacity),
|
||||
fMap: make(map[string][]byte, pCapacity),
|
||||
fMap: make(map[string]interface{}, pCapacity),
|
||||
}
|
||||
}
|
||||
|
||||
@ -43,7 +43,7 @@ func (p *sLRUCache) GetKey(i uint64) ([]byte, bool) {
|
||||
return hash, len(hash) != 0
|
||||
}
|
||||
|
||||
func (p *sLRUCache) Get(pKey []byte) ([]byte, bool) {
|
||||
func (p *sLRUCache) Get(pKey []byte) (interface{}, bool) {
|
||||
p.fMutex.RLock()
|
||||
defer p.fMutex.RUnlock()
|
||||
|
||||
@ -51,13 +51,13 @@ func (p *sLRUCache) Get(pKey []byte) ([]byte, bool) {
|
||||
return val, ok
|
||||
}
|
||||
|
||||
func (p *sLRUCache) Set(pKey, pValue []byte) bool {
|
||||
func (p *sLRUCache) Set(pKey []byte, pValue interface{}) bool {
|
||||
p.fMutex.Lock()
|
||||
defer p.fMutex.Unlock()
|
||||
|
||||
// hash already exists in queue
|
||||
hexKey := encoding.HexEncode(pKey)
|
||||
if _, ok := p.fMap[hexKey]; ok {
|
||||
key := encoding.HexEncode(pKey)
|
||||
if _, ok := p.fMap[key]; ok {
|
||||
return false
|
||||
}
|
||||
|
||||
@ -65,8 +65,8 @@ func (p *sLRUCache) Set(pKey, pValue []byte) bool {
|
||||
delete(p.fMap, p.fQueue[p.fIndex])
|
||||
|
||||
// push hash to queue
|
||||
p.fQueue[p.fIndex] = hexKey
|
||||
p.fMap[hexKey] = pValue
|
||||
p.fQueue[p.fIndex] = key
|
||||
p.fMap[key] = pValue
|
||||
|
||||
// increment queue index
|
||||
p.fIndex = (p.fIndex + 1) % uint64(len(p.fQueue))
|
||||
|
||||
4
pkg/storage/cache/types.go
vendored
4
pkg/storage/cache/types.go
vendored
@ -13,9 +13,9 @@ type ICache interface {
|
||||
}
|
||||
|
||||
type ICacheSetter interface {
|
||||
Set([]byte, []byte) bool
|
||||
Set([]byte, interface{}) bool
|
||||
}
|
||||
|
||||
type ICacheGetter interface {
|
||||
Get([]byte) ([]byte, bool)
|
||||
Get([]byte) (interface{}, bool)
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user