-
Notifications
You must be signed in to change notification settings - Fork 45
/
namespaced.go
72 lines (57 loc) · 2.51 KB
/
namespaced.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2022 Lingfei Kong <colin404@foxmail.com>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file. The original repo for
// this file is https://github.com/superproj/onex.
//
package namespaced
//go:generate mockgen -destination mock_namespaced.go -package namespaced github.com/superproj/onex/internal/cacheserver/biz/namespaced NamespacedBiz
import (
"context"
"fmt"
"github.com/golang/protobuf/ptypes/any"
durationpb "google.golang.org/protobuf/types/known/durationpb"
v1 "github.com/superproj/onex/pkg/api/cacheserver/v1"
"github.com/superproj/onex/pkg/cache"
)
// NamespacedBiz defines the methods that need to be implemented for namespaced cache operations.
type NamespacedBiz interface {
Set(ctx context.Context, key string, value *any.Any, ttl *durationpb.Duration) error
Get(ctx context.Context, key string) (*v1.GetResponse, error)
Del(ctx context.Context, key string) error
}
// NamespacedKey represents a key with a namespace.
type NamespacedKey struct {
Namespace string
Key string
}
// namespacedBiz is a concrete implementation of NamespacedBiz.
type namespacedBiz struct {
cache cache.Cache[*any.Any]
namespace string
}
// Ensure that namespacedBiz implements the NamespacedBiz interface.
var _ NamespacedBiz = (*namespacedBiz)(nil)
// CacheKey returns the cache key for the NamespacedKey.
func (k NamespacedKey) CacheKey() string {
return fmt.Sprintf("namespace:%s:%s", k.Namespace, k.Key)
}
// New creates a new namespacedBiz instance with the specified cache and namespace.
func New(cache cache.Cache[*any.Any], namespace string) *namespacedBiz {
return &namespacedBiz{cache: cache, namespace: namespace}
}
// Set stores a value with the given key and time to live (TTL) in the namespaced cache.
func (b *namespacedBiz) Set(ctx context.Context, key string, value *any.Any, ttl *durationpb.Duration) error {
return b.cache.SetWithTTL(ctx, NamespacedKey{b.namespace, key}, value, ttl.AsDuration())
}
// Get retrieves a value from the namespaced cache by its key.
func (b *namespacedBiz) Get(ctx context.Context, key string) (*v1.GetResponse, error) {
value, ttl, err := b.cache.GetWithTTL(ctx, NamespacedKey{b.namespace, key})
if err != nil {
return nil, err
}
return &v1.GetResponse{Value: value, Expire: durationpb.New(ttl)}, nil
}
// Del deletes a value from the namespaced cache by its key.
func (b *namespacedBiz) Del(ctx context.Context, key string) error {
return b.cache.Del(ctx, NamespacedKey{b.namespace, key})
}