53 lines
1019 B
Go
53 lines
1019 B
Go
package cache
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"git.secnex.io/secnex/taro-bot/config"
|
|
"github.com/valkey-io/valkey-go"
|
|
)
|
|
|
|
type RedisConfiguration struct {
|
|
Host string
|
|
Port string
|
|
Password string
|
|
}
|
|
|
|
type RedisCache struct {
|
|
Client valkey.Client
|
|
Context context.Context
|
|
}
|
|
|
|
var Cache RedisCache
|
|
|
|
func NewRedisConfiguration(host, port, password string) *RedisConfiguration {
|
|
return &RedisConfiguration{
|
|
Host: host,
|
|
Port: port,
|
|
Password: password,
|
|
}
|
|
}
|
|
|
|
func NewRedisConfigurationFromConfig(config *config.Config) *RedisConfiguration {
|
|
return &RedisConfiguration{
|
|
Host: config.RedisHost,
|
|
Port: config.RedisPort,
|
|
Password: config.RedisPassword,
|
|
}
|
|
}
|
|
|
|
func Connect(config *config.Config) error {
|
|
client, err := valkey.NewClient(valkey.ClientOption{InitAddress: []string{fmt.Sprintf("%s:%s", config.RedisHost, config.RedisPort)}})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ctx := context.Background()
|
|
cache := &RedisCache{
|
|
Client: client,
|
|
Context: ctx,
|
|
}
|
|
Cache = *cache
|
|
return nil
|
|
}
|