Documentation ¶
Index ¶
- Variables
- type Document
- type MongoCache
- func (m *MongoCache) Delete(key string) error
- func (m *MongoCache) EnsureIndex() error
- func (m *MongoCache) Get(key string) (interface{}, error)
- func (m *MongoCache) Set(key string, value interface{}) error
- func (m *MongoCache) SetEx(key string, duration time.Duration, value interface{}) error
- func (m *MongoCache) StartGC(gcInterval time.Duration)
- func (m *MongoCache) StopGC()
- type Option
Constants ¶
This section is empty.
Variables ¶
var (
ErrNotFound = errors.New("not found")
)
Functions ¶
This section is empty.
Types ¶
type Document ¶
type Document struct { Key string `bson:"_id" json:"_id"` Value interface{} `bson:"value" json:"value"` ExpireAt time.Time `bson:"expireAt" json:"expireAt"` }
Document holds the key-value pair for mongo cache
type MongoCache ¶
type MongoCache struct { // CollectionName speficies the optional collection name for mongoDB // if CollectionName is not set, then default value will be set CollectionName string // ttl is a duration for a cache key to expire TTL time.Duration // GCInterval specifies the time duration for garbage collector time interval GCInterval time.Duration // GCStart starts the garbage collector and deletes the // expired keys from mongo with given time interval GCStart bool // Mutex is used for handling the concurrent // read/write requests for cache sync.RWMutex // contains filtered or unexported fields }
MongoCache holds the cache values that will be stored in mongoDB
func NewMongoCacheWithTTL ¶
func NewMongoCacheWithTTL(session *mgo.Session, configs ...Option) *MongoCache
NewMongoCacheWithTTL creates a caching layer backed by mongo. TTL's are managed either by a background cleaner or document is removed on the Get operation. Mongo TTL indexes are not utilized since there can be multiple systems using the same collection with different TTL values.
The responsibility of stopping the GC process belongs to the user.
Session is not closed while stopping the GC.
This self-referential function satisfy you to avoid passing nil value to the function as parameter e.g (usage) : configure with defaults, just call; NewMongoCacheWithTTL(session)
configure ttl duration with;
NewMongoCacheWithTTL(session, func(m *MongoCache) { m.TTL = 2 * time.Minute })
or NewMongoCacheWithTTL(session, SetTTL(time.Minute * 2))
configure collection name with;
NewMongoCacheWithTTL(session, func(m *MongoCache) { m.CollectionName = "MongoCacheCollectionName" })
func (*MongoCache) Delete ¶
func (m *MongoCache) Delete(key string) error
Delete deletes a given key if exists
func (*MongoCache) EnsureIndex ¶
func (m *MongoCache) EnsureIndex() error
EnsureIndex ensures the index with expireAt key
func (*MongoCache) Get ¶
func (m *MongoCache) Get(key string) (interface{}, error)
Get returns a value of a given key if it exists
func (*MongoCache) Set ¶
func (m *MongoCache) Set(key string, value interface{}) error
Set will persist a value to the cache or override existing one with the new one
func (*MongoCache) SetEx ¶
func (m *MongoCache) SetEx(key string, duration time.Duration, value interface{}) error
SetEx will persist a value to the cache or override existing one with the new one with ttl duration
func (*MongoCache) StartGC ¶
func (m *MongoCache) StartGC(gcInterval time.Duration)
StartGC starts the garbage collector with given time interval The expired data will be checked & deleted with given interval time
type Option ¶
type Option func(*MongoCache)
Option sets the options specified.
func MustEnsureIndexExpireAt ¶
func MustEnsureIndexExpireAt() Option
MustEnsureIndexExpireAt ensures the expireAt index usage: NewMongoCacheWithTTL(mongoSession, MustEnsureIndexExpireAt())
func SetCollectionName ¶
SetCollectionName sets the collection name for mongoDB in MongoCache struct as option usage: NewMongoCacheWithTTL(mongoSession, SetCollectionName("mongoCollName"))
func SetGCInterval ¶
SetGCInterval sets the garbage collector interval in MongoCache struct as option usage: NewMongoCacheWithTTL(mongoSession, SetGCInterval(time*Minute))