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

Caching API #9

Merged
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
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/armon/go-metrics v0.0.0-20190430140413-ec5e00d3c878 // indirect
github.com/aws/aws-sdk-go v1.28.8
github.com/buger/jsonparser v0.0.0-20200322175846-f7e751efca13
github.com/cespare/xxhash/v2 v2.1.1
github.com/cespare/xxhash/v2 v2.2.0
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd // indirect
github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0 // indirect
github.com/corpix/uarand v0.1.1 // indirect
Expand Down Expand Up @@ -77,6 +77,7 @@ require (
github.com/prometheus/client_golang v1.11.0
github.com/prometheus/common v0.29.0 // indirect
github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0
github.com/redis/go-redis/v9 v9.5.1
github.com/samuel/go-zookeeper v0.0.0-20200724154423-2164a8ac840e
github.com/satori/go.uuid v1.2.0 // indirect
github.com/sjmudd/stopwatch v0.0.0-20170613150411-f380bf8a9be1
Expand Down
11 changes: 10 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,18 @@ github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJm
github.com/blang/semver v3.5.0+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ=
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/buger/jsonparser v0.0.0-20200322175846-f7e751efca13 h1:+qUNY4VRkEH46bLUwxCyUU+iOGJMQBVibAaYzWiwWcg=
github.com/buger/jsonparser v0.0.0-20200322175846-f7e751efca13/go.mod h1:tgcrVJ81GPSF0mz+0nu1Xaz0fazGPrmmJfJtxjbHhUQ=
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.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/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 Down Expand Up @@ -176,6 +181,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
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/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/docker/docker v0.7.3-0.20190327010347-be7ac8be2ae0/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
Expand Down Expand Up @@ -639,6 +646,8 @@ github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0 h1:MkV+77GLUNo5oJ0jf870itWm3D0Sjh7+Za9gazKc5LQ=
github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/redis/go-redis/v9 v9.5.1 h1:H1X4D3yHPaYrkL5X06Wh6xNVM/pX0Ft4RV0vMGvLBh8=
github.com/redis/go-redis/v9 v9.5.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/remyoudompheng/bigfft v0.0.0-20170806203942-52369c62f446/go.mod h1:uYEyJGbgTkfkS4+E/PavXkNJcbFIpEtjt2B0KDQ5+9M=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
Expand Down
50 changes: 50 additions & 0 deletions go/cache/redis/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package redis

import (
"time"

gredis "github.com/redis/go-redis/v9"

Check failure on line 6 in go/cache/redis/cache.go

View workflow job for this annotation

GitHub Actions / cluster initial sharding multi

//go:build comment without // +build comment

Check failure on line 6 in go/cache/redis/cache.go

View workflow job for this annotation

GitHub Actions / cluster initial sharding multi

//go:build comment without // +build comment

Check failure on line 6 in go/cache/redis/cache.go

View workflow job for this annotation

GitHub Actions / cluster initial sharding multi

//go:build comment without // +build comment

Check failure on line 6 in go/cache/redis/cache.go

View workflow job for this annotation

GitHub Actions / Docker Test 3

//go:build comment without // +build comment

Check failure on line 6 in go/cache/redis/cache.go

View workflow job for this annotation

GitHub Actions / Docker Test 3

//go:build comment without // +build comment

Check failure on line 6 in go/cache/redis/cache.go

View workflow job for this annotation

GitHub Actions / Docker Test 3

//go:build comment without // +build comment

Check failure on line 6 in go/cache/redis/cache.go

View workflow job for this annotation

GitHub Actions / cluster initial sharding multi

//go:build comment without // +build comment

Check failure on line 6 in go/cache/redis/cache.go

View workflow job for this annotation

GitHub Actions / cluster initial sharding multi

//go:build comment without // +build comment

Check failure on line 6 in go/cache/redis/cache.go

View workflow job for this annotation

GitHub Actions / cluster initial sharding multi

//go:build comment without // +build comment

Check failure on line 6 in go/cache/redis/cache.go

View workflow job for this annotation

GitHub Actions / Docker Test 3

//go:build comment without // +build comment

Check failure on line 6 in go/cache/redis/cache.go

View workflow job for this annotation

GitHub Actions / Docker Test 3

//go:build comment without // +build comment

Check failure on line 6 in go/cache/redis/cache.go

View workflow job for this annotation

GitHub Actions / Docker Test 3

//go:build comment without // +build comment

Check failure on line 6 in go/cache/redis/cache.go

View workflow job for this annotation

GitHub Actions / Docker Test 1

//go:build comment without // +build comment

Check failure on line 6 in go/cache/redis/cache.go

View workflow job for this annotation

GitHub Actions / Docker Test 1

//go:build comment without // +build comment

Check failure on line 6 in go/cache/redis/cache.go

View workflow job for this annotation

GitHub Actions / Docker Test 1

//go:build comment without // +build comment

Check failure on line 6 in go/cache/redis/cache.go

View workflow job for this annotation

GitHub Actions / Docker Test 1

//go:build comment without // +build comment

Check failure on line 6 in go/cache/redis/cache.go

View workflow job for this annotation

GitHub Actions / Docker Test 1

//go:build comment without // +build comment

Check failure on line 6 in go/cache/redis/cache.go

View workflow job for this annotation

GitHub Actions / Docker Test 1

//go:build comment without // +build comment

Check failure on line 6 in go/cache/redis/cache.go

View workflow job for this annotation

GitHub Actions / Docker Test 1

//go:build comment without // +build comment

Check failure on line 6 in go/cache/redis/cache.go

View workflow job for this annotation

GitHub Actions / Docker Test 1

//go:build comment without // +build comment

Check failure on line 6 in go/cache/redis/cache.go

View workflow job for this annotation

GitHub Actions / Docker Test 2

//go:build comment without // +build comment

Check failure on line 6 in go/cache/redis/cache.go

View workflow job for this annotation

GitHub Actions / Docker Test 2

//go:build comment without // +build comment

Check failure on line 6 in go/cache/redis/cache.go

View workflow job for this annotation

GitHub Actions / Docker Test 2

//go:build comment without // +build comment

Check failure on line 6 in go/cache/redis/cache.go

View workflow job for this annotation

GitHub Actions / Docker Test 2

//go:build comment without // +build comment

Check failure on line 6 in go/cache/redis/cache.go

View workflow job for this annotation

GitHub Actions / Docker Test 2

//go:build comment without // +build comment

Check failure on line 6 in go/cache/redis/cache.go

View workflow job for this annotation

GitHub Actions / Docker Test 2

//go:build comment without // +build comment

Check failure on line 6 in go/cache/redis/cache.go

View workflow job for this annotation

GitHub Actions / Docker Test 2

//go:build comment without // +build comment

Check failure on line 6 in go/cache/redis/cache.go

View workflow job for this annotation

GitHub Actions / Docker Test 2

//go:build comment without // +build comment
"golang.org/x/net/context"
)

const defaultTimeout = 15 * time.Second
const defaultRecordTtl = 5 * time.Minute

type Cache struct {
client *gredis.Client
}

func NewCache() *Cache {
opts := &gredis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
}

client := gredis.NewClient(opts)

return &Cache{
client: client,
}
}

