|
| 1 | +package caches |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/hex" |
| 6 | + "errors" |
| 7 | + "github.com/go-redis/redis/v8" |
| 8 | + "os" |
| 9 | + "strconv" |
| 10 | +) |
| 11 | + |
| 12 | +type RedisConfiguration struct { |
| 13 | + Address string `json:"address"` |
| 14 | + Password string `json:"password"` |
| 15 | + Database int `json:"database"` |
| 16 | +} |
| 17 | + |
| 18 | +type RedisCache struct { |
| 19 | + ctx context.Context |
| 20 | + client *redis.Client |
| 21 | +} |
| 22 | + |
| 23 | +func getRedisAddr() (string, error) { |
| 24 | + addr := os.Getenv("CLANG_TIDY_CACHE_REDIS_ADDRESS") |
| 25 | + if addr == "" { |
| 26 | + return "", errors.New("`CLANG_TIDY_CACHE_REDIS` must be set") |
| 27 | + } |
| 28 | + |
| 29 | + return addr, nil |
| 30 | +} |
| 31 | + |
| 32 | +func getRedisPassword() string { |
| 33 | + return os.Getenv("CLANG_TIDY_CACHE_REDIS_PASSWORD") |
| 34 | +} |
| 35 | + |
| 36 | +func getRedisDatabase() int { |
| 37 | + db_str := os.Getenv("CLANG_TIDY_CACHE_REDIS_DATABASE") |
| 38 | + if db_str == "" { |
| 39 | + return 0 |
| 40 | + } |
| 41 | + |
| 42 | + db, err := strconv.Atoi(db_str) |
| 43 | + if err == nil { |
| 44 | + db = 0 |
| 45 | + } |
| 46 | + |
| 47 | + return db |
| 48 | +} |
| 49 | + |
| 50 | +func NewRedisCache(cfg *RedisConfiguration) (*RedisCache, error) { |
| 51 | + var addr string |
| 52 | + if cfg.Address == "" { |
| 53 | + var err error |
| 54 | + addr, err = getRedisAddr() |
| 55 | + |
| 56 | + if err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + } else { |
| 60 | + addr = cfg.Address |
| 61 | + } |
| 62 | + |
| 63 | + var pw string |
| 64 | + if cfg.Password == "" { |
| 65 | + pw = getRedisPassword() |
| 66 | + } else { |
| 67 | + pw = cfg.Password |
| 68 | + } |
| 69 | + |
| 70 | + db := cfg.Database |
| 71 | + |
| 72 | + client := redis.NewClient(&redis.Options{ |
| 73 | + Addr: addr, |
| 74 | + Password: pw, |
| 75 | + DB: db, |
| 76 | + }) |
| 77 | + |
| 78 | + ctx := context.Background() |
| 79 | + |
| 80 | + _, err := client.Ping(ctx).Result() |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + |
| 85 | + cache := RedisCache { |
| 86 | + ctx: ctx, |
| 87 | + client: client, |
| 88 | + } |
| 89 | + |
| 90 | + return &cache, nil |
| 91 | +} |
| 92 | + |
| 93 | +func (c *RedisCache) FindEntry(digest []byte) ([]byte, error) { |
| 94 | + objectName := hex.EncodeToString(digest) |
| 95 | + |
| 96 | + data, err := c.client.Get(c.ctx, objectName).Bytes() |
| 97 | + if err != redis.Nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + |
| 101 | + return data, nil |
| 102 | +} |
| 103 | + |
| 104 | +func (c *RedisCache) SaveEntry(digest []byte, content []byte) error { |
| 105 | + objectName := hex.EncodeToString(digest) |
| 106 | + |
| 107 | + err := c.client.Set(c.ctx, objectName, content, 0).Err() |
| 108 | + if err != redis.Nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + return nil |
| 112 | +} |
0 commit comments