@@ -27,9 +27,11 @@ func mongodbCacheConfig() *service.ConfigSpec {
2727 service .NewStringField ("value_field" ).
2828 Description ("The field in the document that is used as the value." ),
2929 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." ),
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+ Optional (),
3132 service .NewStringField ("default_ttl" ).
32- Description ("The default TTL value." ),
33+ Description ("The default TTL of each item. After this period an item will be eligible for removal during the next MongoDB cleanup." ).
34+ Optional (),
3335 )
3436}
3537
@@ -62,14 +64,22 @@ func newMongodbCacheFromConfig(parsedConf *service.ParsedConfig) (*mongodbCache,
6264 return nil , err
6365 }
6466
65- ttlField , err := parsedConf .FieldString ("ttl_field" )
66- if err != nil {
67- return nil , err
67+ var ttlField * string
68+ if parsedConf .Contains ("ttl_field" ) {
69+ var ttlf , err = parsedConf .FieldString ("ttl_field" )
70+ if err != nil {
71+ return nil , err
72+ }
73+ ttlField = & ttlf
6874 }
6975
70- defaultTTL , err := parsedConf .FieldDuration ("default_ttl" )
71- if err != nil {
72- return nil , err
76+ var defaultTTL * time.Duration
77+ if parsedConf .Contains ("default_ttl" ) {
78+ var defTTL , err = parsedConf .FieldDuration ("default_ttl" )
79+ if err != nil {
80+ return nil , err
81+ }
82+ defaultTTL = & defTTL
7383 }
7484
7585 return newMongodbCache (collectionName , keyField , valueField , ttlField , defaultTTL , client , database )
@@ -83,11 +93,11 @@ type mongodbCache struct {
8393
8494 keyField string
8595 valueField string
86- ttlField string
87- defaultTTL time.Duration
96+ ttlField * string
97+ defaultTTL * time.Duration
8898}
8999
90- func newMongodbCache (collectionName , keyField , valueField , ttlField string , defaultTTL time.Duration , client * mongo.Client , database * mongo.Database ) (* mongodbCache , error ) {
100+ func newMongodbCache (collectionName , keyField , valueField string , ttlField * string , defaultTTL * time.Duration , client * mongo.Client , database * mongo.Database ) (* mongodbCache , error ) {
91101 return & mongodbCache {
92102 client : client ,
93103 collection : database .Collection (collectionName ),
@@ -115,30 +125,34 @@ func (m *mongodbCache) Get(ctx context.Context, key string) ([]byte, error) {
115125}
116126
117127func (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-
125128 opts := options .UpdateOne ().SetUpsert (true )
126129 filter := bson.M {m .keyField : key }
127- update := bson.M {"$set" : bson.M {m .valueField : string (value ), m .ttlField : expires }}
130+ val := bson.M {m .valueField : string (value )}
131+
132+ if m .ttlField != nil {
133+ if ttl != nil {
134+ val [* m .ttlField ] = time .Now ().Add (* ttl )
135+ } else if m .defaultTTL != nil {
136+ val [* m .ttlField ] = time .Now ().Add (* m .defaultTTL )
137+ }
138+ }
128139
140+ update := bson.M {"$set" : val }
129141 _ , err := m .collection .UpdateOne (ctx , filter , update , opts )
130142 return err
131143}
132144
133145func (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 )
146+ document := bson.M {m .keyField : key , m .valueField : string (value )}
147+
148+ if m .ttlField != nil {
149+ if ttl != nil {
150+ document [* m .ttlField ] = time .Now ().Add (* ttl )
151+ } else if m .defaultTTL != nil {
152+ document [* m .ttlField ] = time .Now ().Add (* m .defaultTTL )
153+ }
139154 }
140155
141- document := bson.M {m .keyField : key , m .valueField : string (value ), m .ttlField : expires }
142156 _ , err := m .collection .InsertOne (ctx , document )
143157 if err != nil {
144158 if errCode := getMongoErrorCode (err ); errCode == mongoDuplicateKeyErrCode {
0 commit comments