freecache icon indicating copy to clipboard operation
freecache copied to clipboard

Fix bug, add method Resize

Open zhangcunli opened this issue 7 years ago • 3 comments

  1. add method Resize newSize >= oldSize, keep all entry newSie < oldSize, discard all entry;

  2. 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 avatar Jan 10 '18 12:01 zhangcunli

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

coocood avatar Jan 10 '18 13:01 coocood

@zhangcunli any update?

coocood avatar Feb 06 '18 12:02 coocood

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;

zhangcunli avatar Sep 21 '18 07:09 zhangcunli