Skip to content

Commit

Permalink
Merge pull request #30 from Sovietaced/docs
Browse files Browse the repository at this point in the history
Force namespace for Map and convert implementation to use Redis hash
  • Loading branch information
Sovietaced authored Dec 21, 2023
2 parents 0dc5a8c + b4643a7 commit 64d3d46
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ The `Map` struct aims to provide similar semantics to a native Go map.
```go
ctx := context.Background()
client := redis.NewClient(&redis.Options{Addr: endpoint})
m := mapp.NewMap(client)
m := mapp.NewMap(client, "my-namespace")
err = m.Set(ctx, "key", "value")
value, exists, err := m.Get(ctx, "key")
```
Expand Down
21 changes: 5 additions & 16 deletions mapp/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ func defaultOptions[K any, V any]() *Options[K, V] {
// Option for the Map
type Option[K any, V any] func(*Options[K, V])

// WithNamespace adds a namespace to the map keyspace. This is highly recommended.
func WithNamespace[K any, V any](namespace string) Option[K, V] {
return func(mo *Options[K, V]) {
mo.namespace = namespace
}
}

// WithKeyMarshaler allows you to configure how keys are marshaled into strings.
func WithKeyMarshaler[K any, V any](marshaler marshal.Marshaler[K]) Option[K, V] {
return func(mo *Options[K, V]) {
Expand All @@ -54,13 +47,13 @@ type Map[K any, V any] struct {
}

// NewMap creates a new generic map. Uses JSON key/value marshalers by default.
func NewMap[K any, V any](client redis.UniversalClient, options ...Option[K, V]) *Map[K, V] {
func NewMap[K any, V any](client redis.UniversalClient, namespace string, options ...Option[K, V]) *Map[K, V] {
opts := defaultOptions[K, V]()
for _, option := range options {
option(opts)
}

return &Map[K, V]{client: client, keyMarshaler: opts.keyMarshaler, valueMarshaler: opts.valueMarshaler, namespace: opts.namespace}
return &Map[K, V]{client: client, namespace: namespace, keyMarshaler: opts.keyMarshaler, valueMarshaler: opts.valueMarshaler}
}

// Get retrieves a key/value from the map. Returns the potential value, whether it exists, and any errors that occurred.
Expand All @@ -70,7 +63,7 @@ func (c *Map[K, V]) Get(ctx context.Context, key K) (V, bool, error) {
return *new(V), false, fmt.Errorf("computing key: %w", err)
}

result := c.client.Get(ctx, keyString)
result := c.client.HGet(ctx, c.namespace, keyString)
if result.Err() != nil {
// Missing key
if result.Err() == redis.Nil {
Expand Down Expand Up @@ -99,7 +92,7 @@ func (c *Map[K, V]) Set(ctx context.Context, key K, value V) error {
return fmt.Errorf("marshalling value: %w", err)
}

result := c.client.Set(ctx, keyString, marshaledValue, 0)
result := c.client.HSet(ctx, c.namespace, keyString, marshaledValue)
if result.Err() != nil {
return fmt.Errorf("setting key=%s: %w", keyString, err)
}
Expand All @@ -115,7 +108,7 @@ func (c *Map[K, V]) Del(ctx context.Context, key K) error {
return fmt.Errorf("computing key: %w", err)
}

result := c.client.Del(ctx, keyString)
result := c.client.HDel(ctx, c.namespace, keyString)
if result.Err() != nil {
return fmt.Errorf("deleting key=%s: %w", keyString, err)
}
Expand All @@ -130,9 +123,5 @@ func (c *Map[K, V]) computeKey(ctx context.Context, key K) (string, error) {
return "", fmt.Errorf("marshalling key: %w", err)
}

if len(c.namespace) > 0 {
return fmt.Sprintf("%s:%s", c.namespace, marshaledKey), nil
}

return marshaledKey, nil
}
2 changes: 1 addition & 1 deletion mapp/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestCache(t *testing.T) {
client := redis.NewClient(&redis.Options{Addr: endpoint})

t.Run("get, set, delete string key/value", func(t *testing.T) {
cache := NewMap[string, string](client, WithNamespace[string, string](RandomNamespace()))
cache := NewMap[string, string](client, RandomNamespace())

err = cache.Set(ctx, "key", "value")
require.NoError(t, err)
Expand Down

0 comments on commit 64d3d46

Please sign in to comment.