Skip to content

Commit

Permalink
runtime/storage/cache: rename Decr->Decrement
Browse files Browse the repository at this point in the history
This was missed in the release earlier today. Fix it now
before people start depending on it and we become stuck
with inconsistent names.

Also fix a documentation error while we're at it.

Fixes encoredev#378 encoredev#379 encoredev#381
  • Loading branch information
eandre committed Sep 21, 2022
1 parent 43a88f0 commit dd3e2fc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
8 changes: 6 additions & 2 deletions docs/develop/caching.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,14 @@ type MyKey struct {

// ResourceRequestsPerUser tracks the number of requests per user and resource.
// The cache items expire after 10 seconds without activity.
var ResourceRequestsPerUser = cache.NewIntKeyspace[auth.UID](cluster, cache.KeyspaceConfig{
var ResourceRequestsPerUser = cache.NewIntKeyspace[MyKey](cluster, cache.KeyspaceConfig{
KeyPattern: "requests/:UserID/:ResourcePath",
DefaultExpiry: cache.ExpireIn(10 * time.Second),
})

// ... then:
key := MyKey{UserID: "some-user-id", ResourcePath: "/foo"}
ResourceRequestsPerUser.Increment(ctx, key, 1)
```

<Callout type="important">
Expand All @@ -147,7 +151,7 @@ Basic keyspace types include
[strings](https://pkg.go.dev/encore.dev/storage/cache#NewStringKeyspace),
[integers](https://pkg.go.dev/encore.dev/storage/cache#NewIntKeyspace),
[floats](https://pkg.go.dev/encore.dev/storage/cache#NewFloatKeyspace),
and [struct types](https://pkg.go.dev/encore.dev/storage/cache#NewStringKeyspace).
and [struct types](https://pkg.go.dev/encore.dev/storage/cache#NewStructKeyspace).
These keyspaces all share the same set of methods (along with a few keyspace-specific ones).

There are also more advanced keyspaces for storing [sets of basic types](https://pkg.go.dev/encore.dev/storage/cache#NewSetKeyspace)
Expand Down
24 changes: 12 additions & 12 deletions runtime/storage/cache/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,11 @@ func (s *IntKeyspace[K]) Delete(ctx context.Context, keys ...K) (deleted int, er
// before incrementing.
//
// Negative values can be used to decrease the value,
// but typically you want to use the Decr method for that.
// but typically you want to use the Decrement method for that.
//
// See https://redis.io/commands/incrby/ for more information.
func (s *IntKeyspace[K]) Increment(ctx context.Context, key K, delta int64) (newVal int64, err error) {
const op = "incr"
const op = "increment"
k, err := s.key(key, op)
defer s.doTrace(op, true, k)(err)
if err != nil {
Expand All @@ -299,18 +299,18 @@ func (s *IntKeyspace[K]) Increment(ctx context.Context, key K, delta int64) (new
return res, err
}

// Decr decrements the number stored in key by delta,
// Decrement decrements the number stored in key by delta,
// and returns the new value.
//
// If the key does not exist it is first created with a value of 0
// before decrementing.
//
// Negative values can be used to increase the value,
// but typically you want to use the Incr method for that.
// but typically you want to use the Increment method for that.
//
// See https://redis.io/commands/decrby/ for more information.
func (s *IntKeyspace[K]) Decr(ctx context.Context, key K, delta int64) (newVal int64, err error) {
const op = "decr"
func (s *IntKeyspace[K]) Decrement(ctx context.Context, key K, delta int64) (newVal int64, err error) {
const op = "decrement"
k, err := s.key(key, op)
defer s.doTrace(op, true, k)(err)
if err != nil {
Expand Down Expand Up @@ -419,11 +419,11 @@ func (s *FloatKeyspace[K]) Delete(ctx context.Context, keys ...K) (deleted int,
// before incrementing.
//
// Negative values can be used to decrease the value,
// but typically you want to use the Decr method for that.
// but typically you want to use the Decrement method for that.
//
// See https://redis.io/commands/incrbyfloat/ for more information.
func (s *FloatKeyspace[K]) Increment(ctx context.Context, key K, delta float64) (newVal float64, err error) {
const op = "incr"
const op = "increment"
k, err := s.key(key, op)
defer s.doTrace(op, true, k)(err)
if err != nil {
Expand All @@ -437,18 +437,18 @@ func (s *FloatKeyspace[K]) Increment(ctx context.Context, key K, delta float64)
return res, err
}

// Decr decrements the number stored in key by delta,
// Decrement decrements the number stored in key by delta,
// and returns the new value.
//
// If the key does not exist it is first created with a value of 0
// before decrementing.
//
// Negative values can be used to increase the value,
// but typically you want to use the Incr method for that.
// but typically you want to use the Increment method for that.
//
// See https://redis.io/commands/incrbyfloat/ for more information.
func (s *FloatKeyspace[K]) Decr(ctx context.Context, key K, delta float64) (newVal float64, err error) {
const op = "decr"
func (s *FloatKeyspace[K]) Decrement(ctx context.Context, key K, delta float64) (newVal float64, err error) {
const op = "decrement"
k, err := s.key(key, op)
defer s.doTrace(op, true, k)(err)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions runtime/storage/cache/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestIntKeyspace(t *testing.T) {
t.Errorf("incr: got %v, want %v", got, want)
}

if got, want := must(ks.Decr(ctx, "one", 1)), int64(3); got != want {
if got, want := must(ks.Decrement(ctx, "one", 1)), int64(3); got != want {
t.Errorf("decr: got %v, want %v", got, want)
}
}
Expand All @@ -126,7 +126,7 @@ func TestFloatKeyspace(t *testing.T) {
t.Errorf("incr: got %v, want %v", got, want)
}

if got, want := must(ks.Decr(ctx, "one", 1)), float64(3); got != want {
if got, want := must(ks.Decrement(ctx, "one", 1)), float64(3); got != want {
t.Errorf("decr: got %v, want %v", got, want)
}
}
Expand Down

0 comments on commit dd3e2fc

Please sign in to comment.