weakcache

package module
v0.0.0-...-90c84b3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 9, 2020 License: MIT Imports: 4 Imported by: 0

README

Weak cache

Weak cache implementation in go. It tracks the reference count of all cached records and evicts one when all references to it have been unreachable for a specified duration of time. A "reference" is simply a unique pointer to the record value which is returned to the caller.

Before returning the pointer, the reference count for the record is increased. Each unique pointer has a finalizer that will decrease the record's reference count. When all pointers to the record have been collected (no references), the record will be evicted.

The eviction delay can be controlled with minTTL. It specifies a grace period during which the record will not be evicted even if it has no references. maxTTL specifies the maximum age of a record.

Documentation

Overview

Package weakcache is weak cache implementation using finalizers and reference counting.

Index

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 a reference-counting cache which lets keys and values that have no reference outside of the cache be garbage collected.

func New

func New(gcInterval time.Duration) *Cache

New creates an empty cache with specified GC interval.

func (*Cache) Close

func (c *Cache) Close()

Close stops the cache GC loop.

func (*Cache) Fetch

func (c *Cache) Fetch(key string, minTTL, maxTTL time.Duration, fetch fetch) (*Record, error)

Fetch gets or sets a record. It calls fetch as a fallback on cache miss. minTTL specifies how long the record will survive without being referenced. maxTTL specifies the maximum lifetime of the record.

func (*Cache) Len

func (c *Cache) Len() int

Len returns the number of cached items.

type Record

type Record struct {
	Value interface{}
	// contains filtered or unexported fields
}

Record is a reference-counted cache record.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL