Documentation ¶
Index ¶
- Constants
- Variables
- type Cache
- func (c *Cache) Get(key interface{}) (value interface{}, err error)
- func (c *Cache) GetWithTimeout(key interface{}, timeout time.Duration) (value interface{}, err error)
- func (c *Cache) Len() int
- func (c *Cache) Reserve(key interface{}) ResolveFunc
- func (c *Cache) ReserveWithLifetime(key interface{}, lifetime time.Duration) ResolveFunc
- func (c *Cache) Stats() Stats
- type Option
- type ResolveFunc
- type Stats
Constants ¶
const (
// DefaultSize is default cache size
DefaultSize = 1024
)
Variables ¶
var ( // ErrEntryNotFound is an error that describes entry not found. ErrEntryNotFound = errors.New("entry not found") // ErrExpired is an error that describes cache expired ErrExpired = errors.New("expired") // ErrGetCacheTimeout is an error of timeout getting cache. ErrGetCacheTimeout = errors.New("get cache timeout") )
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is single flight cache
func (*Cache) Get ¶
Get gets a cache value by a key. It returns ErrEntryNotFound if entry is not registered.
func (*Cache) GetWithTimeout ¶
func (c *Cache) GetWithTimeout(key interface{}, timeout time.Duration) (value interface{}, err error)
GetWithTimeout gets a cache value by a key, indicating timeout of other's fetch. It returns ErrEntryNotFound if entry is not registered, ErrGetCacheTimeout on timeout, and ErrExpired if expired.
func (*Cache) Reserve ¶
func (c *Cache) Reserve(key interface{}) ResolveFunc
Reserve reserves cache entry to fetch. Caller must try fetch the value and call resolveFunc to set result. Reserve must be called jsut once. It will panic if called two or more times.
func (*Cache) ReserveWithLifetime ¶
func (c *Cache) ReserveWithLifetime(key interface{}, lifetime time.Duration) ResolveFunc
ReserveWithLifetime reserves cache entry to fetch indicating its lifetime. Caller must try fetch the value and call resolveFunc to set result, otherwise others will wait until timeout. ReserveWithLifetime must be called jsut once. It will panic if called two or more times.
type Option ¶
type Option interface {
Apply(cache *Cache)
}
An Option is an option for a kocache
func WithDefaultLifetime ¶
WithDefaultLifetime returns an Option that defines cache default lifetime.
type ResolveFunc ¶
type ResolveFunc func(entity interface{}, err error)
ResolveFunc describes function which resolves cache.