Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
cache
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Gaia-X
Trust Services API
cache
Commits
60e23039
Commit
60e23039
authored
3 years ago
by
Yordan Kinkov
Browse files
Options
Downloads
Plain Diff
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
!3
Create SET cache endpoint
Pipeline
#50276
passed
3 years ago
Stage: test
Changes
21
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
internal/service/cache/service.go
+39
-3
39 additions, 3 deletions
internal/service/cache/service.go
with
39 additions
and
3 deletions
internal/service/cache/service.go
+
39
−
3
View file @
60e23039
...
...
@@ -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
{
...
...
This diff is collapsed.
Click to expand it.
Prev
1
2
Next
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment