Skip to content

Commit 73d760c

Browse files
cache/mongo: use Self-referential functions for config options
1 parent fdad031 commit 73d760c

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

mongo_cache.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@ import (
99

1010
// MongoCache holds the cache values that will be stored in mongoDB
1111
type MongoCache struct {
12+
// mongeSession specifies the mongoDB connection
1213
mongeSession *mgo.Session
13-
// cache holds the cache data
1414

15+
// CollectionName speficies the optional collection name for mongoDB
16+
// if CollectionName is not set, then default value will be set
1517
CollectionName string
18+
1619
// ttl is a duration for a cache key to expire
1720
TTL time.Duration
1821

22+
// GCInterval specifies the time duration for garbage collector time interval
1923
GCInterval time.Duration
2024

2125
// StartGC starts the garbage collector and deletes the
@@ -33,6 +37,8 @@ type MongoCache struct {
3337
sync.RWMutex
3438
}
3539

40+
type option func(*MongoCache)
41+
3642
// NewMongoCacheWithTTL creates a caching layer backed by mongo. TTL's are
3743
// managed either by a background cleaner or document is removed on the Get
3844
// operation. Mongo TTL indexes are not utilized since there can be multiple
@@ -55,7 +61,7 @@ type MongoCache struct {
5561
// NewMongoCacheWithTTL(session, func(m *MongoCache) {
5662
// m.CollectionName = "MongoCacheCollectionName"
5763
// })
58-
func NewMongoCacheWithTTL(session *mgo.Session, configs ...func(*MongoCache)) Cache {
64+
func NewMongoCacheWithTTL(session *mgo.Session, configs ...option) Cache {
5965
mc := &MongoCache{
6066
mongeSession: session,
6167
TTL: defaultExpireDuration,
@@ -69,7 +75,7 @@ func NewMongoCacheWithTTL(session *mgo.Session, configs ...func(*MongoCache)) Ca
6975
}
7076

7177
if mc.StartGC {
72-
mc.StartGCol(mc.GCInterval)
78+
mc.StartGCollector(mc.GCInterval)
7379
}
7480

7581
return mc
@@ -110,8 +116,9 @@ func (m *MongoCache) set(key string, value interface{}) error {
110116
return m.CreateKeyValueWithExpiration(kv)
111117
}
112118

113-
// StartGCol starts the garbage collector with given time interval
114-
func (m *MongoCache) StartGCol(gcInterval time.Duration) {
119+
// StartGCollector starts the garbage collector with given time interval
120+
// The expired data will be checked & deleted with given interval time
121+
func (m *MongoCache) StartGCollector(gcInterval time.Duration) {
115122
if gcInterval <= 0 {
116123
return
117124
}

0 commit comments

Comments
 (0)