-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from vinted/hackaton_liu_redis_api
Caching API
- Loading branch information
Showing
6 changed files
with
160 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
"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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, "_") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |