Skip to content

Commit

Permalink
TTL is upsert
Browse files Browse the repository at this point in the history
  • Loading branch information
tanveergill committed Nov 16, 2023
1 parent df067df commit f6e1d3f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
5 changes: 3 additions & 2 deletions pkg/policies/flowcontrol/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package flowcontrol
import (
"context"
"errors"
"time"

"github.com/buraksezer/olric"
olricconfig "github.com/buraksezer/olric/config"
Expand Down Expand Up @@ -73,7 +74,7 @@ func (c *Cache) Get(ctx context.Context, controlPoint, key string) ([]byte, erro
}

// Upsert inserts or updates the value for the given key.
func (c *Cache) Upsert(ctx context.Context, controlPoint, key string, value []byte) error {
func (c *Cache) Upsert(ctx context.Context, controlPoint, key string, value []byte, ttl time.Duration) error {
err := c.Ready()
if err != nil {
return err
Expand All @@ -85,7 +86,7 @@ func (c *Cache) Upsert(ctx context.Context, controlPoint, key string, value []by
return ErrCacheControlPointEmpty
}
cacheKey := formatCacheKey(controlPoint, key)
return c.dmapCache.Put(ctx, cacheKey, value)
return c.dmapCache.Put(ctx, cacheKey, value, olric.EX(ttl))
}

// Delete deletes the value for the given key.
Expand Down
7 changes: 5 additions & 2 deletions pkg/policies/flowcontrol/iface/cache.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package iface

import "context"
import (
"context"
"time"
)

// Cache is an interface for the cache.
type Cache interface {
// Get returns the value for the given key.
Get(ctx context.Context, controlPoint, key string) ([]byte, error)
// Upsert inserts or updates the value for the given key.
Upsert(ctx context.Context, controlPoint, key string, value []byte) error
Upsert(ctx context.Context, controlPoint, key string, value []byte, ttl time.Duration) error
// Delete deletes the value for the given key.
Delete(ctx context.Context, controlPoint, key string) error
}
2 changes: 1 addition & 1 deletion pkg/policies/flowcontrol/service/check/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (h *Handler) Check(ctx context.Context, req *flowcontrolv1.CheckRequest) (*

// CacheUpsert is the CacheUpsert method of Flow Control service updates the cache with the given key and value.
func (h *Handler) CacheUpsert(ctx context.Context, req *flowcontrolv1.CacheUpsertRequest) (*flowcontrolv1.CacheUpsertResponse, error) {
err := h.cache.Upsert(ctx, req.ControlPoint, req.Key, req.Value)
err := h.cache.Upsert(ctx, req.ControlPoint, req.Key, req.Value, req.Ttl.AsDuration())
if err != nil {
return &flowcontrolv1.CacheUpsertResponse{
Code: flowcontrolv1.CacheResponseCode_ERROR,
Expand Down

0 comments on commit f6e1d3f

Please sign in to comment.