Skip to content
Snippets Groups Projects
Commit 92a43217 authored by Lyuben Penkovski's avatar Lyuben Penkovski
Browse files

Make namespace and scope parameters optional

parent 60e23039
Branches
Tags
1 merge request!4Make the namespace and scope fields optional when setting and getting values from cache
Pipeline #50585 passed
...@@ -7,7 +7,7 @@ var CacheGetRequest = Type("CacheGetRequest", func() { ...@@ -7,7 +7,7 @@ var CacheGetRequest = Type("CacheGetRequest", func() {
Field(1, "key", String) Field(1, "key", String)
Field(2, "namespace", String) Field(2, "namespace", String)
Field(3, "scope", String) // Initial implementation with a single scope Field(3, "scope", String) // Initial implementation with a single scope
Required("key", "namespace", "scope") Required("key")
}) })
var CacheSetRequest = Type("CacheSetRequest", func() { var CacheSetRequest = Type("CacheSetRequest", func() {
...@@ -15,5 +15,5 @@ var CacheSetRequest = Type("CacheSetRequest", func() { ...@@ -15,5 +15,5 @@ var CacheSetRequest = Type("CacheSetRequest", func() {
Field(2, "key", String) Field(2, "key", String)
Field(3, "namespace", String) Field(3, "namespace", String)
Field(4, "scope", String) // Initial implementation with a single scope Field(4, "scope", String) // Initial implementation with a single scope
Required("data", "key", "namespace", "scope") Required("data", "key")
}) })
...@@ -32,14 +32,14 @@ var MethodNames = [2]string{"Get", "Set"} ...@@ -32,14 +32,14 @@ var MethodNames = [2]string{"Get", "Set"}
// CacheGetRequest is the payload type of the cache service Get method. // CacheGetRequest is the payload type of the cache service Get method.
type CacheGetRequest struct { type CacheGetRequest struct {
Key string Key string
Namespace string Namespace *string
Scope string Scope *string
} }
// CacheSetRequest is the payload type of the cache service Set method. // CacheSetRequest is the payload type of the cache service Set method.
type CacheSetRequest struct { type CacheSetRequest struct {
Data interface{} Data interface{}
Key string Key string
Namespace string Namespace *string
Scope string Scope *string
} }
...@@ -20,13 +20,17 @@ func BuildGetPayload(cacheGetKey string, cacheGetNamespace string, cacheGetScope ...@@ -20,13 +20,17 @@ func BuildGetPayload(cacheGetKey string, cacheGetNamespace string, cacheGetScope
{ {
key = cacheGetKey key = cacheGetKey
} }
var namespace string var namespace *string
{ {
namespace = cacheGetNamespace if cacheGetNamespace != "" {
namespace = &cacheGetNamespace
}
} }
var scope string var scope *string
{ {
scope = cacheGetScope if cacheGetScope != "" {
scope = &cacheGetScope
}
} }
v := &cache.CacheGetRequest{} v := &cache.CacheGetRequest{}
v.Key = key v.Key = key
...@@ -50,13 +54,17 @@ func BuildSetPayload(cacheSetBody string, cacheSetKey string, cacheSetNamespace ...@@ -50,13 +54,17 @@ func BuildSetPayload(cacheSetBody string, cacheSetKey string, cacheSetNamespace
{ {
key = cacheSetKey key = cacheSetKey
} }
var namespace string var namespace *string
{ {
namespace = cacheSetNamespace if cacheSetNamespace != "" {
namespace = &cacheSetNamespace
}
} }
var scope string var scope *string
{ {
scope = cacheSetScope if cacheSetScope != "" {
scope = &cacheSetScope
}
} }
v := body v := body
res := &cache.CacheSetRequest{ res := &cache.CacheSetRequest{
......
...@@ -45,12 +45,12 @@ func EncodeGetRequest(encoder func(*http.Request) goahttp.Encoder) func(*http.Re ...@@ -45,12 +45,12 @@ func EncodeGetRequest(encoder func(*http.Request) goahttp.Encoder) func(*http.Re
head := p.Key head := p.Key
req.Header.Set("x-cache-key", head) req.Header.Set("x-cache-key", head)
} }
{ if p.Namespace != nil {
head := p.Namespace head := *p.Namespace
req.Header.Set("x-cache-namespace", head) req.Header.Set("x-cache-namespace", head)
} }
{ if p.Scope != nil {
head := p.Scope head := *p.Scope
req.Header.Set("x-cache-scope", head) req.Header.Set("x-cache-scope", head)
} }
return nil return nil
...@@ -119,12 +119,12 @@ func EncodeSetRequest(encoder func(*http.Request) goahttp.Encoder) func(*http.Re ...@@ -119,12 +119,12 @@ func EncodeSetRequest(encoder func(*http.Request) goahttp.Encoder) func(*http.Re
head := p.Key head := p.Key
req.Header.Set("x-cache-key", head) req.Header.Set("x-cache-key", head)
} }
{ if p.Namespace != nil {
head := p.Namespace head := *p.Namespace
req.Header.Set("x-cache-namespace", head) req.Header.Set("x-cache-namespace", head)
} }
{ if p.Scope != nil {
head := p.Scope head := *p.Scope
req.Header.Set("x-cache-scope", head) req.Header.Set("x-cache-scope", head)
} }
body := p.Data body := p.Data
......
...@@ -35,21 +35,21 @@ func DecodeGetRequest(mux goahttp.Muxer, decoder func(*http.Request) goahttp.Dec ...@@ -35,21 +35,21 @@ func DecodeGetRequest(mux goahttp.Muxer, decoder func(*http.Request) goahttp.Dec
return func(r *http.Request) (interface{}, error) { return func(r *http.Request) (interface{}, error) {
var ( var (
key string key string
namespace string namespace *string
scope string scope *string
err error err error
) )
key = r.Header.Get("x-cache-key") key = r.Header.Get("x-cache-key")
if key == "" { if key == "" {
err = goa.MergeErrors(err, goa.MissingFieldError("x-cache-key", "header")) err = goa.MergeErrors(err, goa.MissingFieldError("x-cache-key", "header"))
} }
namespace = r.Header.Get("x-cache-namespace") namespaceRaw := r.Header.Get("x-cache-namespace")
if namespace == "" { if namespaceRaw != "" {
err = goa.MergeErrors(err, goa.MissingFieldError("x-cache-namespace", "header")) namespace = &namespaceRaw
} }
scope = r.Header.Get("x-cache-scope") scopeRaw := r.Header.Get("x-cache-scope")
if scope == "" { if scopeRaw != "" {
err = goa.MergeErrors(err, goa.MissingFieldError("x-cache-scope", "header")) scope = &scopeRaw
} }
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -87,20 +87,20 @@ func DecodeSetRequest(mux goahttp.Muxer, decoder func(*http.Request) goahttp.Dec ...@@ -87,20 +87,20 @@ func DecodeSetRequest(mux goahttp.Muxer, decoder func(*http.Request) goahttp.Dec
var ( var (
key string key string
namespace string namespace *string
scope string scope *string
) )
key = r.Header.Get("x-cache-key") key = r.Header.Get("x-cache-key")
if key == "" { if key == "" {
err = goa.MergeErrors(err, goa.MissingFieldError("x-cache-key", "header")) err = goa.MergeErrors(err, goa.MissingFieldError("x-cache-key", "header"))
} }
namespace = r.Header.Get("x-cache-namespace") namespaceRaw := r.Header.Get("x-cache-namespace")
if namespace == "" { if namespaceRaw != "" {
err = goa.MergeErrors(err, goa.MissingFieldError("x-cache-namespace", "header")) namespace = &namespaceRaw
} }
scope = r.Header.Get("x-cache-scope") scopeRaw := r.Header.Get("x-cache-scope")
if scope == "" { if scopeRaw != "" {
err = goa.MergeErrors(err, goa.MissingFieldError("x-cache-scope", "header")) scope = &scopeRaw
} }
if err != nil { if err != nil {
return nil, err return nil, err
......
...@@ -12,7 +12,7 @@ import ( ...@@ -12,7 +12,7 @@ import (
) )
// NewGetCacheGetRequest builds a cache service Get endpoint payload. // NewGetCacheGetRequest builds a cache service Get endpoint payload.
func NewGetCacheGetRequest(key string, namespace string, scope string) *cache.CacheGetRequest { func NewGetCacheGetRequest(key string, namespace *string, scope *string) *cache.CacheGetRequest {
v := &cache.CacheGetRequest{} v := &cache.CacheGetRequest{}
v.Key = key v.Key = key
v.Namespace = namespace v.Namespace = namespace
...@@ -22,7 +22,7 @@ func NewGetCacheGetRequest(key string, namespace string, scope string) *cache.Ca ...@@ -22,7 +22,7 @@ func NewGetCacheGetRequest(key string, namespace string, scope string) *cache.Ca
} }
// NewSetCacheSetRequest builds a cache service Set endpoint payload. // NewSetCacheSetRequest builds a cache service Set endpoint payload.
func NewSetCacheSetRequest(body interface{}, key string, namespace string, scope string) *cache.CacheSetRequest { func NewSetCacheSetRequest(body interface{}, key string, namespace *string, scope *string) *cache.CacheSetRequest {
v := body v := body
res := &cache.CacheSetRequest{ res := &cache.CacheSetRequest{
Data: v, Data: v,
......
...@@ -56,14 +56,14 @@ func ParseEndpoint( ...@@ -56,14 +56,14 @@ func ParseEndpoint(
cacheGetFlags = flag.NewFlagSet("get", flag.ExitOnError) cacheGetFlags = flag.NewFlagSet("get", flag.ExitOnError)
cacheGetKeyFlag = cacheGetFlags.String("key", "REQUIRED", "") cacheGetKeyFlag = cacheGetFlags.String("key", "REQUIRED", "")
cacheGetNamespaceFlag = cacheGetFlags.String("namespace", "REQUIRED", "") cacheGetNamespaceFlag = cacheGetFlags.String("namespace", "", "")
cacheGetScopeFlag = cacheGetFlags.String("scope", "REQUIRED", "") cacheGetScopeFlag = cacheGetFlags.String("scope", "", "")
cacheSetFlags = flag.NewFlagSet("set", flag.ExitOnError) cacheSetFlags = flag.NewFlagSet("set", flag.ExitOnError)
cacheSetBodyFlag = cacheSetFlags.String("body", "REQUIRED", "") cacheSetBodyFlag = cacheSetFlags.String("body", "REQUIRED", "")
cacheSetKeyFlag = cacheSetFlags.String("key", "REQUIRED", "") cacheSetKeyFlag = cacheSetFlags.String("key", "REQUIRED", "")
cacheSetNamespaceFlag = cacheSetFlags.String("namespace", "REQUIRED", "") cacheSetNamespaceFlag = cacheSetFlags.String("namespace", "", "")
cacheSetScopeFlag = cacheSetFlags.String("scope", "REQUIRED", "") cacheSetScopeFlag = cacheSetFlags.String("scope", "", "")
) )
healthFlags.Usage = healthUsage healthFlags.Usage = healthUsage
healthLivenessFlags.Usage = healthLivenessUsage healthLivenessFlags.Usage = healthLivenessUsage
......
{"swagger":"2.0","info":{"title":"Cache Service","description":"The cache service exposes interface for working with Redis.","version":""},"host":"localhost:8083","consumes":["application/json","application/xml","application/gob"],"produces":["application/json","application/xml","application/gob"],"paths":{"/liveness":{"get":{"tags":["health"],"summary":"Liveness health","operationId":"health#Liveness","responses":{"200":{"description":"OK response."}},"schemes":["http"]}},"/readiness":{"get":{"tags":["health"],"summary":"Readiness health","operationId":"health#Readiness","responses":{"200":{"description":"OK response."}},"schemes":["http"]}},"/v1/cache":{"get":{"tags":["cache"],"summary":"Get cache","description":"Get JSON value from the cache.","operationId":"cache#Get","produces":["application/json"],"parameters":[{"name":"x-cache-key","in":"header","description":"Cache entry key","required":true,"type":"string"},{"name":"x-cache-namespace","in":"header","description":"Cache entry namespace","required":true,"type":"string"},{"name":"x-cache-scope","in":"header","description":"Cache entry scope","required":true,"type":"string"}],"responses":{"200":{"description":"OK response.","schema":{"type":"string","format":"binary"}}},"schemes":["http"]},"post":{"tags":["cache"],"summary":"Set cache","description":"Set a JSON value in the cache.","operationId":"cache#Set","parameters":[{"name":"x-cache-key","in":"header","description":"Cache entry key","required":true,"type":"string"},{"name":"x-cache-namespace","in":"header","description":"Cache entry namespace","required":true,"type":"string"},{"name":"x-cache-scope","in":"header","description":"Cache entry scope","required":true,"type":"string"},{"name":"any","in":"body","required":true,"schema":{"type":"string","format":"binary"}}],"responses":{"201":{"description":"Created response."}},"schemes":["http"]}}}} {"swagger":"2.0","info":{"title":"Cache Service","description":"The cache service exposes interface for working with Redis.","version":""},"host":"localhost:8083","consumes":["application/json","application/xml","application/gob"],"produces":["application/json","application/xml","application/gob"],"paths":{"/liveness":{"get":{"tags":["health"],"summary":"Liveness health","operationId":"health#Liveness","responses":{"200":{"description":"OK response."}},"schemes":["http"]}},"/readiness":{"get":{"tags":["health"],"summary":"Readiness health","operationId":"health#Readiness","responses":{"200":{"description":"OK response."}},"schemes":["http"]}},"/v1/cache":{"get":{"tags":["cache"],"summary":"Get cache","description":"Get JSON value from the cache.","operationId":"cache#Get","produces":["application/json"],"parameters":[{"name":"x-cache-key","in":"header","description":"Cache entry key","required":true,"type":"string"},{"name":"x-cache-namespace","in":"header","description":"Cache entry namespace","required":false,"type":"string"},{"name":"x-cache-scope","in":"header","description":"Cache entry scope","required":false,"type":"string"}],"responses":{"200":{"description":"OK response.","schema":{"type":"string","format":"binary"}}},"schemes":["http"]},"post":{"tags":["cache"],"summary":"Set cache","description":"Set a JSON value in the cache.","operationId":"cache#Set","parameters":[{"name":"x-cache-key","in":"header","description":"Cache entry key","required":true,"type":"string"},{"name":"x-cache-namespace","in":"header","description":"Cache entry namespace","required":false,"type":"string"},{"name":"x-cache-scope","in":"header","description":"Cache entry scope","required":false,"type":"string"},{"name":"any","in":"body","required":true,"schema":{"type":"string","format":"binary"}}],"responses":{"201":{"description":"Created response."}},"schemes":["http"]}}}}
\ No newline at end of file \ No newline at end of file
...@@ -53,12 +53,12 @@ paths: ...@@ -53,12 +53,12 @@ paths:
- name: x-cache-namespace - name: x-cache-namespace
in: header in: header
description: Cache entry namespace description: Cache entry namespace
required: true required: false
type: string type: string
- name: x-cache-scope - name: x-cache-scope
in: header in: header
description: Cache entry scope description: Cache entry scope
required: true required: false
type: string type: string
responses: responses:
"200": "200":
...@@ -83,12 +83,12 @@ paths: ...@@ -83,12 +83,12 @@ paths:
- name: x-cache-namespace - name: x-cache-namespace
in: header in: header
description: Cache entry namespace description: Cache entry namespace
required: true required: false
type: string type: string
- name: x-cache-scope - name: x-cache-scope
in: header in: header
description: Cache entry scope description: Cache entry scope
required: true required: false
type: string type: string
- name: any - name: any
in: body in: body
......
{"openapi":"3.0.3","info":{"title":"Cache Service","description":"The cache service exposes interface for working with Redis.","version":"1.0"},"servers":[{"url":"http://localhost:8083","description":"Cache Server"}],"paths":{"/liveness":{"get":{"tags":["health"],"summary":"Liveness health","operationId":"health#Liveness","responses":{"200":{"description":"OK response."}}}},"/readiness":{"get":{"tags":["health"],"summary":"Readiness health","operationId":"health#Readiness","responses":{"200":{"description":"OK response."}}}},"/v1/cache":{"get":{"tags":["cache"],"summary":"Get cache","description":"Get JSON value from the cache.","operationId":"cache#Get","parameters":[{"name":"x-cache-key","in":"header","description":"Cache entry key","allowEmptyValue":true,"required":true,"schema":{"type":"string","description":"Cache entry key","example":"did:web:example.com"},"example":"did:web:example.com"},{"name":"x-cache-namespace","in":"header","description":"Cache entry namespace","allowEmptyValue":true,"required":true,"schema":{"type":"string","description":"Cache entry namespace","example":"Login"},"example":"Login"},{"name":"x-cache-scope","in":"header","description":"Cache entry scope","allowEmptyValue":true,"required":true,"schema":{"type":"string","description":"Cache entry scope","example":"administration"},"example":"administration"}],"responses":{"200":{"description":"OK response.","content":{"application/json":{"schema":{"type":"string","example":"Qui iusto enim est dolores dolorem et.","format":"binary"},"example":"Quisquam ab dolores distinctio quis."}}}}},"post":{"tags":["cache"],"summary":"Set cache","description":"Set a JSON value in the cache.","operationId":"cache#Set","parameters":[{"name":"x-cache-key","in":"header","description":"Cache entry key","allowEmptyValue":true,"required":true,"schema":{"type":"string","description":"Cache entry key","example":"did:web:example.com"},"example":"did:web:example.com"},{"name":"x-cache-namespace","in":"header","description":"Cache entry namespace","allowEmptyValue":true,"required":true,"schema":{"type":"string","description":"Cache entry namespace","example":"Login"},"example":"Login"},{"name":"x-cache-scope","in":"header","description":"Cache entry scope","allowEmptyValue":true,"required":true,"schema":{"type":"string","description":"Cache entry scope","example":"administration"},"example":"administration"}],"requestBody":{"required":true,"content":{"application/json":{"schema":{"type":"string","example":"Et illum fugiat ut.","format":"binary"},"example":"Optio aliquam error nam."}}},"responses":{"201":{"description":"Created response."}}}}},"components":{},"tags":[{"name":"health","description":"Health service provides health check endpoints."},{"name":"cache","description":"Cache service allows storing and retrieving data from distributed cache."}]} {"openapi":"3.0.3","info":{"title":"Cache Service","description":"The cache service exposes interface for working with Redis.","version":"1.0"},"servers":[{"url":"http://localhost:8083","description":"Cache Server"}],"paths":{"/liveness":{"get":{"tags":["health"],"summary":"Liveness health","operationId":"health#Liveness","responses":{"200":{"description":"OK response."}}}},"/readiness":{"get":{"tags":["health"],"summary":"Readiness health","operationId":"health#Readiness","responses":{"200":{"description":"OK response."}}}},"/v1/cache":{"get":{"tags":["cache"],"summary":"Get cache","description":"Get JSON value from the cache.","operationId":"cache#Get","parameters":[{"name":"x-cache-key","in":"header","description":"Cache entry key","allowEmptyValue":true,"required":true,"schema":{"type":"string","description":"Cache entry key","example":"did:web:example.com"},"example":"did:web:example.com"},{"name":"x-cache-namespace","in":"header","description":"Cache entry namespace","allowEmptyValue":true,"schema":{"type":"string","description":"Cache entry namespace","example":"Login"},"example":"Login"},{"name":"x-cache-scope","in":"header","description":"Cache entry scope","allowEmptyValue":true,"schema":{"type":"string","description":"Cache entry scope","example":"administration"},"example":"administration"}],"responses":{"200":{"description":"OK response.","content":{"application/json":{"schema":{"type":"string","example":"Qui iusto enim est dolores dolorem et.","format":"binary"},"example":"Quisquam ab dolores distinctio quis."}}}}},"post":{"tags":["cache"],"summary":"Set cache","description":"Set a JSON value in the cache.","operationId":"cache#Set","parameters":[{"name":"x-cache-key","in":"header","description":"Cache entry key","allowEmptyValue":true,"required":true,"schema":{"type":"string","description":"Cache entry key","example":"did:web:example.com"},"example":"did:web:example.com"},{"name":"x-cache-namespace","in":"header","description":"Cache entry namespace","allowEmptyValue":true,"schema":{"type":"string","description":"Cache entry namespace","example":"Login"},"example":"Login"},{"name":"x-cache-scope","in":"header","description":"Cache entry scope","allowEmptyValue":true,"schema":{"type":"string","description":"Cache entry scope","example":"administration"},"example":"administration"}],"requestBody":{"required":true,"content":{"application/json":{"schema":{"type":"string","example":"Et illum fugiat ut.","format":"binary"},"example":"Optio aliquam error nam."}}},"responses":{"201":{"description":"Created response."}}}}},"components":{},"tags":[{"name":"health","description":"Health service provides health check endpoints."},{"name":"cache","description":"Cache service allows storing and retrieving data from distributed cache."}]}
\ No newline at end of file \ No newline at end of file
...@@ -47,7 +47,6 @@ paths: ...@@ -47,7 +47,6 @@ paths:
in: header in: header
description: Cache entry namespace description: Cache entry namespace
allowEmptyValue: true allowEmptyValue: true
required: true
schema: schema:
type: string type: string
description: Cache entry namespace description: Cache entry namespace
...@@ -57,7 +56,6 @@ paths: ...@@ -57,7 +56,6 @@ paths:
in: header in: header
description: Cache entry scope description: Cache entry scope
allowEmptyValue: true allowEmptyValue: true
required: true
schema: schema:
type: string type: string
description: Cache entry scope description: Cache entry scope
...@@ -94,7 +92,6 @@ paths: ...@@ -94,7 +92,6 @@ paths:
in: header in: header
description: Cache entry namespace description: Cache entry namespace
allowEmptyValue: true allowEmptyValue: true
required: true
schema: schema:
type: string type: string
description: Cache entry namespace description: Cache entry namespace
...@@ -104,7 +101,6 @@ paths: ...@@ -104,7 +101,6 @@ paths:
in: header in: header
description: Cache entry scope description: Cache entry scope
allowEmptyValue: true allowEmptyValue: true
required: true
schema: schema:
type: string type: string
description: Cache entry scope description: Cache entry scope
......
...@@ -3,7 +3,6 @@ package cache ...@@ -3,7 +3,6 @@ package cache
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"fmt"
"time" "time"
"go.uber.org/zap" "go.uber.org/zap"
...@@ -30,17 +29,26 @@ func New(cache Cache, logger *zap.Logger) *Service { ...@@ -30,17 +29,26 @@ func New(cache Cache, logger *zap.Logger) *Service {
} }
func (s *Service) Get(ctx context.Context, req *cache.CacheGetRequest) (interface{}, error) { func (s *Service) Get(ctx context.Context, req *cache.CacheGetRequest) (interface{}, error) {
var operation = zap.String("operation", "get") logger := s.logger.With(zap.String("operation", "get"))
if req.Key == "" || req.Namespace == "" || req.Scope == "" {
s.logger.Error("bad request: missing key or namespace or scopes", operation) if req.Key == "" {
logger.Error("bad request: missing key")
return nil, errors.New(errors.BadRequest, "bad request") return nil, errors.New(errors.BadRequest, "bad request")
} }
var namespace, scope string
if req.Namespace != nil {
namespace = *req.Namespace
}
if req.Scope != nil {
scope = *req.Scope
}
// create key from the input fields // create key from the input fields
key := makeCacheKey(req.Namespace, req.Scope, req.Key) key := makeCacheKey(req.Key, namespace, scope)
data, err := s.cache.Get(ctx, key) data, err := s.cache.Get(ctx, key)
if err != nil { if err != nil {
s.logger.Error("error getting value from cache", zap.String("key", key), zap.Error(err)) logger.Error("error getting value from cache", zap.String("key", key), zap.Error(err))
if errors.Is(errors.NotFound, err) { if errors.Is(errors.NotFound, err) {
return nil, errors.New("key not found in cache", err) return nil, errors.New("key not found in cache", err)
} }
...@@ -49,7 +57,7 @@ func (s *Service) Get(ctx context.Context, req *cache.CacheGetRequest) (interfac ...@@ -49,7 +57,7 @@ func (s *Service) Get(ctx context.Context, req *cache.CacheGetRequest) (interfac
var decodedValue interface{} var decodedValue interface{}
if err := json.Unmarshal(data, &decodedValue); err != nil { if err := json.Unmarshal(data, &decodedValue); err != nil {
s.logger.Error("cannot decode json value from cache", zap.Error(err)) logger.Error("cannot decode json value from cache", zap.Error(err))
return nil, errors.New("cannot decode json value from cache", err) return nil, errors.New("cannot decode json value from cache", err)
} }
...@@ -57,32 +65,47 @@ func (s *Service) Get(ctx context.Context, req *cache.CacheGetRequest) (interfac ...@@ -57,32 +65,47 @@ func (s *Service) Get(ctx context.Context, req *cache.CacheGetRequest) (interfac
} }
func (s *Service) Set(ctx context.Context, req *cache.CacheSetRequest) error { func (s *Service) Set(ctx context.Context, req *cache.CacheSetRequest) error {
var operation = zap.String("operation", "set") logger := s.logger.With(zap.String("operation", "set"))
if req.Key == "" || req.Namespace == "" || req.Scope == "" { if req.Key == "" {
s.logger.Error("bad request: missing key or namespace or scope or data", operation) logger.Error("bad request: missing key")
return errors.New(errors.BadRequest, "bad request") return errors.New(errors.BadRequest, "bad request")
} }
var namespace, scope string
if req.Namespace != nil {
namespace = *req.Namespace
}
if req.Scope != nil {
scope = *req.Scope
}
// TODO(kinkov): issue #3 - evaluate key metadata (key, namespace and scope) and set TTL over a policy execution // TODO(kinkov): issue #3 - evaluate key metadata (key, namespace and scope) and set TTL over a policy execution
// create key from the input fields // create key from the input fields
key := makeCacheKey(req.Namespace, req.Scope, req.Key) key := makeCacheKey(req.Key, namespace, scope)
// encode payload to json bytes for storing in cache // encode payload to json bytes for storing in cache
value, err := json.Marshal(req.Data) value, err := json.Marshal(req.Data)
if err != nil { if err != nil {
s.logger.Error("error encode payload to json", zap.Error(err), operation) logger.Error("error encode payload to json", zap.Error(err))
return errors.New("error encode payload to json", err) return errors.New("error encode payload to json", err)
} }
if err := s.cache.Set(ctx, key, value, 0); err != nil { if err := s.cache.Set(ctx, key, value, 0); err != nil {
s.logger.Error("error setting value in cache", zap.Error(err), operation) logger.Error("error storing value in cache", zap.Error(err))
return errors.New("error setting value in cache", err) return errors.New("error storing value in cache", err)
} }
return nil return nil
} }
func makeCacheKey(namespace, scope, key string) string { func makeCacheKey(key, namespace, scope string) string {
return fmt.Sprintf("%s,%s,%s", namespace, scope, key) k := key
if namespace != "" {
k += "," + namespace
}
if scope != "" {
k += "," + scope
}
return k
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment