cache

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 14 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package cache keeps recognition results (never audio) so the same file or URL is not paid for twice. Files are keyed by a SHA-256 of their bytes, so a renamed or copied file still hits; URLs are keyed by the URL and expire after 24 hours.

Index

Constants

View Source
const (
	EndpointStandard   = "recognize"
	EndpointEnterprise = "enterprise"
)

Endpoints used in cache keys.

View Source
const URLTTL = 24 * time.Hour

URLTTL is how long a URL result stays valid: the audio behind a URL can change.

Variables

This section is empty.

Functions

func KeyForFile

func KeyForFile(path string, endpoint string, params map[string]string) (string, error)

KeyForFile hashes the file's bytes with the endpoint and parameters.

func KeyForInput

func KeyForInput(in media.Input, endpoint string, params map[string]string) (string, error)

KeyForInput keys a media input (file, stdin audio, or URL).

func KeyForURL

func KeyForURL(u string, endpoint string, params map[string]string) string

KeyForURL keys a URL with the endpoint and parameters.

Types

type Cache

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

Cache is the results cache. It is safe for concurrent use.

func Open

func Open() (*Cache, error)

Open opens the cache at CacheDir()/cache.db.

func OpenAt

func OpenAt(path string) (*Cache, error)

OpenAt opens a cache database at path.

func (*Cache) Clear

func (c *Cache) Clear() error

Clear deletes every cached result.

func (*Cache) Close

func (c *Cache) Close() error

Close closes the database.

func (*Cache) Count

func (c *Cache) Count() (int, error)

Count returns the number of cached results.

func (*Cache) Get

func (c *Cache) Get(key string) (json.RawMessage, bool)

Get returns a cached result. URL entries older than URLTTL are misses.

func (*Cache) Put

func (c *Cache) Put(key string, isURL bool, result json.RawMessage) error

Put stores a result.

func (*Cache) PutEntry

func (c *Cache) PutEntry(e Entry, isURL bool) error

PutEntry stores a result with its source (path or URL, for display) and endpoint. At defaults to now.

func (*Cache) Recent

func (c *Cache) Recent(limit int) ([]Entry, error)

Recent returns the newest entries first (for the explorer's Recent tab).

func (*Cache) SetClock

func (c *Cache) SetClock(now func() time.Time)

SetClock replaces the clock (tests).

type Entry

type Entry struct {
	Key, Source string
	Endpoint    string
	At          time.Time
	Result      json.RawMessage
}

Entry is one cached result. Result is the API's result object for the standard endpoint ("null" for no match) or the array of enterprise matches.

Jump to

Keyboard shortcuts

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