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

Add EvictOldest to public API, add OnEviction and OnExpiration for setting callbacks outside of the Config #3

Merged
merged 1 commit into from
Oct 17, 2017

Conversation

danielstjules
Copy link
Contributor

Minimal changes, not breaking the api this time 😁

cc @segmentio/gateway

cache.go Outdated
return false
}

cache.evictions++

Choose a reason for hiding this comment

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

would this operation need to take place within a mutex?

Copy link
Contributor Author

@danielstjules danielstjules Oct 17, 2017

Choose a reason for hiding this comment

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

Good catch, it was previously wrapped with one cause of set

@danielstjules danielstjules force-pushed the danielstjules/public-and-handlers branch from 05c7790 to a6043d8 Compare October 17, 2017 20:16
@danielstjules danielstjules force-pushed the danielstjules/public-and-handlers branch from a6043d8 to e5fd14e Compare October 17, 2017 20:20
cache.go Outdated
@@ -259,6 +269,16 @@ func (cache *Cache) SetMaxAge(maxAge time.Duration) error {
return nil
}

// OnEviction sets the eviction callback
func (cache *Cache) OnEviction(callback func(key, value interface{})) {
cache.onEviction = callback

Choose a reason for hiding this comment

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

I don't think this is thread safe.

cache.go Outdated

// OnExpiration sets the expiration callback
func (cache *Cache) OnExpiration(callback func(key, value interface{})) {
cache.onExpiration = callback

Choose a reason for hiding this comment

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

I don't think this is thread safe.

Copy link

@f2prateek f2prateek left a comment

Choose a reason for hiding this comment

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

It should be ok based if you set it once and forget, but otherwise I think changing the callbacks isn't thread safe (since they might be set while another goroutine is invoking the old callbacks, and potentially could cause the old callback to be invoked even after OnEviction). Though maybe it's not a big deal that the behaviour isn't 100% correct here though.

@danielstjules
Copy link
Contributor Author

danielstjules commented Oct 17, 2017

@f2prateek I can highlight in comments that it's not threadsafe. I'd imagine it's only to be used during cache setup.

@danielstjules
Copy link
Contributor Author

danielstjules commented Oct 17, 2017

Actually, the only operations that use those callbacks are already guarded by the mutex so this is a quick change :)

@danielstjules danielstjules force-pushed the danielstjules/public-and-handlers branch from e5fd14e to 677bf32 Compare October 17, 2017 20:47
@danielstjules danielstjules merged commit c255b83 into master Oct 17, 2017
@danielstjules danielstjules deleted the danielstjules/public-and-handlers branch October 17, 2017 20:57
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