cache

package
v0.0.57 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2021 License: Apache-2.0, NCSA Imports: 4 Imported by: 0

Documentation

Overview

Package cache implements a simple in-memory file cache and provides a simple Fetcher wrapper that uses the cache for its Fetch operations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fetcher

func Fetcher(f analysis.Fetcher, cache *Cache) analysis.Fetcher

Fetcher creates an analysis.Fetcher that implements fetches through the cache, and delegates all other operations to f. If cache == nil, f is returned unmodified. The returned value is safe for concurrent use if f is.

func ParseByteSize

func ParseByteSize(s string) (int, error)

ParseByteSize parses a string specifying a possibly-fractional number with units and returns an equivalent value in bytes. Fractions are rounded down. Returns -1 and an error in case of invalid format.

The supported unit labels are:

B    * 2^0  bytes
K    * 2^10 bytes
M    * 2^20 bytes
G    * 2^30 bytes
T    * 2^40 bytes

The labels are case-insensitive ("10g" is the same as "10G")

Examples:

ParseByteSize("25")    ==> 25
ParseByteSize("1k")    ==> 1024
ParseByteSize("2.5G")  ==> 2684354560
ParseByteSize("10.3k") ==> 10547
ParseByteSize("-45xx") ==> -1 [error]

Types

type ByteSize

type ByteSize int

A ByteSize implements the flag.Value interface to allow specifying a cache size on the command line. Legal size values are as parsed by ParseByteSize.

func (*ByteSize) Get

func (b *ByteSize) Get() interface{}

Get implements a method of the flag.Value interface.

func (*ByteSize) Set

func (b *ByteSize) Set(s string) error

Set implements a method of the flag.Value interface.

func (*ByteSize) String

func (b *ByteSize) String() string

String implements a method of the flag.Value interface.

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

A Cache implements a limited-size cache of key-value pairs, where keys are strings and values are byte slices. Entries are evicted from the cache using a least-frequently used policy, based on how many times a given key has been fetched with Get. A *Cache is safe for concurrent use.

func New

func New(maxBytes int) *Cache

New returns a new empty cache with a capacity of maxBytes. Returns nil if maxBytes <= 0.

func (*Cache) Get

func (c *Cache) Get(key string) []byte

Get fetches the specified key from the cache, returning nil if the key is not present. A successful fetch counts as a usage for the purposes of the cache eviction policy.

func (*Cache) Has

func (c *Cache) Has(key string) bool

Has returns whether the specified key is resident in the cache. This does not affect the usage count of the key for purposes of cache eviction.

func (*Cache) Put

func (c *Cache) Put(key string, data []byte)

Put adds the specified key and data to the cache if it is not already present. If necessary, existing keys are evicted to maintain size.

func (*Cache) Stats

func (c *Cache) Stats() (residentBytes, numHits, numMisses int)

Stats returns usage statistics for the cache.

Jump to

Keyboard shortcuts

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