Skip to content

Instantly share code, notes, and snippets.

@wicoady1
Last active April 1, 2018 10:10
Show Gist options
  • Save wicoady1/d85ec8accd0513ce6d1ae43f0b6360a4 to your computer and use it in GitHub Desktop.
Save wicoady1/d85ec8accd0513ce6d1ae43f0b6360a4 to your computer and use it in GitHub Desktop.
//check https://github.com/wicoady1/redissimulation/blob/master/handler/service.response.go for full code
//validateResponse validate response, hold multiple / spam response with redis
func (m *Module) validateResponse(input string) error {
//let's use "anti-spam:test" key as redis key
key := fmt.Sprintf("anti-spam:%s", input)
//check key, is it exist?
value, err := m.Redis.Get(key)
if err != nil {
log.Fatal(err)
}
//value not nil, means the redis is already exists
//therefore return error
if value != nil {
return fmt.Errorf("Value Exists, Please Wait")
}
//if there is no redis key
//set new key with 10 second expiration time
err = m.Redis.Setex(key, 10, "1")
if err != nil {
return err
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment