github.com/willyham/dosa@v2.3.1-0.20171024181418-1e446d37ee71+incompatible/connectors/redis/redigo.go (about) 1 // Copyright (c) 2017 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package redis 22 23 import ( 24 "fmt" 25 "time" 26 27 "github.com/garyburd/redigo/redis" 28 "github.com/uber-go/dosa" 29 ) 30 31 // NewRedigoClient returns a redigo implementation of SimpleRedis 32 func NewRedigoClient(config ServerConfig) SimpleRedis { 33 c := &simpleRedis{config: config} 34 c.pool = &redis.Pool{ 35 MaxActive: config.MaxActive, 36 MaxIdle: config.MaxIdle, 37 IdleTimeout: config.IdleTimeout, 38 Dial: func() (redis.Conn, error) { 39 c, err := redis.Dial( 40 "tcp", 41 c.getURL(), 42 redis.DialConnectTimeout(config.ConnectTimeout), 43 redis.DialReadTimeout(config.ReadTimeout), 44 redis.DialWriteTimeout(config.WriteTimeout)) 45 if err != nil { 46 return nil, err 47 } 48 return c, err 49 }, 50 Wait: false, 51 } 52 return c 53 } 54 55 type simpleRedis struct { 56 config ServerConfig 57 pool *redis.Pool 58 } 59 60 func (c *simpleRedis) getURL() string { 61 return fmt.Sprintf("%s:%d", c.config.Host, c.config.Port) 62 } 63 64 // Get returns an error if the key is not found in cache 65 func (c *simpleRedis) Get(key string) ([]byte, error) { 66 bytes, err := redis.Bytes(c.do("GET", key)) 67 if err == redis.ErrNil { 68 err = &dosa.ErrNotFound{} 69 } 70 return bytes, err 71 } 72 73 func (c *simpleRedis) SetEx(key string, value []byte, ttl time.Duration) error { 74 _, err := c.do("SET", key, value, "EX", ttl.Seconds()) 75 return err 76 } 77 78 func (c *simpleRedis) Del(key string) error { 79 _, err := c.do("DEL", key) 80 return err 81 } 82 83 // Shutdown closes the underlying connection pool to redis 84 func (c *simpleRedis) Shutdown() error { 85 return c.pool.Close() 86 } 87 88 // Do is a proxy method that calls Redigo's `Do` method and returns its output. It remembers 89 // to close connections taken from the pool 90 func (c *simpleRedis) do(commandName string, args ...interface{}) (interface{}, error) { 91 conn := c.pool.Get() 92 defer func() { _ = conn.Close() }() 93 return conn.Do(commandName, args...) 94 }