-
Notifications
You must be signed in to change notification settings - Fork 250
feat: in memory adapter for entity caching cache #3137
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
Merged
+905
−6
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d83f6ae
feat: in memory adapter
SkArchon ea86a48
fix: tests
SkArchon a066019
fix: updates
SkArchon e863832
fix: updates
SkArchon 6e562d8
fix: update in memory logic and tests
SkArchon d508d07
fix: gofmt
SkArchon f80628a
fix: review comments
SkArchon 078474b
fix: updates
SkArchon 450d257
fix: updates
SkArchon 052d793
Merge remote-tracking branch 'origin/main' into milinda/eng-9922-in-m…
SkArchon 0e1b212
Merge remote-tracking branch 'origin/main' into milinda/eng-9922-in-m…
SkArchon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| package in_memory | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "fmt" | ||
| "sync" | ||
|
|
||
| "github.com/dgraph-io/ristretto/v2" | ||
| enginecache "github.com/wundergraph/graphql-go-tools/v2/pkg/entitycaching" | ||
| ) | ||
|
|
||
| const entryCost = 1 | ||
| const maxSize = 100_000 | ||
|
|
||
| type InMemoryCache struct { | ||
| cache *ristretto.Cache[string, []byte] | ||
| // closeOnce keeps Close idempotent, so two shutdown paths reaching it is | ||
| // not a panic on a channel ristretto has already closed. | ||
| closeOnce sync.Once | ||
| } | ||
|
|
||
| // NewInMemoryCache returns a cache holding at most maxEntries entries. The | ||
| // caller owns it and must Close it. | ||
| func NewInMemoryCache(maxEntries int64) (*InMemoryCache, error) { | ||
| if maxEntries <= 0 { | ||
| return nil, fmt.Errorf("in memory entity cache needs a positive size, got %d", maxEntries) | ||
| } | ||
| if maxEntries > maxSize { | ||
| return nil, fmt.Errorf("in memory entity cache size is too large: %d", maxEntries) | ||
| } | ||
|
|
||
| cache, err := ristretto.NewCache(&ristretto.Config[string, []byte]{ | ||
| MaxCost: maxEntries, | ||
| NumCounters: maxEntries * 10, | ||
| IgnoreInternalCost: true, | ||
| BufferItems: 64, | ||
| }) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create in memory entity cache: %w", err) | ||
| } | ||
|
|
||
| return &InMemoryCache{cache: cache}, nil | ||
| } | ||
|
|
||
| // GetMany implements enginecache.GetMany. | ||
| func (c *InMemoryCache) GetMany(ctx context.Context, keys []string) (map[string]enginecache.Item, error) { | ||
| if err := ctx.Err(); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if len(keys) == 0 { | ||
| return nil, nil | ||
| } | ||
|
|
||
| results := make(map[string]enginecache.Item, len(keys)) | ||
| for _, key := range keys { | ||
| // In case there are duplicate keys, ignore duplicate fetches | ||
| if _, found := results[key]; found { | ||
| continue | ||
| } | ||
|
|
||
| // We need to do getTTL OR store the expiration date on the entry | ||
| // however doing the latter still means that its a different value | ||
| // from the expiration date the ristretto uses internally | ||
| value, getOk := c.cache.Get(key) | ||
| ttl, ttlOk := c.cache.GetTTL(key) | ||
|
|
||
| // Either the key does not exist, or it just expired now | ||
| if !getOk || !ttlOk || ttl <= 0 { | ||
| continue | ||
| } | ||
|
|
||
| results[key] = enginecache.Item{Key: key, Value: bytes.Clone(value), TTL: ttl} | ||
| } | ||
|
|
||
| return results, nil | ||
| } | ||
|
|
||
| // SetMany implements enginecache.SetMany. | ||
| func (c *InMemoryCache) SetMany(ctx context.Context, items []enginecache.Item) error { | ||
| if err := ctx.Err(); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if len(items) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| // Map used for deduplications | ||
| last := make(map[string]enginecache.Item, len(items)) | ||
|
|
||
| for _, item := range items { | ||
| if item.TTL <= 0 { | ||
| return fmt.Errorf("%w: key %q", enginecache.ErrMissingTTL, item.Key) | ||
| } | ||
| // In case user sends same key, use the last entry to save | ||
| last[item.Key] = item | ||
| } | ||
|
|
||
| for _, item := range last { | ||
| c.cache.SetWithTTL(item.Key, bytes.Clone(item.Value), entryCost, item.TTL) | ||
| } | ||
|
|
||
| c.cache.Wait() | ||
| return nil | ||
| } | ||
|
|
||
| // Close closes the in memory cache | ||
| func (c *InMemoryCache) Close() { | ||
| c.closeOnce.Do(c.cache.Close) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.