-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
448 changed files
with
428 additions
and
188,195 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
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,60 @@ | ||
package providers | ||
|
||
import ( | ||
t "github.com/darkweak/souin/configurationtypes" | ||
redis "github.com/go-redis/redis/v8" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
// Redis provider type | ||
type Redis struct { | ||
*redis.Client | ||
t.AbstractConfigurationInterface | ||
} | ||
|
||
// RedisConnectionFactory function create new Redis instance | ||
func RedisConnectionFactory(configuration t.AbstractConfigurationInterface) (*Redis, error) { | ||
return &Redis{ | ||
redis.NewClient(&redis.Options{ | ||
Addr: configuration.GetDefaultCache().Redis.URL, | ||
DB: 0, | ||
Password: "", | ||
}), | ||
configuration, | ||
}, nil | ||
} | ||
|
||
// Get method returns the populated response if exists, empty response then | ||
func (provider *Redis) Get(key string) []byte { | ||
val2, err := provider.Client.Get(provider.Context(), key).Result() | ||
|
||
if err != nil { | ||
return []byte{} | ||
} | ||
|
||
return []byte(val2) | ||
} | ||
|
||
// Set method will store the response in Redis provider | ||
func (provider *Redis) Set(key string, value []byte, url t.URL, duration time.Duration) { | ||
if duration == 0 { | ||
ttl, _ := strconv.Atoi(url.TTL) | ||
duration = time.Duration(ttl)*time.Second | ||
} | ||
|
||
err := provider.Client.Set(provider.Context(), key, string(value), duration).Err() | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
// Delete method will delete the response in Redis provider if exists corresponding to key param | ||
func (provider *Redis) Delete(key string) { | ||
provider.Do(provider.Context(), "del", key) | ||
} | ||
|
||
// Init method will | ||
func (provider *Redis) Init() error { | ||
return nil | ||
} |
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,75 @@ | ||
package providers | ||
|
||
import ( | ||
"fmt" | ||
"github.com/darkweak/souin/cache/types" | ||
"github.com/darkweak/souin/tests" | ||
"testing" | ||
"time" | ||
|
||
"github.com/darkweak/souin/configurationtypes" | ||
"github.com/darkweak/souin/errors" | ||
) | ||
|
||
const REDISVALUE = "My first data" | ||
|
||
func getRedisClientAndMatchedURL(key string) (types.AbstractProviderInterface, configurationtypes.URL) { | ||
return tests.GetCacheProviderClientAndMatchedURL(key, func(configurationInterface configurationtypes.AbstractConfigurationInterface) (types.AbstractProviderInterface, error) { | ||
provider, _ := RedisConnectionFactory(configurationInterface) | ||
|
||
return provider, nil | ||
}) | ||
} | ||
|
||
func TestIShouldBeAbleToReadAndWriteDataInRedis(t *testing.T) { | ||
client, u := getRedisClientAndMatchedURL("Test") | ||
client.Set("Test", []byte(REDISVALUE), u, time.Duration(10)*time.Second) | ||
res := client.Get("Test") | ||
if REDISVALUE != string(res) { | ||
errors.GenerateError(t, fmt.Sprintf("%s not corresponding to %s", res, REDISVALUE)) | ||
} | ||
} | ||
|
||
func TestRedis_GetRequestInCache(t *testing.T) { | ||
client, _ := getRedisClientAndMatchedURL(NONEXISTENTKEY) | ||
res := client.Get(NONEXISTENTKEY) | ||
if string(res) != "" { | ||
errors.GenerateError(t, fmt.Sprintf("Key %s should not exist", NONEXISTENTKEY)) | ||
} | ||
} | ||
|
||
func TestRedis_SetRequestInCache_OneByte(t *testing.T) { | ||
client, u := getRedisClientAndMatchedURL(BYTEKEY) | ||
client.Set(BYTEKEY, []byte{65}, u, time.Duration(20) * time.Second) | ||
} | ||
|
||
func TestRedis_SetRequestInCache_TTL(t *testing.T) { | ||
key := "MyEmptyKey" | ||
client, matchedURL := getRedisClientAndMatchedURL(key) | ||
nv := []byte("Hello world") | ||
setValueThenVerify(client, key, nv, matchedURL, time.Duration(20) * time.Second, t) | ||
} | ||
|
||
func TestRedis_SetRequestInCache_NoTTL(t *testing.T) { | ||
client, matchedURL := getRedisClientAndMatchedURL(BYTEKEY) | ||
nv := []byte("New value") | ||
setValueThenVerify(client, BYTEKEY, nv, matchedURL, 0, t) | ||
} | ||
|
||
func TestRedis_DeleteRequestInCache(t *testing.T) { | ||
client, _ := RedisConnectionFactory(tests.MockConfiguration()) | ||
client.Delete(BYTEKEY) | ||
time.Sleep(1 * time.Second) | ||
if 0 < len(client.Get(BYTEKEY)) { | ||
errors.GenerateError(t, fmt.Sprintf("Key %s should not exist", BYTEKEY)) | ||
} | ||
} | ||
|
||
func TestRedis_Init(t *testing.T) { | ||
client, _ := RedisConnectionFactory(tests.MockConfiguration()) | ||
err := client.Init() | ||
|
||
if nil != err { | ||
errors.GenerateError(t, "Impossible to init Redis provider") | ||
} | ||
} |
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
Oops, something went wrong.