diff --git a/design/design.go b/design/design.go index 5d526681e7f636984e8229fb5ffd58e2cbb519d5..b0de46f5bcda553a1ec8a92b595d368b8b20c097 100644 --- a/design/design.go +++ b/design/design.go @@ -83,6 +83,9 @@ var _ = Service("cache", func() { Header("scope:x-cache-scope", String, "Cache entry scope", func() { Example("administration") }) + Header("ttl:x-cache-ttl", Int, "Cache entry TTL in seconds", func() { + Example(60) + }) Body("data") Response(StatusCreated) @@ -107,6 +110,9 @@ var _ = Service("cache", func() { Header("scope:x-cache-scope", String, "Cache entry scope", func() { Example("administration") }) + Header("ttl:x-cache-ttl", Int, "Cache entry TTL in seconds", func() { + Example(60) + }) Body("data") Response(StatusOK) diff --git a/design/types.go b/design/types.go index b17285e5daaa8e7b0aca59f7355012848ad2999a..ae43f342ab8a1aa8037758c977406e4959df9ab6 100644 --- a/design/types.go +++ b/design/types.go @@ -15,5 +15,6 @@ var CacheSetRequest = Type("CacheSetRequest", func() { Field(2, "key", String) Field(3, "namespace", String) Field(4, "scope", String) // Initial implementation with a single scope + Field(5, "ttl", Int) Required("data", "key") }) diff --git a/internal/service/cache/service.go b/internal/service/cache/service.go index ef52e3069dd7cb2d1e4c400c64ad43ad8dabd7cf..aaa8ad9ce4a019c7904dbde95105984ec7cbf241 100644 --- a/internal/service/cache/service.go +++ b/internal/service/cache/service.go @@ -73,8 +73,6 @@ func (s *Service) Set(ctx context.Context, req *cache.CacheSetRequest) error { return errors.New(errors.BadRequest, "missing key") } - // TODO(kinkov): issue #3 - evaluate key metadata (key, namespace and scope) and set TTL over a policy execution - // create key from the input fields key := makeCacheKey(req.Key, req.Namespace, req.Scope) // encode payload to json bytes for storing in cache @@ -84,7 +82,13 @@ func (s *Service) Set(ctx context.Context, req *cache.CacheSetRequest) error { return errors.New(errors.BadRequest, "cannot encode payload to json", err) } - if err := s.cache.Set(ctx, key, value, 0); err != nil { + // set cache ttl if provided in request + var ttl time.Duration + if req.TTL != nil { + ttl = time.Duration(*req.TTL) * time.Second + } + + if err := s.cache.Set(ctx, key, value, ttl); err != nil { logger.Error("error storing value in cache", zap.Error(err)) return errors.New("error storing value in cache", err) }