Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrClosed = errors.New("object cache closed") ErrExists = errors.New("object exists") ErrNotExists = errors.New("object not exists") )
View Source
var WithGCPeriod = func(gcPeriod time.Duration) Option {
return func(cache *oCache) {
cache.gc = gcPeriod
}
}
View Source
var WithLogger = func(l *zap.SugaredLogger) Option {
return func(cache *oCache) {
cache.log = l
}
}
View Source
var WithTTL = func(ttl time.Duration) Option {
return func(cache *oCache) {
cache.ttl = ttl
}
}
Functions ¶
This section is empty.
Types ¶
type OCache ¶
type OCache interface { // DoLockedIfNotExists does an action if the object with id is not in cache // under a global lock, this will prevent a race which otherwise occurs // when object is created in parallel with action DoLockedIfNotExists(id string, action func() error) error // Get gets an object from cache or create a new one via 'loadFunc' // Increases the object refs counter on successful // When 'loadFunc' returns a non-nil error, an object will not be stored to cache Get(ctx context.Context, id string) (value Object, err error) // Pick returns value if it's presents in cache (will not call loadFunc) Pick(ctx context.Context, id string) (value Object, err error) // Add adds new object to cache // Returns error when object exists Add(id string, value Object) (err error) // Remove closes and removes object Remove(ctx context.Context, id string) (ok bool, err error) // ForEach iterates over all loaded objects, breaks when callback returns false ForEach(f func(v Object) (isContinue bool)) // GC frees not used and expired objects // Will automatically called every 'gcPeriod' GC() // Len returns current cache size Len() int // Close closes all objects and cache Close() (err error) }
type Option ¶
type Option func(*oCache)
func WithPrometheus ¶
func WithPrometheus(reg *prometheus.Registry, namespace, subsystem string) Option
Click to show internal directories.
Click to hide internal directories.