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

Merge branch '3-implement-cache-set-endpoint' into 'main'

Create SET cache endpoint

Closes #3

See merge request !3
parents 6924e7b5 8d0892c7
No related branches found
No related tags found
1 merge request!3Create SET cache endpoint
Pipeline #50276 passed with stage
in 45 seconds
......@@ -2,7 +2,9 @@ package cache
import (
"context"
"encoding/json"
"fmt"
"time"
"go.uber.org/zap"
......@@ -12,6 +14,7 @@ import (
type Cache interface {
Get(ctx context.Context, key string) ([]byte, error)
Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
}
type Service struct {
......@@ -26,7 +29,7 @@ func New(cache Cache, logger *zap.Logger) *Service {
}
}
func (s *Service) Get(ctx context.Context, req *cache.CacheGetRequest) ([]byte, error) {
func (s *Service) Get(ctx context.Context, req *cache.CacheGetRequest) (interface{}, error) {
var operation = zap.String("operation", "get")
if req.Key == "" || req.Namespace == "" || req.Scope == "" {
s.logger.Error("bad request: missing key or namespace or scopes", operation)
......@@ -34,7 +37,7 @@ func (s *Service) Get(ctx context.Context, req *cache.CacheGetRequest) ([]byte,
}
// create key from the input fields
key := makeCacheKey(req.Key, req.Namespace, req.Scope)
key := makeCacheKey(req.Namespace, req.Scope, req.Key)
data, err := s.cache.Get(ctx, key)
if err != nil {
s.logger.Error("error getting value from cache", zap.String("key", key), zap.Error(err))
......@@ -44,7 +47,40 @@ func (s *Service) Get(ctx context.Context, req *cache.CacheGetRequest) ([]byte,
return nil, errors.New("error getting value from cache", err)
}
return data, nil
var decodedValue interface{}
if err := json.Unmarshal(data, &decodedValue); err != nil {
s.logger.Error("cannot decode json value from cache", zap.Error(err))
return nil, errors.New("cannot decode json value from cache", err)
}
return decodedValue, nil
}
func (s *Service) Set(ctx context.Context, req *cache.CacheSetRequest) error {
var operation = zap.String("operation", "set")
if req.Key == "" || req.Namespace == "" || req.Scope == "" {
s.logger.Error("bad request: missing key or namespace or scope or data", operation)
return errors.New(errors.BadRequest, "bad request")
}
// 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.Namespace, req.Scope, req.Key)
// encode payload to json bytes for storing in cache
value, err := json.Marshal(req.Data)
if err != nil {
s.logger.Error("error encode payload to json", zap.Error(err), operation)
return errors.New("error encode payload to json", err)
}
if err := s.cache.Set(ctx, key, value, 0); err != nil {
s.logger.Error("error setting value in cache", zap.Error(err), operation)
return errors.New("error setting value in cache", err)
}
return nil
}
func makeCacheKey(namespace, scope, key string) string {
......
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