diff --git a/pkg/policies/flowcontrol/cache.go b/pkg/policies/flowcontrol/cache.go index 19ab200853..d934127200 100644 --- a/pkg/policies/flowcontrol/cache.go +++ b/pkg/policies/flowcontrol/cache.go @@ -3,6 +3,7 @@ package flowcontrol import ( "context" "errors" + "time" "github.com/buraksezer/olric" olricconfig "github.com/buraksezer/olric/config" @@ -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 @@ -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. diff --git a/pkg/policies/flowcontrol/iface/cache.go b/pkg/policies/flowcontrol/iface/cache.go index 87f5f6d868..10714bd9d6 100644 --- a/pkg/policies/flowcontrol/iface/cache.go +++ b/pkg/policies/flowcontrol/iface/cache.go @@ -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 } diff --git a/pkg/policies/flowcontrol/service/check/check.go b/pkg/policies/flowcontrol/service/check/check.go index 73e664c757..891d05d62c 100644 --- a/pkg/policies/flowcontrol/service/check/check.go +++ b/pkg/policies/flowcontrol/service/check/check.go @@ -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,