Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

caches: support Redis backend #14

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions caches/redis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package caches

import (
"context"
"encoding/hex"
"errors"
"github.com/go-redis/redis/v8"
"os"
"strconv"
)

type RedisConfiguration struct {
Address string `json:"address"`
Password string `json:"password"`
Database int `json:"database"`
}

type RedisCache struct {
ctx context.Context
client *redis.Client
}

func getRedisAddr() (string, error) {
addr := os.Getenv("CLANG_TIDY_CACHE_REDIS_ADDRESS")
if addr == "" {
return "", errors.New("`CLANG_TIDY_CACHE_REDIS` must be set")
}

return addr, nil
}

func getRedisPassword() string {
return os.Getenv("CLANG_TIDY_CACHE_REDIS_PASSWORD")
}

func getRedisDatabase() int {
db_str := os.Getenv("CLANG_TIDY_CACHE_REDIS_DATABASE")
if db_str == "" {
return 0
}

db, err := strconv.Atoi(db_str)
if err == nil {
db = 0
}

return db
}

func NewRedisCache(cfg *RedisConfiguration) (*RedisCache, error) {
var addr string
if cfg.Address == "" {
var err error
addr, err = getRedisAddr()

if err != nil {
return nil, err
}
} else {
addr = cfg.Address
}

var pw string
if cfg.Password == "" {
pw = getRedisPassword()
} else {
pw = cfg.Password
}

db := cfg.Database

client := redis.NewClient(&redis.Options{
Addr: addr,
Password: pw,
DB: db,
})

ctx := context.Background()

_, err := client.Ping(ctx).Result()
if err != nil {
return nil, err
}

cache := RedisCache {
ctx: ctx,
client: client,
}

return &cache, nil
}

func (c *RedisCache) FindEntry(digest []byte) ([]byte, error) {
objectName := hex.EncodeToString(digest)

data, err := c.client.Get(c.ctx, objectName).Bytes()
if err != redis.Nil {
return nil, err
}

return data, nil
}

func (c *RedisCache) SaveEntry(digest []byte, content []byte) error {
objectName := hex.EncodeToString(digest)

err := c.client.Set(c.ctx, objectName, content, 0).Err()
if err != redis.Nil {
return err
}
return nil
}
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ require (
cloud.google.com/go v0.100.2 // indirect
cloud.google.com/go/compute v0.1.0 // indirect
cloud.google.com/go/iam v0.1.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/go-redis/redis/v8 v8.11.5 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.7 // indirect
Expand All @@ -24,6 +27,5 @@ require (
google.golang.org/grpc v1.40.1 // indirect
google.golang.org/protobuf v1.27.1 // indirect
)
require (
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
)

require github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
Expand All @@ -68,6 +71,8 @@ github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnht
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand All @@ -80,6 +85,8 @@ github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeME
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
Expand Down
13 changes: 11 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import (
const VERSION = "0.3.0"

type Configuration struct {
ClangTidyPath string `json:"clang_tidy_path"`
GcsConfig *caches.GcsConfiguration `json:"gcs,omitempty"`
ClangTidyPath string `json:"clang_tidy_path"`
GcsConfig *caches.GcsConfiguration `json:"gcs,omitempty"`
RedisConfig *caches.RedisConfiguration `json:"redis,omitempty"`
}

func readConfigFile(cfg *Configuration) error {
Expand Down Expand Up @@ -241,6 +242,14 @@ func main() {
}
}

// if no other cache is configured then default to the FS cache
if cfg.RedisConfig != nil {
candidate, err := caches.NewRedisCache(cfg.RedisConfig)
if err == nil {
cache = candidate
}
}

// if no other cache is configured then default to the FS cache
if cache == nil {
cache = caches.NewFsCache()
Expand Down
Loading