|
| 1 | +package rcache |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "io" |
| 6 | + "net/url" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/shomali11/xredis" |
| 12 | +) |
| 13 | + |
| 14 | +type logger interface { |
| 15 | + SetOutput(out io.Writer) |
| 16 | + Print(args ...interface{}) |
| 17 | + Printf(format string, args ...interface{}) |
| 18 | + Println(args ...interface{}) |
| 19 | + Info(args ...interface{}) |
| 20 | + Infof(format string, args ...interface{}) |
| 21 | + Infoln(args ...interface{}) |
| 22 | + Warn(args ...interface{}) |
| 23 | + Warnf(format string, args ...interface{}) |
| 24 | + Warnln(args ...interface{}) |
| 25 | + Error(args ...interface{}) |
| 26 | + Errorf(format string, args ...interface{}) |
| 27 | + Errorln(args ...interface{}) |
| 28 | +} |
| 29 | + |
| 30 | +func New(redisURL string, logger logger) *Cache { |
| 31 | + opts := &xredis.Options{ |
| 32 | + Host: "localhost", |
| 33 | + Port: 6379, |
| 34 | + Password: "", // no password set |
| 35 | + // DB: 0, // use default DB |
| 36 | + } |
| 37 | + |
| 38 | + if redisURL != "" { |
| 39 | + opts = &xredis.Options{} |
| 40 | + u, err := url.Parse(redisURL) |
| 41 | + if err != nil { |
| 42 | + logger.Error(err) |
| 43 | + return nil |
| 44 | + } |
| 45 | + opts.Host = u.Host |
| 46 | + if strings.Contains(opts.Host, ":") { |
| 47 | + opts.Host = strings.Split(opts.Host, ":")[0] |
| 48 | + } |
| 49 | + p, _ := u.User.Password() |
| 50 | + opts.Password = p |
| 51 | + // opts.User = u.User.Username() |
| 52 | + port, err := strconv.Atoi(u.Port()) |
| 53 | + if err != nil { |
| 54 | + logger.Error("cache couldn't parse port") |
| 55 | + return nil |
| 56 | + } |
| 57 | + opts.Port = port |
| 58 | + } |
| 59 | + |
| 60 | + logger.Info("trying cache", opts) |
| 61 | + client := xredis.SetupClient(opts) |
| 62 | + pong, err := client.Ping() |
| 63 | + if err != nil { |
| 64 | + logger.Error(err) |
| 65 | + return nil |
| 66 | + } |
| 67 | + |
| 68 | + logger.Info("cache running", pong) |
| 69 | + return &Cache{ |
| 70 | + Client: client, |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +type Cache struct { |
| 75 | + Client *xredis.Client |
| 76 | +} |
| 77 | + |
| 78 | +func (cache *Cache) Get(key string) (string, error) { |
| 79 | + val, ok, err := cache.Client.Get(key) |
| 80 | + if val == "" { |
| 81 | + return "", errors.New("no value for [" + key + "]") |
| 82 | + } |
| 83 | + if !ok && err == nil { |
| 84 | + return "", errors.New("Not found") |
| 85 | + } |
| 86 | + return val, err |
| 87 | +} |
| 88 | + |
| 89 | +func (cache *Cache) Del(key string) error { |
| 90 | + _, err := cache.Client.Expire(key, 1) |
| 91 | + return err |
| 92 | +} |
| 93 | + |
| 94 | +func (cache *Cache) Expire(key string) error { |
| 95 | + err := cache.Del(key) |
| 96 | + return err |
| 97 | +} |
| 98 | + |
| 99 | +func (cache *Cache) GetBytes(key string) ([]byte, error) { // Encourages BLOATED interfaces |
| 100 | + val, err := cache.Get(key) |
| 101 | + if err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + return []byte(val), nil |
| 105 | +} |
| 106 | + |
| 107 | +func (cache *Cache) Set(key string, value string, duration time.Duration) error { |
| 108 | + secs := int64(duration / time.Second) |
| 109 | + ok, err := cache.Client.SetEx(key, value, secs) |
| 110 | + if !ok { |
| 111 | + if err == nil { |
| 112 | + return errors.New("Not found") |
| 113 | + } |
| 114 | + if strings.Contains(err.Error(), "invalid expire time in set") { |
| 115 | + return errors.New("Invalid expire timeout in seconds [" + strconv.Itoa(int(secs)) + "]") |
| 116 | + } |
| 117 | + } |
| 118 | + return err |
| 119 | +} |
| 120 | + |
| 121 | +func (cache *Cache) SetBytes(key string, value []byte, duration time.Duration) error { // Encourages BLOATED interfaces |
| 122 | + result := string(value[:]) |
| 123 | + err := cache.Set(key, result, duration) |
| 124 | + return err |
| 125 | +} |
| 126 | + |
| 127 | +func (cache *Cache) FlushDB() error { |
| 128 | + return cache.Client.FlushDb() |
| 129 | +} |
0 commit comments