/
cache.go
145 lines (119 loc) · 3.65 KB
/
cache.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package mongodb
import (
"context"
"fmt"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/benthosdev/benthos/v4/public/service"
)
const mongoDuplicateKeyErrCode = 11000
func mongodbCacheConfig() *service.ConfigSpec {
return service.NewConfigSpec().
Version("3.43.0").
Summary(`Use a MongoDB instance as a cache.`).
Fields(clientFields()...).
Fields(
service.NewStringField("collection").
Description("The name of the target collection."),
service.NewStringField("key_field").
Description("The field in the document that is used as the key."),
service.NewStringField("value_field").
Description("The field in the document that is used as the value."),
)
}
func init() {
err := service.RegisterCache(
"mongodb", mongodbCacheConfig(),
func(conf *service.ParsedConfig, mgr *service.Resources) (service.Cache, error) {
return newMongodbCacheFromConfig(conf)
})
if err != nil {
panic(err)
}
}
func newMongodbCacheFromConfig(parsedConf *service.ParsedConfig) (*mongodbCache, error) {
client, database, err := getClient(parsedConf)
if err != nil {
return nil, err
}
collectionName, err := parsedConf.FieldString("collection")
if err != nil {
return nil, err
}
keyField, err := parsedConf.FieldString("key_field")
if err != nil {
return nil, err
}
valueField, err := parsedConf.FieldString("value_field")
if err != nil {
return nil, err
}
return newMongodbCache(collectionName, keyField, valueField, client, database)
}
//------------------------------------------------------------------------------
type mongodbCache struct {
client *mongo.Client
collection *mongo.Collection
keyField string
valueField string
}
func newMongodbCache(collectionName, keyField, valueField string, client *mongo.Client, database *mongo.Database) (*mongodbCache, error) {
return &mongodbCache{
client: client,
collection: database.Collection(collectionName),
keyField: keyField,
valueField: valueField,
}, nil
}
func (m *mongodbCache) Get(ctx context.Context, key string) ([]byte, error) {
filter := bson.M{m.keyField: key}
document, err := m.collection.FindOne(ctx, filter).Raw()
if err != nil {
return nil, service.ErrKeyNotFound
}
value, err := document.LookupErr(m.valueField)
if err != nil {
return nil, fmt.Errorf("error getting field from document %s: %v", m.valueField, err)
}
valueStr := value.StringValue()
return []byte(valueStr), nil
}
func (m *mongodbCache) Set(ctx context.Context, key string, value []byte, _ *time.Duration) error {
opts := options.Update().SetUpsert(true)
filter := bson.M{m.keyField: key}
update := bson.M{"$set": bson.M{m.valueField: string(value)}}
_, err := m.collection.UpdateOne(ctx, filter, update, opts)
return err
}
func (m *mongodbCache) Add(ctx context.Context, key string, value []byte, _ *time.Duration) error {
document := bson.M{m.keyField: key, m.valueField: string(value)}
_, err := m.collection.InsertOne(ctx, document)
if err != nil {
if errCode := getMongoErrorCode(err); errCode == mongoDuplicateKeyErrCode {
err = service.ErrKeyAlreadyExists
}
}
return err
}
func (m *mongodbCache) Delete(ctx context.Context, key string) error {
filter := bson.M{m.keyField: key}
_, err := m.collection.DeleteOne(ctx, filter)
return err
}
func (m *mongodbCache) Close(ctx context.Context) error {
return m.client.Disconnect(ctx)
}
func getMongoErrorCode(err error) int {
var errorCode int
switch e := err.(type) {
default:
errorCode = 0
case mongo.WriteException:
errorCode = e.WriteErrors[0].Code
case mongo.CommandError:
errorCode = int(e.Code)
}
return errorCode
}