Skip to content

Instantly share code, notes, and snippets.

@elithrar
Last active October 6, 2015 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elithrar/22e414e067bbf6a5f2e6 to your computer and use it in GitHub Desktop.
Save elithrar/22e414e067bbf6a5f2e6 to your computer and use it in GitHub Desktop.
// Will return 0 (no-op) if the key already exists
c.Send("SETNX", "key", 1)
// Will return integer 0 if the key doesn’t exist
c.Send("EXPIRE", rate.Period.Seconds())
c.Flush()
exists, err := redis.Bool(c.Receive())
if err != nil {
return ctx, err
}
// We can ignore this: the value of exists tells us what we need.
// (just need to flush the value out of the buffer)
_, err := c.Receive
if err != nil {
return ctx, err
}
if !exists {
return Context{
// ...
}
}
// In addition, for keys already existing, we need to INCR the value
c.Send("INCR", key)
c.Send("TTL", key)
c.Flush()
count, err := redis.Int64(c.Receive())
if err != nil {
return ctx, err
}
ttl, err := redis.Int64(c.Receive())
if err != nil {
return ctx, err
}
// Check count and TTL as per existing code
// ...
// ...
return Context{
// ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment