Returned StarCityGames support

This commit is contained in:
Aryom Belousov 2020-11-09 15:24:32 +03:00
parent de1187b96c
commit 7c6a6176dc
12 changed files with 133 additions and 72 deletions

49
caching/cache_test.go Normal file
View file

@ -0,0 +1,49 @@
package caching
import (
"fmt"
"testing"
"time"
"github.com/alicebob/miniredis/v2"
"github.com/go-redis/redis"
"github.com/stretchr/testify/assert"
)
func TestGetSet(t *testing.T) {
client, s := getTestClient()
defer s.Close()
keyName := "test_key"
value := "test_value"
client.Set(keyName, value)
val, err := client.Get(keyName)
assert.Nil(t, err)
assert.Equal(t, value, val)
}
func TestExpiration(t *testing.T) {
client, s := getTestClient()
defer s.Close()
client.Expiration = time.Millisecond
keyName := "test_key"
value := "test_value"
client.Set(keyName, value)
s.FastForward(time.Millisecond * 2)
val, err := client.Get(keyName)
assert.Zero(t, val)
assert.NotNil(t, err)
}
func getTestClient() (*CacheClient, *miniredis.Miniredis) {
s, _ := miniredis.Run()
fmt.Println(s.Addr())
c := redis.NewClient(&redis.Options{
Addr: s.Addr(),
})
return &CacheClient{
Storage: c,
Expiration: 0,
}, s
}

View file

@ -6,7 +6,7 @@ import (
)
type CacheClient struct {
storage *redis.Client
Storage *redis.Client
Expiration time.Duration
}
@ -22,7 +22,7 @@ func GetClient() *CacheClient {
}
func (client *CacheClient) Init() {
client.storage = redis.NewClient(&redis.Options{
client.Storage = redis.NewClient(&redis.Options{
Addr: HostName,
Password: Password,
DB: 0,
@ -31,9 +31,9 @@ func (client *CacheClient) Init() {
}
func (client *CacheClient) Set(key string, value string) {
client.storage.Set(key, value, client.Expiration)
client.Storage.Set(key, value, client.Expiration)
}
func (client *CacheClient) Get(key string) (string, error) {
return client.storage.Get(key).Result()
return client.Storage.Get(key).Result()
}