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
- type CacheInfo
- type CachedProvider
- func (p *CachedProvider) ExportVoicesToJSON(ctx context.Context, outputPath string) error
- func (p *CachedProvider) Generate(ctx context.Context, req tts.GenerateRequest) (string, error)
- func (p *CachedProvider) GetCacheInfo(ctx context.Context) (*CacheInfo, error)
- func (p *CachedProvider) ListVoices(ctx context.Context) ([]tts.Voice, error)
- func (p *CachedProvider) ListVoicesRefresh(ctx context.Context) ([]tts.Voice, error)
- func (p *CachedProvider) Name() string
- type VoiceCache
- func (c *VoiceCache) Clear(ctx context.Context, provider string) error
- func (c *VoiceCache) ClearAll(ctx context.Context) error
- func (c *VoiceCache) Close() error
- func (c *VoiceCache) ExportToJSON(ctx context.Context, provider, outputPath string) error
- func (c *VoiceCache) Get(ctx context.Context, provider string) ([]tts.Voice, error)
- func (c *VoiceCache) GetCacheInfo(ctx context.Context, provider string) (*CacheInfo, error)
- func (c *VoiceCache) Set(ctx context.Context, provider string, voices []tts.Voice) error
- func (c *VoiceCache) SetLogger(log logger.LoggerInterface)
Constants ¶
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 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 ¶
ListVoices returns cached voices if available, otherwise fetches from provider.
func (*CachedProvider) ListVoicesRefresh ¶
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) ExportToJSON ¶
func (c *VoiceCache) ExportToJSON(ctx context.Context, provider, outputPath string) error
ExportToJSON exports cached voices to a JSON file.
func (*VoiceCache) Get ¶
Get retrieves cached voices for a provider. Returns nil if cache is expired or doesn't exist.
func (*VoiceCache) GetCacheInfo ¶
GetCacheInfo returns information about cached voices.
func (*VoiceCache) SetLogger ¶
func (c *VoiceCache) SetLogger(log logger.LoggerInterface)
SetLogger sets the logger for debug output.