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

Implemented new Append() method #203

Merged
merged 11 commits into from
Feb 5, 2020
Merged

Conversation

snacker81
Copy link
Contributor

@snacker81 snacker81 commented Feb 4, 2020

Implemented new Append() method with proper locking. Without this method you would need to wrap a Get()/Set() part with your own locks hurting performance.

Fixes #158

Copy link
Collaborator

@cristaloleg cristaloleg left a comment

Choose a reason for hiding this comment

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

LGTM

shard.go Outdated
@@ -25,40 +25,52 @@ type cacheShard struct {
stats Stats
}

func (s *cacheShard) get(key string, hashedKey uint64) ([]byte, error) {
s.lock.RLock()
func (s *cacheShard) get(key string, hashedKey uint64, skipLock bool) ([]byte, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not a fan of bool param here. It's easy to misuse such an API.
WDYT about left get as it was and create a separate method getWithoutLock (or a better name if you've) ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, and the same regarding set method 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am really missing parameter default values here (which would be false of cause to not change the API).

But I have no better idea. I could clone the method but this will result in a "lot" of duplicate code. I would basically implement the method again just leaving out the lock lines?

Since those calls are not public I think we can live with it. What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

There is no need for a default params. Both of the functions are from different worls - one is lock-free and another is not.

Combining them is a way to make an error quite easily.

That's why I suggest to split them into 2 different. Little code duplication isn't a problem (especially here).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done, good? :)

shard.go Outdated Show resolved Hide resolved
shard.go Outdated Show resolved Hide resolved
cristaloleg
cristaloleg previously approved these changes Feb 4, 2020
@cristaloleg
Copy link
Collaborator

@janisz @mxplusb ?

@cristaloleg
Copy link
Collaborator

btw, looks like your branch is outdated and you've a conflict, can you fix it too? pretty please :)

@snacker81
Copy link
Contributor Author

Fixed it, kinda "redid" the set()/get() part because I didnot want to screw up (merge conflict). Please check again carefully, thanks!

bigcache.go Outdated
@@ -134,6 +134,13 @@ func (c *BigCache) Set(key string, entry []byte) error {
return shard.set(key, hashedKey, entry)
}

// Append appends entry under existing key
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you elaborate this? What if key does not exist and how it differs from Set

Copy link
Contributor Author

Choose a reason for hiding this comment

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

pls check

bigcache_test.go Outdated

// when
cache.Append("key", value)
cache.Append("key", value)
Copy link
Collaborator

Choose a reason for hiding this comment

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

We need a test to append different value and to run multiple appends in parallel.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

improved the test, the test is already marked with t.Parallel() is that what you mean?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I thought about doing sometehing like we did here https://github.com/allegro/bigcache/blob/master/bigcache_test.go#L405-L406 just to check if locking is done properly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok good call, I implemented something like you mentioned and found another bug :)

One problem remains: the race detector triggers but I have no idea why. Do you guys have experience with it? Is it "valid" all the time?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok fixed it, it was because I was using the "err" variable from the outer scope! :)

shard.go Show resolved Hide resolved
shard.go Show resolved Hide resolved
shard.go Show resolved Hide resolved
bigcache_test.go Outdated
// given
cache, _ := NewBigCache(DefaultConfig(5 * time.Second))
key := "key"
value1 := make([]byte, 50)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
value1 := make([]byte, 50)
value1 := blob('a', 50)

Copy link
Collaborator

@janisz janisz left a comment

Choose a reason for hiding this comment

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

Looks good. Only small issues to fix.

@user3415653
Copy link

+1, need this for my project

@cristaloleg
Copy link
Collaborator

@user3415653 can you provide more details? Why do you need this feature? (it's ready to merge, but I'm curious about a bit wider context, probably we can improve bigcache in the future). Thanks in advance.

@cristaloleg
Copy link
Collaborator

@janisz PTAL 😉

@cristaloleg cristaloleg merged commit 75a65a8 into allegro:master Feb 5, 2020
@cristaloleg
Copy link
Collaborator

Thanks @snacker81

@siennathesane
Copy link
Collaborator

@cristaloleg I think it's too late now, but this should've been a v2.2.0 bump instead of v2.1.7 as it adds a new API.

@cristaloleg
Copy link
Collaborator

@mxplusb good point! will publish v2.2.0

@snacker81
Copy link
Contributor Author

Thanks @snacker81

@cristaloleg No problem thanks for the solution-oriented process :)

flisky pushed a commit to flisky/bigcache that referenced this pull request May 7, 2020
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.

Implement new append function to use bigcache as index for internal database
6 participants