freecache icon indicating copy to clipboard operation
freecache copied to clipboard

HI, is it to delete element in iterator?

Open roooneychina opened this issue 2 years ago • 1 comments

HI, would you like kindly to tell me about that :is it to delete element in iterator? many thanks.

roooneychina avatar Apr 01 '22 11:04 roooneychina

it seems that it is statisfied me. great func TestIterator(t *testing.T) { cache := NewCache(1024) count := 5 for i := 0; i < count; i++ { err := cache.Set([]byte(fmt.Sprintf("%d", i)), []byte(fmt.Sprintf("val%d", i)), 0) if err != nil { t.Error(err) } } //fmt.Println(cache.EntryCount()) go func() { res, err33 := cache.Get([]byte(fmt.Sprintf("%d", 3))) if err33 != nil{ t.Error((err33)) } fmt.Println(string(res)) cache.Del([]byte(fmt.Sprintf("%d", 3))) fmt.Println(cache.EntryCount()) cache.Del([]byte(fmt.Sprintf("%d", 2))) cache.Del([]byte(fmt.Sprintf("%d", 0))) cache.Del([]byte(fmt.Sprintf("%d", 1))) cache.Del([]byte(fmt.Sprintf("%d", 1))) }()

go func() {
	time.Sleep(time.Second*10)

	err := cache.Set([]byte(fmt.Sprintf("%d", 6)), []byte(fmt.Sprintf("val%d", 6)), 0)
	if err!= nil{
		t.Error((err))
	}
//	fmt.Println(string(res))
	//cache.Del([]byte(fmt.Sprintf("%d", 3)))
	fmt.Println(cache.EntryCount())
}()
// Set some value that expires to make sure expired entry is not returned.
//cache.Set([]byte("abc"), []byte("def"), 1)
//time.Sleep(5 * time.Second)
it := cache.NewIterator()
var ii int64
for ii=0; ii < cache.EntryCount(); ii++ {
	entry := it.Next()
	time.Sleep(time.Second*5)
	if entry == nil {
		continue;
		//t.Fatalf("entry is nil for %d", i)
	}
	go func() {
		_, err33 := cache.Get([]byte(fmt.Sprintf("%d", 2)))
		if err33 != nil{
			fmt.Println("not found")
		}

	}()

	fmt.Println("start")
	fmt.Println(string(entry.Value))
	if string(entry.Value) != "val"+string(entry.Key) {
		t.Fatalf("entry key value not match %s %s", entry.Key, entry.Value)
	}
	err := cache.Set([]byte(fmt.Sprintf("%d", 4)), []byte(fmt.Sprintf("val%d", 18)), 0)
	if err!= nil{
		t.Error((err))
	}
	cache.Del([]byte(fmt.Sprintf("%d", 4)))
	cache.Del([]byte(fmt.Sprintf("%d", 4)))

}
e := it.Next()
if e != nil {
	t.Fail()
}
time.Sleep(time.Second*20)
fmt.Println("second round")
it2 := cache.NewIterator()
var i int64
for i = 0; i < cache.EntryCount(); i++ {
	entry := it2.Next()

	if entry == nil {
		break
	}
	fmt.Println(string(entry.Value))
}

}

roooneychina avatar Apr 01 '22 13:04 roooneychina