Skip to content

Commit

Permalink
feat: implement Redis cache client (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
flavioltonon authored May 5, 2023
1 parent e33d5c8 commit 954e93e
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions infrastructure/repository/redis/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package redis

import (
"context"
"time"

ozzo "github.com/go-ozzo/ozzo-validation/v4"
"github.com/redis/go-redis/v9"
)

// Cache is a Redis cache
type Cache struct {
client *redis.Client
options *Options
}

type Options struct {
Address string
ConnectionTimeout time.Duration
}

func (o Options) Validate() error {
return ozzo.ValidateStruct(&o,
ozzo.Field(&o.Address, ozzo.Required),
ozzo.Field(&o.ConnectionTimeout, ozzo.Required, ozzo.Min(1*time.Second)),
)
}

// NewCache creates a new Cache
func NewCache(options *Options) (*Cache, error) {
if err := options.Validate(); err != nil {
return nil, err
}

client := redis.NewClient(&redis.Options{
Addr: options.Address,
})

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

if err := client.Ping(ctx).Err(); err != nil {
return nil, err
}

return &Cache{
client: client,
options: options,
}, nil
}

0 comments on commit 954e93e

Please sign in to comment.