Documentation
¶
Overview ¶
Package ttlcache is a small on-disk key/value cache with per-entry TTLs. Built for coily after the lockdown inversion: every aws / kubectl / gh
Index ¶
- type Cache
- func (c *Cache) Get(key string) ([]byte, bool)
- func (c *Cache) GetMaxAge(key string, maxAge time.Duration) ([]byte, bool)
- func (c *Cache) GetOrSet(key string, ttl time.Duration, fetch func() ([]byte, error)) ([]byte, error)
- func (c *Cache) Invalidate(key string) error
- func (c *Cache) Set(key string, value []byte, ttl time.Duration) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct {
Dir string
}
Cache is a directory of TTL'd JSON entries on disk. One file per key, named by sha256(key) so disk layout stays bounded and predictable. The
func New ¶
New returns a Cache rooted at dir. The directory is created lazily on first Set, with mode 0o700 so other local users cannot read cached
func (*Cache) Get ¶
Get returns the cached value for key, or (nil, false) if there is no fresh entry. Any read / unmarshal / TTL failure is reported as a miss
func (*Cache) GetMaxAge ¶
GetMaxAge returns the cached value for key only if a fresh entry exists and its age is within max. Age is measured against the entry's
func (*Cache) GetOrSet ¶
func (c *Cache) GetOrSet(key string, ttl time.Duration, fetch func() ([]byte, error)) ([]byte, error)
GetOrSet returns the cached value for key, or calls fetch() and stores the result if there is no fresh entry. fetch is called at most once.
func (*Cache) Invalidate ¶
Invalidate removes the entry for key. Returns nil if the entry did not exist (idempotent), the filesystem error otherwise. Used by callers