This commit is contained in:
number571 2026-03-29 13:08:16 +07:00
parent 1eac6ea206
commit 68dd52dab5
3 changed files with 11 additions and 11 deletions

View File

@ -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
}

View File

@ -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))

View File

@ -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)
}