mirror of
https://github.com/pomerium/pomerium.git
synced 2025-05-23 22:17:14 +02:00
cache : add cache service (#457)
Signed-off-by: Bobby DeSimone <bobbydesimone@gmail.com>
This commit is contained in:
parent
8a9cb0f803
commit
dccc7cd2ff
46 changed files with 1837 additions and 587 deletions
57
internal/grpc/cache/client/cache_client.go
vendored
Normal file
57
internal/grpc/cache/client/cache_client.go
vendored
Normal file
|
@ -0,0 +1,57 @@
|
|||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/pomerium/pomerium/internal/grpc/cache"
|
||||
"github.com/pomerium/pomerium/internal/telemetry/trace"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
// Cacher specifies an interface for remote clients connecting to the cache service.
|
||||
type Cacher interface {
|
||||
Get(ctx context.Context, key string) (keyExists bool, value []byte, err error)
|
||||
Set(ctx context.Context, key string, value []byte) error
|
||||
Close() error
|
||||
}
|
||||
|
||||
// Client represents a gRPC cache service client.
|
||||
type Client struct {
|
||||
conn *grpc.ClientConn
|
||||
client cache.CacheClient
|
||||
}
|
||||
|
||||
// New returns a new gRPC cache service client.
|
||||
func New(conn *grpc.ClientConn) (p *Client) {
|
||||
return &Client{conn: conn, client: cache.NewCacheClient(conn)}
|
||||
}
|
||||
|
||||
// Get retrieves a value from the cache service.
|
||||
func (a *Client) Get(ctx context.Context, key string) (keyExists bool, value []byte, err error) {
|
||||
ctx, span := trace.StartSpan(ctx, "grpc.cache.client.Get")
|
||||
defer span.End()
|
||||
|
||||
response, err := a.client.Get(ctx, &cache.GetRequest{Key: key})
|
||||
if err != nil {
|
||||
return false, nil, err
|
||||
}
|
||||
return response.GetExists(), response.GetValue(), nil
|
||||
}
|
||||
|
||||
// Set stores a key value pair in the cache service.
|
||||
func (a *Client) Set(ctx context.Context, key string, value []byte) error {
|
||||
ctx, span := trace.StartSpan(ctx, "grpc.cache.client.Set")
|
||||
defer span.End()
|
||||
|
||||
_, err := a.client.Set(ctx, &cache.SetRequest{Key: key, Value: value})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close tears down the ClientConn and all underlying connections.
|
||||
func (a *Client) Close() error {
|
||||
return a.conn.Close()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue