Skip to content

Commit

Permalink
support set hashed and non hashed entries
Browse files Browse the repository at this point in the history
  • Loading branch information
etrpx committed May 29, 2024
1 parent ffae434 commit 5511b7a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 27 deletions.
57 changes: 42 additions & 15 deletions seed/redis/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,36 +70,63 @@ func (r *SeedComponent) seed(ctx context.Context) error {
return err
}

err = client.FlushAll(ctx).Err()
if err != nil {
if err = client.FlushAll(ctx).Err(); err != nil {
return err
}

if err = r.setEntries(ctx, err, client); err != nil {
return err
}

for _, hashData := range r.config.Data {
var count int
err = client.HSet(ctx, hashData.Key, hashData.Fields).Err()
if err = r.HashSetEntries(ctx, err, client); err != nil {
return err
}
r.logInsertions()

return nil
}

func (r *SeedComponent) setEntries(ctx context.Context, err error, client *redis.Client) error {
for _, entry := range r.config.Entries {
err = client.Set(ctx, entry.Key, entry.Value, entry.TTL*time.Second).Err()

if err != nil {
return err
}
}
return nil
}

func (r *SeedComponent) HashSetEntries(ctx context.Context, err error, client *redis.Client) error {
for _, hEntry := range r.config.HEntries {
err = client.HSet(ctx, hEntry.Key, hEntry.Values).Err()

if err != nil {
return err
}
if hashData.TTL > 0 {
err = client.Expire(ctx, hashData.Key, time.Duration(hashData.TTL)*time.Second).Err()
if hEntry.TTL > 0 {
err = client.Expire(ctx, hEntry.Key, hEntry.TTL*time.Second).Err()
if err != nil {
return err
}
}
count = len(hashData.Fields)

r.writer.WriteString(fmt.Sprintf(
"inserted %s fields to %s",
r.writer.Color.Green(strconv.Itoa(count)),
r.writer.Color.Green(hashData.Key),
))
}

return nil
}

func (r *SeedComponent) logInsertions() {
count := len(r.config.Entries)
hashedCount := len(r.config.HEntries)

r.writer.WriteString(fmt.Sprintf(
"inserted %s fields to %s and %s fields to %s",
r.writer.Color.Green(strconv.Itoa(count)),
r.writer.Color.Green("Entries"),
r.writer.Color.Green(strconv.Itoa(hashedCount)),
r.writer.Color.Green("Hashed Entries"),
))
}

func (r *SeedComponent) clientProvider() (*redis.Client, error) {
if r.config.ClientProvider != nil {
return r.config.ClientProvider()
Expand Down
32 changes: 20 additions & 12 deletions seed/redis/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

package redis

import "github.com/go-redis/redis/v8"
import (
"github.com/go-redis/redis/v8"
"time"
)

// SeedConfig represents the configuration for the redis seed component.
type SeedConfig struct {
Expand All @@ -16,18 +19,23 @@ type SeedConfig struct {
// if both ClientProvider and Address are provided, ClientProvider is used.
ClientProvider func() (*redis.Client, error) `json:"-"`

// Data - a list of objects, each represents a redis key and its data
Data []*SeedData `json:"data,omitempty"`
}
// Entries - a list of key-value pairs to set in redis
Entries []*Set `json:"entries,omitempty"`

// SeedData represents data for a redis hash.
type SeedData struct {
// Key - the name of the redis key
Key string `json:"key,omitempty"`
// HEntries - a list of key-value pairs to set in redis hashes
HEntries []*HSet `json:"hentries,omitempty"`
}

// Fields - a map of field names and their values to insert using the redis HSet function:
Fields []string `json:"fields,omitempty"`
// Set Represents a key-value pair to set in redis.
type Set struct {
Key string `json:"key,omitempty"`
Value string `json:"value"`
TTL time.Duration `json:"ttl"`
}

// TTL - the time to live for the key in seconds
TTL int `json:"ttl,omitempty"`
// HSet Represents a key-value pair to set in a redis hash.
type HSet struct {
Key string `json:"key"`
Values map[string]string `json:"values"`
TTL time.Duration `json:"ttl"`
}

0 comments on commit 5511b7a

Please sign in to comment.