-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapi.go
More file actions
115 lines (88 loc) · 3.43 KB
/
api.go
File metadata and controls
115 lines (88 loc) · 3.43 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package goxCache
import (
"context"
"fmt"
"github.com/devlibx/gox-base"
"github.com/devlibx/gox-base/errors"
)
//go:generate mockgen -source=api.go -destination=./mocks/mock_api.go -package=mockGoxCache
// Cache error
type CacheError struct {
Err error
Message string
ErrorCode string
}
var ErrNoOpCacheError = errors.New("no_op_cache")
// Configuration to setup a cache
type Config struct {
Name string
Prefix string `yaml:"prefix"`
Type string `yaml:"type"`
Endpoint string `yaml:"endpoint"`
Endpoints []string `yaml:"endpoints"`
Disabled bool `yaml:"disabled"`
Clustered bool `yaml:"clustered"`
TlsEnabled bool `yaml:"tls_enabled"`
Properties gox.StringObjectMap `yaml:"properties"`
}
// Configuration to setup a cache registry
type Configuration struct {
Disabled bool `yaml:"disabled"`
Properties gox.StringObjectMap `yaml:"properties"`
Providers map[string]Config `yaml:"caches"`
}
type SubscribeCallbackFunc func(data gox.StringObjectMap) error
type Cache interface {
// If this cache is enabled or not
IsEnabled() bool
// If the underlying server backing this cache is running or not e.g. for redis backed cache it will check if
// redis server is running or not
IsRunning(ctx context.Context) (bool, error)
// Put a key with given name. TTL=0 means never expire
Put(ctx context.Context, key string, data interface{}, ttlInSec int) (string, error)
// Put multiple key,value pairs with given name. No TTL supported
MPut(ctx context.Context, dataMap map[string]interface{}) error
// Get data for given key
Get(ctx context.Context, key string) (interface{}, string, error)
// Get data for a list of keys
MGet(ctx context.Context, keys []string) ([]interface{}, []string, error)
// Get data for given key and convert it to StringObjectMap before returning it
// Error will be returned if key is not found or if value cannot be converted to StringObjectMap
GetAsMap(ctx context.Context, key string) (gox.StringObjectMap, string, error)
// Publish data to this cache which other client can subscribe
Publish(ctx context.Context, data gox.StringObjectMap) (interface{}, error)
// Subscribe to data in this cache
Subscribe(ctx context.Context, callback SubscribeCallbackFunc) error
// Delete a key from this cache
Delete(ctx context.Context, key string) error
// Close and shutdown underlying connections
Close() error
}
type Redis interface {
Cache
PFAdd(ctx context.Context, key string, els ...interface{}) (string, error)
PFCount(ctx context.Context, keys ...string) (int64, []string, error)
}
type Registry interface {
// Register a new cache with given config
RegisterCache(config *Config) (Cache, error)
// Get a cache with name, error if cache was not registered before get
GetCache(name string) (Cache, error)
// Run a health check and give final result
// If a registry is disabled then we get status=ok. If individual caches are disabled they are marked as status=ok
HealthCheck(ctx context.Context) (gox.StringObjectMap, error)
// Shutdown the registry i.e. stop all underlying connections
Close() error
}
// Give a error as string
func (e *CacheError) Error() string {
if e.Err == nil {
return ""
} else {
return fmt.Sprintf("error=%s, erroCode=%s", e.Err.Error(), e.ErrorCode)
}
}
// Build string representation
func (e *CacheError) Unwrap() error {
return e.Err
}