-
-
Save elithrar/22e414e067bbf6a5f2e6 to your computer and use it in GitHub Desktop.
Pipeline Redis commands for https://github.com/ulule/limiter/blob/master/store_redis.go#L65 / https://groups.google.com/forum/?fromgroups#!topic/golang-nuts/lK7JSHAmYd0
This file contains 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
// 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