package main import ( "fmt" "github.com/gomodule/redigo/redis" "time" ) /** newPool builds redis database connection @param host string host:port of redis server returns redis.Connection of connection to database returns err from database connection */ func newPool(host string, username string, password string) *redis.Pool { return &redis.Pool{ MaxIdle: 12000, MaxActive: 12000, Dial: func() (redis.Conn, error) { c, err := redis.Dial("tcp", host) if err != nil { panic(err.Error()) } if username != "" && password != "" { _, err = c.Do("AUTH", password) if err != nil { c.Close() panic(err.Error()) } } return c, err }, TestOnBorrow: func(c redis.Conn, t time.Time) error { _, err := c.Do("PING") return err }, } } /** setRateTest sets key/value into database @param rdbPtr connection is a pointer to the redis connection @param key int redis key for the storage @param value string string for the value to store */ func setRedis(rdbPtr *redis.Conn, key int, value string) { rdb := *rdbPtr //defer waitGroup.Done() _, err := rdb.Do("Set", key, value) if err != nil { fmt.Println(err, key, value) panic(err) } } /** buildTestData iterates through a goroutine of setRedis to build test data into the redis db @param redisPtr connection to redis @param size int number of entries into the database */ func buildTestData(rdbPtr *redis.Conn, size int) { fmt.Println("Initializing Test Data...") for i := 1; i < size; i++ { key := i value := "ThisIsATestStringThatShouldBeReplacedWithSomethingMoreRandomMaybeFromAnInputOrCSV" setRedis(rdbPtr, key, value) } fmt.Println("Database Initialized...") } /** getRedis queries the redis database for value of key @param rdbPtr connection is a pointer to the redis connection @param key int is key value to query in database */ func getRedis(rdbPtr *redis.Conn, key int, timesChan chan time.Duration, errorChan chan bool) { rdb := *rdbPtr startTime := time.Now() _, err := rdb.Do("Get", key) responseTime := time.Now().Sub(startTime) if err != nil { errorChan <- true fmt.Println("Unhandled error:", err) } timesChan <- responseTime }