Created
September 17, 2018 02:09
-
-
Save ak1t0/023187c5f9c965ec9f6ffead4c0002d7 to your computer and use it in GitHub Desktop.
redigo snippet for ISUCON
This file contains hidden or 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
// global variable for redis connection pool | |
var server = "localhost:6379" | |
var Pool = redis.Pool{ | |
MaxIdle: 3, | |
IdleTimeout: 240 * time.Second, | |
Dial: func() (redis.Conn, error) { | |
c, err := redis.Dial("tcp", server) | |
if err != nil { | |
return nil, err | |
} | |
return c, err | |
}, | |
TestOnBorrow: func(c redis.Conn, t time.Time) error { | |
_, err := c.Do("PING") | |
return err | |
}, | |
} | |
// cache to redis alternative to haveread table | |
rconn := Pool.Get() | |
defer rconn.Close() | |
latestID := messages[0].ID | |
rconn.Do("SET", fmt.Sprintf("u%dc%d", userID, chanID), fmt.Sprintf("%d", latestID)) | |
rconn := Pool.Get() | |
defer rconn.Close() | |
id, _ := redis.Int64(rconn.Do("GET", fmt.Sprintf("u%dc%d", userID, chID))) | |
if id != 0 { | |
return id, nil | |
} else { | |
return 0, nil | |
} | |
// redis init | |
rconn := Pool.Get() | |
defer rconn.Close() | |
rconn.Do("flushdb") | |
rconn.Do("SET", "flushed", "yeah") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment