Implement and use custom caching strategies
mainThe library supports caching to avoid redundant requests. You can use built-in strategies or implement the CacheStrategy interface.
Built-in: In-Memory Cache
Use InMemoryCache for transient, memory-based caching.
Built-in: File System Cache
Use FsCache to persist cache to a directory.
Custom Cache
Implement the get and set methods of the CacheStrategy interface.
import { fetchTranscript, InMemoryCache, FsCache, CacheStrategy } from 'youtube-transcript-plus';
// In-Memory
fetchTranscript('videoId', { cache: new InMemoryCache(1800000) });
// File System
fetchTranscript('videoId', { cache: new FsCache('./my-cache-dir', 86400000) });
// Custom
class CustomCache implements CacheStrategy {
async get(key: string): Promise<string | null> { /* ... */ }
async set(key: string, value: string, ttl?: number): Promise<void> { /* ... */ }
}
fetchTranscript('videoId', { cache: new CustomCache() });