func (c *Cache) Get(key string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()

return c.client.Get(ctx, key).Result()
}

func (c *Cache) Set(key, value string) error {
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()

return c.client.Set(ctx, key, value, defaultRecordTtl).Err()
}

func (c *Cache) Delete(key ...string) error {
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()

return c.client.Del(ctx, key...).Err()
}
74 changes: 74 additions & 0 deletions go/cache/redis/cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package redis_test

import (
"testing"

"vitess.io/vitess/go/cache/redis"
)

func Test_RedisStringSet(t *testing.T) {
t.Skip("E2E not set up")

cache := redis.NewCache()

if err := cache.Set("hi", "mom"); err != nil {
t.Error(err)
}

val, err := cache.Get("hi")
if err != nil {
t.Error(err)
}

if val != "mom" {
t.Error("Value is not \"mom\"")
}
}

func Test_RedisStringSetAndDel(t *testing.T) {
t.Skip("E2E not set up")

cache := redis.NewCache()

if err := cache.Set("hi", "mom"); err != nil {
t.Error(err)
}

val, err := cache.Get("hi")
if err != nil || len(val) == 0 {
t.Error(err)
}

if err = cache.Delete("hi"); err != nil {
t.Error(err)
}

val_ex, err := cache.Get("hi")
if err == nil || len(val_ex) != 0 {
t.Error(err)
}
}

func Test_RedisOperationsWithKeyGen(t *testing.T) {
t.Skip("E2E not set up")

cache := redis.NewCache()

cols := []string{"id", "user_id"}
vtgs := []string{"1323", "4362"}

key := redis.GenerateCacheKey(append(cols, vtgs...)...)

if err := cache.Set(key, "mom"); err != nil {
t.Error(err)
}

val, err := cache.Get(key)
if err != nil || len(val) == 0 {
t.Error(err)
}

if err = cache.Delete(key); err != nil {
t.Error(err)
}
}
7 changes: 7 additions & 0 deletions go/cache/redis/keygen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package redis

import "strings"

func GenerateCacheKey(args ...string) string {
return strings.Join(args, "_")
}
17 changes: 17 additions & 0 deletions go/cache/redis/keygen_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package redis_test

import (
"strings"
"testing"

"vitess.io/vitess/go/cache/redis"
)

func Test_GenerateCacheKey_GeneratesACacheKey(t *testing.T) {
expected := "id_user_id_4_5"
key := redis.GenerateCacheKey("id", "user_id", "4", "5")

if strings.Compare(expected, key) != 0 {
t.Error()
}
}
Loading