Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug, add method Resize #33

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

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
@@ -114,7 +114,7 @@ func (seg *segment) set(key, value []byte, hashVal uint64, expireSeconds int) (e
match = false
// 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