-
Notifications
You must be signed in to change notification settings - Fork 249
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
Changes from 6 commits
d83f6ae
ea86a48
a066019
e863832
6e562d8
d508d07
f80628a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package cache | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "sync" | ||
| "time" | ||
|
|
||
| enginecache "github.com/wundergraph/graphql-go-tools/v2/pkg/entitycaching" | ||
| ) | ||
|
|
||
| // ErrMissingTTL reports items that were rejected because they carried no | ||
| // positive TTL. Every cached entry expires, so a TTL is mandatory. | ||
| var ErrMissingTTL = errors.New("cache item requires a positive TTL") | ||
|
|
||
| type inMemoryEntry struct { | ||
| value []byte | ||
| // expiresAt is always set, an entry without an expiry is never stored. | ||
| expiresAt time.Time | ||
| } | ||
|
|
||
| // InMemoryCache sweeps every expired entry on each call, so both methods take | ||
| // the mutex exclusively, hence a plain Mutex rather than an RWMutex. | ||
| // This cache should only be used for tests OR development and never production | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This rather belongs to the Also this could become a major bottleneck. Using a normal mutex instead of an RWMutex forces syncronous reads to the cache for all concurrent requests on the router. You can't use an RWMutex because of I mean yeah it's only meant to be used in tests but if Ristretto can solve it for free and have it production ready for when the situation calls for it.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. switched to ristretto |
||
| type InMemoryCache struct { | ||
| mu sync.Mutex | ||
| entries map[string]inMemoryEntry | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. switched to ristretto |
||
| now func() time.Time | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is only for test purposes right? Have you considered using
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cleaned up |
||
| } | ||
|
|
||
| // NewInMemoryCache returns an empty InMemoryCache ready for use. | ||
| func NewInMemoryCache() *InMemoryCache { | ||
| return &InMemoryCache{ | ||
| entries: make(map[string]inMemoryEntry), | ||
| now: time.Now, | ||
| } | ||
| } | ||
|
|
||
| // GetMany returns one result per key, in the same order as keys. It also drops | ||
| // every expired entry, including those no key in this batch asked about. | ||
| func (c *InMemoryCache) GetMany(ctx context.Context, keys []string) ([]enginecache.Result, error) { | ||
| if len(keys) == 0 { | ||
| return nil, nil | ||
| } | ||
|
|
||
| if err := ctx.Err(); err != nil { | ||
| return nil, err | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd put this on the top like in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
|
||
| c.mu.Lock() | ||
| defer c.mu.Unlock() | ||
|
|
||
| // Sweeping first means anything still present is live, so the lookup below | ||
| // needs no expiry check of its own. | ||
| c.sweepExpired(c.now()) | ||
|
|
||
| results := make([]enginecache.Result, len(keys)) | ||
| for i, key := range keys { | ||
| entry, ok := c.entries[key] | ||
| if !ok { | ||
| continue | ||
| } | ||
| results[i] = enginecache.Result{Value: bytes.Clone(entry.value), Found: true} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're not really making use of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cleaned up |
||
| } | ||
|
|
||
| return results, nil | ||
| } | ||
|
|
||
| // SetMany stores every item, all of which must carry a positive TTL. A single | ||
| // item without one fails the whole batch with an ErrMissingTTL and nothing is | ||
| // stored. It otherwise only fails on a cancelled context. It also drops every | ||
| // expired entry, including those no lookup has claimed. | ||
| 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 | ||
| } | ||
|
|
||
| // Validated up front, before the lock is taken, so a batch carrying a bad | ||
| // item writes nothing at all rather than being applied in part. | ||
| for _, item := range items { | ||
| if item.TTL <= 0 { | ||
| return fmt.Errorf("%w: key %q", ErrMissingTTL, item.Key) | ||
| } | ||
| } | ||
|
|
||
| c.mu.Lock() | ||
| defer c.mu.Unlock() | ||
|
|
||
| now := c.now() | ||
| for _, item := range items { | ||
| c.entries[item.Key] = inMemoryEntry{ | ||
| value: bytes.Clone(item.Value), | ||
| expiresAt: now.Add(item.TTL), | ||
| } | ||
| } | ||
|
|
||
| // Everything just stored has a deadline in the future, so a batch never | ||
| // sweeps away its own entries. | ||
| c.sweepExpired(now) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // sweepExpired drops every entry that has expired by now. The caller must hold | ||
| // the mutex. A TTL of d covers [set, set+d), so an entry reaching its deadline | ||
| // exactly is already expired. | ||
| func (c *InMemoryCache) sweepExpired(now time.Time) { | ||
| for key, entry := range c.entries { | ||
| if !now.Before(entry.expiresAt) { | ||
| delete(c.entries, key) | ||
| } | ||
| } | ||
| } | ||
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 error isn't specific to the in memory cache, right? I would define it in the corresponding graphql-go-tools package then and mention in the methods godoc when its returned
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.
moved to graphqlgotools