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") ErrNilValue = errors.New("nil value") )
View Source
var ErrLoadPanic = errors.New("loadFunc panicked")
ErrLoadPanic is what every waiter on a load receives when the loadFunc panicked instead of returning; the panic itself propagates to the loading caller.
View Source
var WithCloseTimeout = func(d time.Duration) Option {
return func(cache *oCache) {
if d > 0 {
cache.closeTimeout = d
}
}
}
WithCloseTimeout bounds Close's waits on in-flight loads and on entries another closer holds (default 10s). The value.Close calls themselves take no ctx and are not bounded. Non-positive values are ignored.
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 creates a new one via 'loadFunc';
// it also refreshes the object's GC deadline.
// When 'loadFunc' returns a non-nil error, an object will not be stored to cache.
// A load that completed by the time ctx is done still returns its value.
// Returns ErrClosed on a closed cache, including for waiters whose load
// lands after Close.
Get(ctx context.Context, id string) (value Object, err error)
// Pick returns value if it's present in cache (will not call loadFunc,
// does not refresh the GC deadline) — but it waits out an in-flight load
// for the id, bounded by ctx. Returns ErrClosed on a closed cache.
Pick(ctx context.Context, id string) (value Object, err error)
// Add adds new object to cache
// Returns error when the value is nil, the object exists or the cache is closed
Add(id string, value Object) (err error)
// Remove closes and removes object
Remove(ctx context.Context, id string) (ok bool, err error)
// RemoveSame closes and removes the object only if the value currently
// stored under id is exactly the given one (pointer identity). It lets a
// caller evict a specific instance it owns without racing a newer value
// that has replaced it under the same id. Returns ok=true only when this
// call performed the removal.
RemoveSame(ctx context.Context, id string, value Object) (ok bool, err error)
// TryRemove tries to close and to remove the object. ok reports whether
// this call removed it; (false, nil) means the object declined to close,
// is still loading, or another closer owns it. A non-nil err can
// accompany ok=true when the value closed with an error.
TryRemove(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 the cache. The pass is bounded by a close
// timeout; an entry held past it by an in-flight load or a busy closer is
// not waited for — its value is closed by that path's own closed-cache
// branch when it completes.
Close() (err error)
}
type Option ¶
type Option func(*oCache)
func WithPrometheus ¶
func WithPrometheus(reg *prometheus.Registry, namespace, subsystem string) Option
func WithPrometheusMetrics ¶ added in v0.8.9
func WithPrometheusMetrics(hit, miss, gc prometheus.Counter, size prometheus.GaugeFunc) Option
Click to show internal directories.
Click to hide internal directories.