repositorypackage
0.5.5
Repository: https://github.com/coopnorge/go-redis-facade.git
Documentation: pkg.go.dev
# README
Go Redis Facade
Coop Redis Facade wraps simple interaction with Redis clients for CRUD operations by preventing race conditions between multiple client instances against singular instances of Redis.
If you are interested in how Sync between clients works, take a look at this post.
Installation
$ go get -u github.com/coopnorge/go-redis-facade
Quick Start
Add this import line to the file you're working in:
import "github.com/coopnorge/go-redis-facade"
We recommend create custom constructor.
func NewRedisStorageFacade(cfg *config.MyAppConfig) *database.RedisFacade {
// ...
}
Prepare configuration for Redis connection
redisCfg := database.Config{
Address: "RedisAddress:RedisPort",
Password: "RedisPassword",
Database: "RedisDatabase",
DialTimeout: "RedisDialTimeout",
ReadTimeout: "RedisReadTimeout",
WriteTimeout: "RedisWriteTimeout",
MaxRetries: "RedisMaxRetries",
PoolSize: "RedisPoolSize",
PoolTimeout: "RedisPoolTimeout",
EncryptionEnabled: "RedisEncryptorEnabled",
}
encrCfg := database.EncryptionConfig{
RedisKeyURI: "RedisEncryptorKeyURI",
Aad: []byte("RedisEncryptorAad"),
}
Create Redis wrapper instances
encryptor, encryptorErr := database.NewEncryptionClient(encryptionConfig)
if encryptorErr != nil {
panic(fmt.Errorf("unable to create new encryption client for Redis Client, error: %w", encryptorErr))
}
redFacade, redFacadeErr := database.NewRedisFacade(redisConfig, encryptor)
if redFacadeErr != nil {
panic(fmt.Errorf("unable to create Redis Client, error: %w", redFacadeErr))
}
Use case
Then you can add client to your repository to work with Redis
func NewUserRepository(s database.KeyValueStorage) *UserRepository {
return &UserRepository{db: s}
}
func (r *UserRepository) Create(ctx context.Context, u model.User) error {
u.ID = uuid.NewUUID()
u.CreatedAt = time.Now()
j, jErr := json.Marshal(cart)
if jErr != nil {
return jErr
}
return r.db.Save(ctx, u.ID, string(j), expirationTime)
}
Mocks
To generate or update mocks use tools Eitri or use directly Mockhandler