-
Notifications
You must be signed in to change notification settings - Fork 7
Support caching for map primitive #10
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
Conversation
| @@ -0,0 +1,136 @@ | |||
| // Copyright 2019-present Open Networking Foundation. | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to update the copyright header in my IDE 😄
pkg/client/map/cache.go
Outdated
| for event := range ch { | ||
| switch event.Type { | ||
| case EventNone: | ||
| m.cache.Add(event.Entry.Key, event.Entry) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These cache writes need to be locked
pkg/client/map/cache.go
Outdated
| // This check is performed because a concurrent event could update the cached entry | ||
| m.mu.Lock() | ||
| prevEntry, ok := m.cache.Get(key) | ||
| if !ok || prevEntry.(*Entry).Version < entry.Version { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This condition is unsafe for the same reason it’s unsafe on Get: a later Remove would make this version check impossible without maintaining tombstones. The entries just have to be removed from the cache.
|
I think this doesn’t actually provide RYW consistency. Cache hits read from an event stream, and cache misses from the database. But in the Go client, events are not serialized with reads, so it’s possible a cache miss can occur after a later cache hit in logical time. |
This PR adds support for wrapping a
Mapprimitive in an LRU cache.To cache a map, just use the
WithCacheoption when constructing the map:The cache is implemented using Hashicorp's LRU cache. When a client writes to the cache, any existing cached entries are deleted. A background goroutine listens for update events from the cluster to populate the cache. Because of the consistency guarantees of Atomix sessions, this guarantees a client will receive the same consistency guarantees as an uncached client.