Documentation
¶
Overview ¶
Package memory provides an in-process cache.Cache implementation.
Properties:
- Map-backed, sync.RWMutex-guarded; safe for concurrent use.
- TTL is honoured on read AND swept lazily by an optional janitor goroutine (interval set via Options.SweepEvery; 0 disables).
- Optional MaxEntries cap with a tiny FIFO eviction order (oldest SetTime first). Not LRU — LRU adds an extra synchronisation point per Get; if you need it, wrap an LRU pkg or vendor your own. FIFO is a reasonable default for typical web caches.
- drops.Hook fires after every operation with kind = "cache.get", "cache.set", "cache.del", "cache.exists", "cache.ttl", "cache.ping".
Index ¶
- type Cache
- func (c *Cache) Close() error
- func (c *Cache) Delete(ctx context.Context, keys ...string) (n int, err error)
- func (c *Cache) Exists(ctx context.Context, key string) (_ bool, err error)
- func (c *Cache) Get(ctx context.Context, key string) (_ []byte, err error)
- func (c *Cache) GetMulti(ctx context.Context, keys ...string) (_ map[string][]byte, err error)
- func (c *Cache) Len() int
- func (c *Cache) Ping(ctx context.Context) (err error)
- func (c *Cache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) (err error)
- func (c *Cache) SetMulti(ctx context.Context, items map[string][]byte, ttl time.Duration) (err error)
- func (c *Cache) TTL(ctx context.Context, key string) (_ time.Duration, err error)
- type Options
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is the in-memory implementation of cache.Cache.
func New ¶
New returns a memory-backed cache. The janitor (if enabled) is started immediately; Close stops it.
type Options ¶
type Options struct {
// MaxEntries caps the live entry count. When inserting beyond it,
// the oldest-inserted entry is evicted. 0 means unbounded.
MaxEntries int
// SweepEvery is the janitor interval that removes expired entries.
// 0 disables the janitor; expired entries are still skipped on
// read but the map will grow until something overwrites them.
SweepEvery time.Duration
// Hook fires after every operation, suitable for logging / metrics.
Hook drops.Hook
// Clock allows tests to inject a virtual clock; defaults to time.Now.
Clock func() time.Time
}
Options tunes the in-memory cache.
Click to show internal directories.
Click to hide internal directories.