-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
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 GitHub Actions / cluster initial sharding multi
Check failure on line 6 in go/cache/redis/cache.go GitHub Actions / cluster initial sharding multi
Check failure on line 6 in go/cache/redis/cache.go GitHub Actions / cluster initial sharding multi
Check failure on line 6 in go/cache/redis/cache.go GitHub Actions / Docker Test 3
Check failure on line 6 in go/cache/redis/cache.go GitHub Actions / Docker Test 3
Check failure on line 6 in go/cache/redis/cache.go GitHub Actions / Docker Test 3
Check failure on line 6 in go/cache/redis/cache.go GitHub Actions / cluster initial sharding multi
Check failure on line 6 in go/cache/redis/cache.go GitHub Actions / cluster initial sharding multi
Check failure on line 6 in go/cache/redis/cache.go GitHub Actions / cluster initial sharding multi
Check failure on line 6 in go/cache/redis/cache.go GitHub Actions / Docker Test 3
Check failure on line 6 in go/cache/redis/cache.go GitHub Actions / Docker Test 3
Check failure on line 6 in go/cache/redis/cache.go GitHub Actions / Docker Test 3
Check failure on line 6 in go/cache/redis/cache.go GitHub Actions / Docker Test 1
Check failure on line 6 in go/cache/redis/cache.go GitHub Actions / Docker Test 1
Check failure on line 6 in go/cache/redis/cache.go GitHub Actions / Docker Test 1
Check failure on line 6 in go/cache/redis/cache.go GitHub Actions / Docker Test 1
Check failure on line 6 in go/cache/redis/cache.go GitHub Actions / Docker Test 1
Check failure on line 6 in go/cache/redis/cache.go GitHub Actions / Docker Test 1
Check failure on line 6 in go/cache/redis/cache.go GitHub Actions / Docker Test 1
Check failure on line 6 in go/cache/redis/cache.go GitHub Actions / Docker Test 1
Check failure on line 6 in go/cache/redis/cache.go GitHub Actions / Docker Test 2
Check failure on line 6 in go/cache/redis/cache.go GitHub Actions / Docker Test 2
Check failure on line 6 in go/cache/redis/cache.go GitHub Actions / Docker Test 2
Check failure on line 6 in go/cache/redis/cache.go GitHub Actions / Docker Test 2
Check failure on line 6 in go/cache/redis/cache.go GitHub Actions / Docker Test 2
Check failure on line 6 in go/cache/redis/cache.go GitHub Actions / Docker Test 2
Check failure on line 6 in go/cache/redis/cache.go GitHub Actions / Docker Test 2
|
||
"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() | ||
} |
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) | ||
} | ||
} |
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, "_") | ||
} |
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() | ||
} | ||
} |