Skip to content

Conversation

@mingrammer
Copy link
Contributor

@mingrammer mingrammer commented Nov 30, 2020

Sometimes, we can run an operation that takes longer than the timeout of the incoming request. In this case, the whole request should be canceled along with all subtasks including the cache operations. But current implementation doesn't support passing context, so we can't cancel the cache operations even if the current context is expired.

So I added the passing context feature for all cache operations with this PR. It keeps the backward compatibility, thus does not affect existing code using gomemcache.

import (
    "github.com/bradfitz/gomemcache/memcache"
)
func main() {
    mc := memcache.New("10.0.0.1:11211", "10.0.0.2:11211", "10.0.0.3:11212")
    mc.Set(&memcache.Item{Key: "foo", Value: []byte("my value")})
    it, err := mc.Get("foo")

    // With context
    ctx, cancel := context.WithTimeout(2 * time.Second)
    doLongTimeJob(ctx)
    ...
    it, err = mc.GetWithContext(ctx, "bar")
    ...
}

I would appreciate it if you think about this feature in a good way.

@dormando
Copy link
Collaborator

dormando commented Apr 4, 2025

Note: can't really say if this is the right way to do it or not (sorry my Go is limited). The change is relatively large and there don't seem to be many test additions... so we'll have to give this a think.

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.

4 participants