Skip to content
Snippets Groups Projects
Commit 0b519ce1 authored by Yordan Kinkov's avatar Yordan Kinkov
Browse files

#1 Add cache TTL as optional parameter on set endpoints

parent a2a9bff3
No related branches found
No related tags found
No related merge requests found
......@@ -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)
......
......@@ -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")
})
......@@ -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)
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment