cache

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package cache provides SQLite-based caching for TTS provider voice lists. It reduces API calls to TTS providers by caching voice information locally.

Key features:

  • SQLite-based persistent storage (~/.md2audio/voice_cache.db)
  • 30-day cache duration (configurable)
  • Provider-specific voice caching
  • Cache refresh and expiration handling
  • JSON export functionality

The cache significantly improves performance when listing voices from API-based providers like ElevenLabs, and enables offline access to voice lists.

Index

Constants

View Source
const (
	// DefaultCacheDir is the default directory for cache files
	DefaultCacheDir = ".md2audio"
	// DefaultCacheFile is the default SQLite database filename
	DefaultCacheFile = "voice_cache.db"
	// DefaultCacheDuration is how long cache entries are valid (30 days)
	// Voice lists from TTS providers don't change frequently, so a longer
	// cache duration reduces unnecessary API calls. Users can always use
	// -refresh-cache to get the latest voices when needed.
	DefaultCacheDuration = 30 * 24 * time.Hour
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheInfo

type CacheInfo struct {
	Provider    string
	Count       int
	OldestEntry time.Time
	NewestEntry time.Time
}

CacheInfo contains information about cached voices.

func (*CacheInfo) IsExpired

func (i *CacheInfo) IsExpired(duration time.Duration) bool

IsExpired returns true if the cache is expired.

type CachedProvider

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

CachedProvider wraps a TTS provider with voice caching capabilities.

func NewCachedProvider

func NewCachedProvider(provider tts.Provider, cache *VoiceCache) *CachedProvider

NewCachedProvider creates a new cached provider wrapper.

func (*CachedProvider) ExportVoicesToJSON

func (p *CachedProvider) ExportVoicesToJSON(ctx context.Context, outputPath string) error

ExportVoicesToJSON exports cached voices to a JSON file.

func (*CachedProvider) Generate

func (p *CachedProvider) Generate(ctx context.Context, req tts.GenerateRequest) (string, error)

Generate delegates to the underlying provider.

func (*CachedProvider) GetCacheInfo

func (p *CachedProvider) GetCacheInfo(ctx context.Context) (*CacheInfo, error)

GetCacheInfo returns cache information for the provider.

func (*CachedProvider) ListVoices

func (p *CachedProvider) ListVoices(ctx context.Context) ([]tts.Voice, error)

ListVoices returns cached voices if available, otherwise fetches from provider.

func (*CachedProvider) ListVoicesRefresh

func (p *CachedProvider) ListVoicesRefresh(ctx context.Context) ([]tts.Voice, error)

ListVoicesRefresh forces a refresh of the voice cache.

func (*CachedProvider) Name

func (p *CachedProvider) Name() string

Name returns the underlying provider's name.

type VoiceCache

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

VoiceCache provides caching for TTS provider voices using SQLite.

func NewVoiceCache

func NewVoiceCache() (*VoiceCache, error)

NewVoiceCache creates a new voice cache with default settings. The cache is stored in ~/.md2audio/voice_cache.db by default.

func NewVoiceCacheWithPath

func NewVoiceCacheWithPath(dbPath string, cacheDuration time.Duration) (*VoiceCache, error)

NewVoiceCacheWithPath creates a new voice cache with a custom path.

func (*VoiceCache) Clear

func (c *VoiceCache) Clear(ctx context.Context, provider string) error

Clear removes all cached voices for a provider.

func (*VoiceCache) ClearAll

func (c *VoiceCache) ClearAll(ctx context.Context) error

ClearAll removes all cached voices for all providers.

func (*VoiceCache) Close

func (c *VoiceCache) Close() error

Close closes the database connection.

func (*VoiceCache) ExportToJSON

func (c *VoiceCache) ExportToJSON(ctx context.Context, provider, outputPath string) error

ExportToJSON exports cached voices to a JSON file.

func (*VoiceCache) Get

func (c *VoiceCache) Get(ctx context.Context, provider string) ([]tts.Voice, error)

Get retrieves cached voices for a provider. Returns nil if cache is expired or doesn't exist.

func (*VoiceCache) GetCacheInfo

func (c *VoiceCache) GetCacheInfo(ctx context.Context, provider string) (*CacheInfo, error)

GetCacheInfo returns information about cached voices.

func (*VoiceCache) Set

func (c *VoiceCache) Set(ctx context.Context, provider string, voices []tts.Voice) error

Set stores voices for a provider in the cache.

func (*VoiceCache) SetLogger

func (c *VoiceCache) SetLogger(log logger.LoggerInterface)

SetLogger sets the logger for debug output.

Jump to

Keyboard shortcuts

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