Skip to content

Conversation

@zhangcunli
Copy link

@zhangcunli zhangcunli commented Jan 10, 2018

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

segment.go Outdated
// increase capacity and limit entry len.
for hdr.valCap < hdr.valLen {
hdr.valCap = hdr.valLen
hdr.valCap *= hdr.valCap
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*= 2

@coocood
Copy link
Owner

coocood commented Jan 10, 2018

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

cache.go Outdated
if entry == nil {
break
}
entryCount = atomic.AddInt64(&entryCount, 1)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why atomic.AddUint64 ? There is no concurrent access to local variable.

Why this function were changed at all? Can you prove there is a need to change this function?

@coocood
Copy link
Owner

coocood commented Feb 6, 2018

@zhangcunli any update?

@zhangcunli
Copy link
Author

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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants