freecache
freecache copied to clipboard
Fix bug, add method Resize
-
add method Resize newSize >= oldSize, keep all entry newSie < oldSize, discard all entry;
-
bug, case:
package main
import (
"fmt"
_ "strconv"
_ "time"
"github.com/coocood/freecache"
//"github.com/zhangcunli/freecache"
)
func main() {
cachesize := 512 * 1024
cache := freecache.NewCache(cachesize)
value1 := "aaa"
key1 := []byte("key1")
value := value1
cache.Set(key1, []byte(value), 0)
it := cache.NewIterator()
for {
entry := it.Next()
if entry == nil {
break
}
fmt.Printf("-----------> iterator, key:%s, value:%s\n", string(entry.Key), string(entry.Value))
}
value = value1 + "XXXXXX"
cache.Set(key1, []byte(value), 0)
value = value1 + "XXXXYYYYYYY"
cache.Set(key1, []byte(value), 0)
it = cache.NewIterator()
for {
entry := it.Next()
if entry == nil {
break
}
fmt.Printf("-----------> iterator, key:%s, value:%s\n", string(entry.Key), string(entry.Value))
}
}
@zhangcunli Thank you for your great work. Would you please add the case to test and make sure all tests are passed? And we need to test that when size is increased, all old entries are preserved.
@zhangcunli any update?
Modify the ringbuffer resize, ringbuffer_test.go: //////////////////////////////////////////////////////////////////////////////////// ringbuf_test.go:28: [size:32, start:17, end:33, index:0] ringbuf_test.go:29: ghibbbbccccefghi size=16 --> size to 64 --> size to 32 Now ringbuffer index should be 16, we should not set the index to 0 and overwirte the ringbuffer;