Skip to content

Commit a3504a3

Browse files
committed
Add TTL to MongoDB Cache
1 parent d7076f4 commit a3504a3

File tree

1 file changed

+38
-20
lines changed

1 file changed

+38
-20
lines changed

internal/impl/mongodb/cache.go

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,3 @@
1-
// Copyright 2024 Redpanda Data, Inc.
2-
//
3-
// Licensed under the Apache License, Version 2.0 (the "License");
4-
// you may not use this file except in compliance with the License.
5-
// You may obtain a copy of the License at
6-
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
8-
//
9-
// Unless required by applicable law or agreed to in writing, software
10-
// distributed under the License is distributed on an "AS IS" BASIS,
11-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
// See the License for the specific language governing permissions and
13-
// limitations under the License.
14-
151
package mongodb
162

173
import (
@@ -40,6 +26,10 @@ func mongodbCacheConfig() *service.ConfigSpec {
4026
Description("The field in the document that is used as the key."),
4127
service.NewStringField("value_field").
4228
Description("The field in the document that is used as the value."),
29+
service.NewStringField("ttl_field").
30+
Description("The field in the document that is used as the TTL. A TTL index on that field has to be manually added in MongoDB."),
31+
service.NewStringField("default_ttl").
32+
Description("The default TTL value."),
4333
)
4434
}
4535

@@ -72,7 +62,17 @@ func newMongodbCacheFromConfig(parsedConf *service.ParsedConfig) (*mongodbCache,
7262
return nil, err
7363
}
7464

75-
return newMongodbCache(collectionName, keyField, valueField, client, database)
65+
ttlField, err := parsedConf.FieldString("ttl_field")
66+
if err != nil {
67+
return nil, err
68+
}
69+
70+
defaultTTL, err := parsedConf.FieldDuration("default_ttl")
71+
if err != nil {
72+
return nil, err
73+
}
74+
75+
return newMongodbCache(collectionName, keyField, valueField, ttlField, defaultTTL, client, database)
7676
}
7777

7878
//------------------------------------------------------------------------------
@@ -83,14 +83,18 @@ type mongodbCache struct {
8383

8484
keyField string
8585
valueField string
86+
ttlField string
87+
defaultTTL time.Duration
8688
}
8789

88-
func newMongodbCache(collectionName, keyField, valueField string, client *mongo.Client, database *mongo.Database) (*mongodbCache, error) {
90+
func newMongodbCache(collectionName, keyField, valueField, ttlField string, defaultTTL time.Duration, client *mongo.Client, database *mongo.Database) (*mongodbCache, error) {
8991
return &mongodbCache{
9092
client: client,
9193
collection: database.Collection(collectionName),
9294
keyField: keyField,
9395
valueField: valueField,
96+
ttlField: ttlField,
97+
defaultTTL: defaultTTL,
9498
}, nil
9599
}
96100

@@ -110,17 +114,31 @@ func (m *mongodbCache) Get(ctx context.Context, key string) ([]byte, error) {
110114
return []byte(valueStr), nil
111115
}
112116

113-
func (m *mongodbCache) Set(ctx context.Context, key string, value []byte, _ *time.Duration) error {
117+
func (m *mongodbCache) Set(ctx context.Context, key string, value []byte, ttl *time.Duration) error {
118+
var expires time.Time
119+
if ttl != nil {
120+
expires = time.Now().Add(*ttl)
121+
} else {
122+
expires = time.Now().Add(m.defaultTTL)
123+
}
124+
114125
opts := options.UpdateOne().SetUpsert(true)
115126
filter := bson.M{m.keyField: key}
116-
update := bson.M{"$set": bson.M{m.valueField: string(value)}}
127+
update := bson.M{"$set": bson.M{m.valueField: string(value), m.ttlField: expires}}
117128

118129
_, err := m.collection.UpdateOne(ctx, filter, update, opts)
119130
return err
120131
}
121132

122-
func (m *mongodbCache) Add(ctx context.Context, key string, value []byte, _ *time.Duration) error {
123-
document := bson.M{m.keyField: key, m.valueField: string(value)}
133+
func (m *mongodbCache) Add(ctx context.Context, key string, value []byte, ttl *time.Duration) error {
134+
var expires time.Time
135+
if ttl != nil {
136+
expires = time.Now().Add(*ttl)
137+
} else {
138+
expires = time.Now().Add(m.defaultTTL)
139+
}
140+
141+
document := bson.M{m.keyField: key, m.valueField: string(value), m.ttlField: expires}
124142
_, err := m.collection.InsertOne(ctx, document)
125143
if err != nil {
126144
if errCode := getMongoErrorCode(err); errCode == mongoDuplicateKeyErrCode {

0 commit comments

Comments
 (0)