Skip to content

Commit 624c71c

Browse files
committed
Make ttl/default_ttl/ttl_field optional
1 parent a3504a3 commit 624c71c

File tree

1 file changed

+53
-25
lines changed

1 file changed

+53
-25
lines changed

internal/impl/mongodb/cache.go

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
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+
115
package mongodb
216

317
import (
@@ -27,9 +41,11 @@ func mongodbCacheConfig() *service.ConfigSpec {
2741
service.NewStringField("value_field").
2842
Description("The field in the document that is used as the value."),
2943
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."),
44+
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.").
45+
Optional(),
3146
service.NewStringField("default_ttl").
32-
Description("The default TTL value."),
47+
Description("The default TTL of each item. After this period an item will be eligible for removal during the next MongoDB cleanup.").
48+
Optional(),
3349
)
3450
}
3551

@@ -62,14 +78,22 @@ func newMongodbCacheFromConfig(parsedConf *service.ParsedConfig) (*mongodbCache,
6278
return nil, err
6379
}
6480

65-
ttlField, err := parsedConf.FieldString("ttl_field")
66-
if err != nil {
67-
return nil, err
81+
var ttlField *string
82+
if parsedConf.Contains("ttl_field") {
83+
var ttlf, err = parsedConf.FieldString("ttl_field")
84+
if err != nil {
85+
return nil, err
86+
}
87+
ttlField = &ttlf
6888
}
6989

70-
defaultTTL, err := parsedConf.FieldDuration("default_ttl")
71-
if err != nil {
72-
return nil, err
90+
var defaultTTL *time.Duration
91+
if parsedConf.Contains("default_ttl") {
92+
var defTTL, err = parsedConf.FieldDuration("default_ttl")
93+
if err != nil {
94+
return nil, err
95+
}
96+
defaultTTL = &defTTL
7397
}
7498

7599
return newMongodbCache(collectionName, keyField, valueField, ttlField, defaultTTL, client, database)
@@ -83,11 +107,11 @@ type mongodbCache struct {
83107

84108
keyField string
85109
valueField string
86-
ttlField string
87-
defaultTTL time.Duration
110+
ttlField *string
111+
defaultTTL *time.Duration
88112
}
89113

90-
func newMongodbCache(collectionName, keyField, valueField, ttlField string, defaultTTL time.Duration, client *mongo.Client, database *mongo.Database) (*mongodbCache, error) {
114+
func newMongodbCache(collectionName, keyField, valueField string, ttlField *string, defaultTTL *time.Duration, client *mongo.Client, database *mongo.Database) (*mongodbCache, error) {
91115
return &mongodbCache{
92116
client: client,
93117
collection: database.Collection(collectionName),
@@ -115,30 +139,34 @@ func (m *mongodbCache) Get(ctx context.Context, key string) ([]byte, error) {
115139
}
116140

117141
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-
125142
opts := options.UpdateOne().SetUpsert(true)
126143
filter := bson.M{m.keyField: key}
127-
update := bson.M{"$set": bson.M{m.valueField: string(value), m.ttlField: expires}}
144+
val := bson.M{m.valueField: string(value)}
128145

146+
if m.ttlField != nil {
147+
if ttl != nil {
148+
val[*m.ttlField] = time.Now().Add(*ttl)
149+
} else if m.defaultTTL != nil {
150+
val[*m.ttlField] = time.Now().Add(*m.defaultTTL)
151+
}
152+
}
153+
154+
update := bson.M{"$set": val}
129155
_, err := m.collection.UpdateOne(ctx, filter, update, opts)
130156
return err
131157
}
132158

133159
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)
160+
document := bson.M{m.keyField: key, m.valueField: string(value)}
161+
162+
if m.ttlField != nil {
163+
if ttl != nil {
164+
document[*m.ttlField] = time.Now().Add(*ttl)
165+
} else if m.defaultTTL != nil {
166+
document[*m.ttlField] = time.Now().Add(*m.defaultTTL)
167+
}
139168
}
140169

141-
document := bson.M{m.keyField: key, m.valueField: string(value), m.ttlField: expires}
142170
_, err := m.collection.InsertOne(ctx, document)
143171
if err != nil {
144172
if errCode := getMongoErrorCode(err); errCode == mongoDuplicateKeyErrCode {

0 commit comments

Comments
 (0)